2
0
mirror of https://github.com/lowdefy/lowdefy.git synced 2025-03-25 15:10:33 +08:00

feat(build): cleanup, test fixes

This commit is contained in:
Sam Tolmay 2020-10-21 15:11:26 +02:00
parent e3a3bf9952
commit 1c1792b690
18 changed files with 115 additions and 95 deletions

@ -36,36 +36,34 @@ Blocks:
function buildRequests(block, context) {
if (!type.isNone(block.requests)) {
if (type.isArray(block.requests)) {
block.requests.forEach((request) => {
request.requestId = request.id;
request.id = `request:${context.pageId}:${context.contextId}:${request.id}`;
context.requests.push(request);
});
delete block.requests;
} else {
if (!type.isArray(block.requests)) {
throw new Error(
`Requests is not an array at ${block.blockId} on page ${
context.pageId
}. Received ${JSON.stringify(block.requests)}`
);
}
block.requests.forEach((request) => {
request.requestId = request.id;
request.id = `request:${context.pageId}:${context.contextId}:${request.id}`;
context.requests.push(request);
});
delete block.requests;
}
if (!type.isNone(block.mutations)) {
if (type.isArray(block.mutations)) {
block.mutations.forEach((mutation) => {
mutation.mutationId = mutation.id;
mutation.id = `mutation:${context.pageId}:${context.contextId}:${mutation.id}`;
context.mutations.push(mutation);
});
delete block.mutations;
} else {
if (!type.isArray(block.mutations)) {
throw new Error(
`Mutations is not an array at ${block.blockId} on page ${
context.pageId
}. Received ${JSON.stringify(block.mutations)}`
);
}
block.mutations.forEach((mutation) => {
mutation.mutationId = mutation.id;
mutation.id = `mutation:${context.pageId}:${context.contextId}:${mutation.id}`;
context.mutations.push(mutation);
});
delete block.mutations;
}
}
@ -145,32 +143,33 @@ async function buildBlock(block, context) {
block.mutations = context.mutations;
}
if (!type.isNone(block.blocks)) {
if (type.isArray(block.blocks)) {
set(block, 'areas.content.blocks', block.blocks);
delete block.blocks;
} else {
if (!type.isArray(block.blocks)) {
throw new Error(
`Blocks at ${block.blockId} on page ${
context.pageId
} is not an array. Received ${JSON.stringify(block.blocks)}`
);
}
set(block, 'areas.content.blocks', block.blocks);
delete block.blocks;
}
if (type.isObject(block.areas)) {
let promises = [];
Object.keys(block.areas).forEach((key) => {
if (type.isArray(block.areas[key].blocks)) {
const blockPromises = block.areas[key].blocks.map(async (blk) => {
await buildBlock(blk, context);
});
promises = promises.concat(blockPromises);
} else {
if (type.isNone(block.areas[key].blocks)) {
block.areas[key].blocks = [];
}
if (!type.isArray(block.areas[key].blocks)) {
throw new Error(
`Expected blocks to be an array at ${block.blockId} in area ${key} on page ${
context.pageId
}. Received ${JSON.stringify(block.areas[key].blocks)}`
);
}
const blockPromises = block.areas[key].blocks.map(async (blk) => {
await buildBlock(blk, context);
});
promises = promises.concat(blockPromises);
});
await Promise.all(promises);
}

@ -522,6 +522,39 @@ describe('block areas', () => {
);
});
test('Add array if area blocks is undefined', async () => {
const components = {
pages: [
{
id: 'page1',
type: 'Context',
areas: {
content: {},
},
},
],
};
const res = await buildPages({ components, context });
expect(res).toEqual({
pages: [
{
id: 'page:page1',
blockId: 'page1',
pageId: 'page1',
type: 'Context',
meta: outputMetas.Context,
requests: [],
mutations: [],
areas: {
content: {
blocks: [],
},
},
},
],
});
});
test('content area on page ', async () => {
const components = {
pages: [

@ -107,7 +107,11 @@ class RefBuilder {
}
async getFileContent(path) {
return this.configLoader.load(path);
const content = await this.configLoader.load(path);
if (!content) {
throw new Error(`Tried to reference file with path "${path}", but file does not exist.`);
}
return content;
}
async build() {

@ -21,9 +21,7 @@ const configLoaderMockImplementation = (files) => {
const mockImp = (filePath) => {
const file = files.find((file) => file.path === filePath);
if (!file) {
throw new Error(
`Tried to read file with file path ${JSON.stringify(filePath)}, but file does not exist.`
);
return null;
}
return file.content;
};
@ -49,7 +47,7 @@ test('buildRefs file not found', async () => {
];
mockConfigLoader.mockImplementation(configLoaderMockImplementation(files));
await expect(buildRefs({ context })).rejects.toThrow(
'Tried to read file with file path "doesNotExist", but file does not exist.'
'Tried to reference file with path "doesNotExist", but file does not exist.'
);
});

@ -14,5 +14,5 @@ test('cleanOutputDirectory calls cleanDirectory', async () => {
outputBaseDir: 'outputBaseDir',
};
await cleanOutputDirectory({ context });
expect(cleanDirectory.mock.calls).toMatchInlineSnapshot([['outputBaseDir']]);
expect(cleanDirectory.mock.calls).toEqual([['outputBaseDir']]);
});

@ -18,7 +18,7 @@ import { type } from '@lowdefy/helpers';
async function writePage({ page, context }) {
if (!type.isObject(page)) {
throw new Error(`Page is not an object.`);
throw new Error(`Page is not an object. Received ${JSON.stringify(page)}`);
}
await context.artifactSetter.set({
filePath: `pages/${page.pageId}/${page.pageId}.json`,

@ -136,3 +136,12 @@ test('writePages pages not an array', async () => {
};
await expect(writePages({ components, context })).rejects.toThrow('Pages is not an array.');
});
test('writePages page is not an object', async () => {
const components = {
pages: ['page'],
};
await expect(writePages({ components, context })).rejects.toThrow(
'Page is not an object. Received "page"'
);
});

@ -40,7 +40,6 @@ function getRequestsAndMutationsOnBlock({ block, requests, mutations, pageId })
}
if (type.isObject(block.areas)) {
Object.keys(block.areas).forEach((key) => {
if (type.isNone(block.areas[key].blocks)) return;
if (!type.isArray(block.areas[key].blocks)) {
throw new Error(
`Blocks is not an array on page "${pageId}", block "${block.blockId}", area "${key}".`

@ -282,6 +282,26 @@ test('writeRequests to throw when blocks is not a array', async () => {
);
});
test('writeRequests to throw when block is not an object', async () => {
const components = {
pages: [
{
id: 'page:page1',
pageId: 'page1',
blockId: 'page1',
areas: {
content: {
blocks: ['block'],
},
},
},
],
};
await expect(writeRequests({ components, context })).rejects.toThrow(
'Block is not an object on page "page1".'
);
});
test('writeRequests deletes request properties', async () => {
const components = {
pages: [

@ -1,30 +0,0 @@
/*
Copyright 2020 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import createFileLoader from './fileLoader';
import createFileSetter from './fileSetter';
function createContext(bootstrapContext) {
const context = {
logger: bootstrapContext.logger,
configLoader: createFileLoader({ baseDir: bootstrapContext.configBaseDir }),
artifactSetter: createFileSetter({ baseDir: bootstrapContext.outputBaseDir }),
};
return context;
}
export default createContext;

@ -25,6 +25,12 @@ test('load file', async () => {
expect(res).toEqual('File loader text file 1.');
});
test('load file, file does not exist', async () => {
const fileLoader = createFileLoader({ baseDir });
const res = await fileLoader.load('doesNotExist.txt');
expect(res).toEqual(null);
});
test('load two files', async () => {
const fileLoader = createFileLoader({ baseDir });
const files = ['fileLoader1.txt', 'fileLoader2.txt'];
@ -35,17 +41,6 @@ test('load two files', async () => {
test('load two files, one file errors', async () => {
const fileLoader = createFileLoader({ baseDir });
const files = ['fileLoader1.txt', 'doesNotExist.txt'];
await expect(Promise.all(files.map((file) => fileLoader.load(file)))).rejects.toThrow(
'src/test/fileLoader/doesNotExist.txt", but file does not exist'
);
});
test('load file, file does not exist', async () => {
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
const fileLoader = createFileLoader({ baseDir });
// Since error message contains exact file path, test if parts of error message are present
await expect(fileLoader.load(filePath)).rejects.toThrow('Tried to read file with file path');
await expect(fileLoader.load(filePath)).rejects.toThrow(
'src/test/fileLoader/doesNotExist.txt", but file does not exist'
);
const res = await Promise.all(files.map((file) => fileLoader.load(file)));
expect(res).toEqual(['File loader text file 1.', null]);
});

@ -62,9 +62,7 @@ async function getFile(filePath) {
return handleFileType(filePath);
}
throw new Error(
`Tried to read file with file path ${JSON.stringify(
filePath
)}, but file path should be a string`
`Tried to get file with file path ${JSON.stringify(filePath)}, but file path should be a string`
);
}

@ -89,15 +89,15 @@ test('getFile text.txt', async () => {
test('getFile doesNotExist.txt', async () => {
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
// Since error message contains exact file path, test if parts of error message are present
await expect(getFile(filePath)).rejects.toThrow('Tried to read file with file path');
await expect(getFile(filePath)).rejects.toThrow(
'src/test/getFile/doesNotExist.txt", but file does not exist'
);
const file = await getFile(filePath);
expect(file).toEqual({
filePath,
content: null,
});
});
test('getFile null', async () => {
await expect(getFile(null)).rejects.toThrow(
'Tried to read file with file path null, but file path should be a string'
'Tried to get file with file path null, but file path should be a string'
);
});

@ -26,9 +26,7 @@ async function readFile(filePath) {
return file;
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(
`Tried to read file with file path ${JSON.stringify(filePath)}, but file does not exist`
);
return null;
}
throw error;
}

@ -21,16 +21,13 @@ const baseDir = path.resolve(process.cwd(), 'src/test/readFile');
test('readFile', async () => {
const filePath = path.resolve(baseDir, 'readFile.txt');
const res = await readFile(filePath);
expect(res).toEqual(`Test Read File`);
expect(res).toEqual('Test Read File');
});
test('readFile file not found throws', async () => {
const filePath = path.resolve(baseDir, 'doesNotExist.txt');
// Since error message contains exact file path, test if parts of error message are present
await expect(readFile(filePath)).rejects.toThrow('Tried to read file with file path');
await expect(readFile(filePath)).rejects.toThrow(
'src/test/readFile/doesNotExist.txt", but file does not exist'
);
const res = await readFile(filePath);
expect(res).toEqual(null);
});
test('readFile error', async () => {