fix(build): Move page not an object error to addDefaultPages.

This commit is contained in:
Gervwyk 2022-02-15 13:22:18 +02:00
parent 20f02bdef9
commit b3c980d2bf
6 changed files with 15 additions and 41 deletions

View File

@ -29,7 +29,12 @@ async function addDefaultPages({ components }) {
throw new Error('lowdefy.pages is not an array.');
}
const pageIds = components.pages.map((page) => page.id);
const pageIds = components.pages.map((page, index) => {
if (!type.isObject(page)) {
throw new Error(`pages[${index}] is not an object. Received ${JSON.stringify(page)}`);
}
return page.id;
});
// deep copy to avoid mutating defaultConfig
const filteredDefaultPages = defaultPages.filter(
(defaultPage) => !pageIds.includes(defaultPage.id)

View File

@ -204,6 +204,15 @@ test('addDefaultPages, pages not an array', async () => {
);
});
test('addDefaultPages, with a page not an object', async () => {
const components = {
pages: [null],
};
await expect(addDefaultPages({ components, context })).rejects.toThrow(
'pages[0] is not an object. Received null'
);
});
test('addDefaultPages, pages are copied', async () => {
const components1 = {};
const res1 = await addDefaultPages({ components: components1, context });

View File

@ -14,12 +14,7 @@
limitations under the License.
*/
import { type } from '@lowdefy/helpers';
async function writePage({ page, context }) {
if (!type.isObject(page)) {
throw new Error(`Page is not an object. Received ${JSON.stringify(page)}`);
}
await context.writeBuildArtifact(
`pages/${page.pageId}/${page.pageId}.json`,
JSON.stringify(page, null, 2)
@ -27,10 +22,6 @@ async function writePage({ page, context }) {
}
async function writePages({ components, context }) {
if (type.isNone(components.pages)) return;
if (!type.isArray(components.pages)) {
throw new Error(`Pages is not an array.`);
}
const writePromises = components.pages.map((page) => writePage({ page, context }));
return Promise.all(writePromises);
}

View File

@ -99,25 +99,3 @@ test('writePages no pages', async () => {
await writePages({ components, context });
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
});
test('writePages pages undefined', async () => {
const components = {};
await writePages({ components, context });
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
});
test('writePages pages not an array', async () => {
const components = {
pages: 'pages',
};
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"'
);
});

View File

@ -14,8 +14,6 @@
limitations under the License.
*/
import { type } from '@lowdefy/helpers';
async function writeRequestsOnPage({ page, context }) {
return Promise.all(
page.requests.map(async (request) => {
@ -32,7 +30,6 @@ async function writeRequestsOnPage({ page, context }) {
}
async function writeRequests({ components, context }) {
if (type.isNone(components.pages)) return;
const writePromises = components.pages.map((page) => writeRequestsOnPage({ page, context }));
return Promise.all(writePromises);
}

View File

@ -224,12 +224,6 @@ test('writeRequests empty pages array', async () => {
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
});
test('writeRequests no pages array', async () => {
const components = {};
await writeRequests({ components, context });
expect(mockWriteBuildArtifact.mock.calls).toEqual([]);
});
test('writeRequests deletes request properties', async () => {
const components = {
pages: [