Skip to content

Commit

Permalink
Merge pull request #1211 from lowdefy/add-build-test-page
Browse files Browse the repository at this point in the history
Add build test page
  • Loading branch information
Gervwyk committed May 25, 2022
2 parents 1035864 + 9ea612b commit 2ef19d9
Show file tree
Hide file tree
Showing 27 changed files with 469 additions and 304 deletions.
Expand Up @@ -17,7 +17,7 @@
import { type } from '@lowdefy/helpers';
import page404 from './404.js';

async function addDefaultPages({ components }) {
function addDefaultPages({ components }) {
// If not copied, the same object is mutated by build every time
// build runs for dev server. See #647
const defaultPages = [JSON.parse(JSON.stringify(page404))];
Expand Down
32 changes: 15 additions & 17 deletions packages/build/src/build/addDefaultPages/addDefaultPages.test.js
Expand Up @@ -31,9 +31,9 @@ beforeEach(() => {
mockLogWarn.mockReset();
});

test('addDefaultPages, no pages array', async () => {
test('addDefaultPages, no pages array', () => {
const components = {};
const res = await addDefaultPages({ components, context });
const res = addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
Expand Down Expand Up @@ -77,9 +77,9 @@ test('addDefaultPages, no pages array', async () => {
});
});

test('addDefaultPages, empty pages array', async () => {
test('addDefaultPages, empty pages array', () => {
const components = { pages: [] };
const res = await addDefaultPages({ components, context });
const res = addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
Expand Down Expand Up @@ -123,9 +123,9 @@ test('addDefaultPages, empty pages array', async () => {
});
});

test('addDefaultPages, pages without 404 page', async () => {
test('addDefaultPages, pages without 404 page', () => {
const components = { pages: [{ id: 'page1', type: 'PageHeaderMenu' }] };
const res = await addDefaultPages({ components, context });
const res = addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
Expand Down Expand Up @@ -173,14 +173,14 @@ test('addDefaultPages, pages without 404 page', async () => {
});
});

test('addDefaultPages, pages with 404 page, should not overwrite', async () => {
test('addDefaultPages, pages with 404 page, should not overwrite', () => {
const components = {
pages: [
{ id: 'page1', type: 'PageHeaderMenu' },
{ id: '404', type: 'PageHeaderMenu' },
],
};
const res = await addDefaultPages({ components, context });
const res = addDefaultPages({ components, context });
expect(res).toEqual({
pages: [
{
Expand All @@ -195,30 +195,28 @@ test('addDefaultPages, pages with 404 page, should not overwrite', async () => {
});
});

test('addDefaultPages, pages not an array', async () => {
test('addDefaultPages, pages not an array', () => {
const components = {
pages: { id: 'page1', type: 'PageHeaderMenu' },
};
await expect(addDefaultPages({ components, context })).rejects.toThrow(
'lowdefy.pages is not an array.'
);
expect(() => addDefaultPages({ components, context })).toThrow('lowdefy.pages is not an array.');
});

test('addDefaultPages, with a page not an object', async () => {
test('addDefaultPages, with a page not an object', () => {
const components = {
pages: [null],
};
await expect(addDefaultPages({ components, context })).rejects.toThrow(
expect(() => addDefaultPages({ components, context })).toThrow(
'pages[0] is not an object. Received null'
);
});

test('addDefaultPages, pages are copied', async () => {
test('addDefaultPages, pages are copied', () => {
const components1 = {};
const res1 = await addDefaultPages({ components: components1, context });
const res1 = addDefaultPages({ components: components1, context });
const page1 = res1.pages[0];
page1.id = 'page:404';
const components2 = {};
const res2 = await addDefaultPages({ components: components2, context });
const res2 = addDefaultPages({ components: components2, context });
expect(res2.pages[0].id).toEqual('404');
});
2 changes: 1 addition & 1 deletion packages/build/src/build/buildConnections.js
Expand Up @@ -20,7 +20,7 @@ import { type } from '@lowdefy/helpers';
import countOperators from '../utils/countOperators.js';
import createCheckDuplicateId from '../utils/createCheckDuplicateId.js';

async function buildConnections({ components, context }) {
function buildConnections({ components, context }) {
const checkDuplicateConnectionId = createCheckDuplicateId({
message: 'Duplicate connectionId "{{ id }}".',
});
Expand Down
28 changes: 14 additions & 14 deletions packages/build/src/build/buildConnections.test.js
Expand Up @@ -19,23 +19,23 @@ import testContext from '../test/testContext.js';

const context = testContext();

test('buildConnections no connections', async () => {
test('buildConnections no connections', () => {
const components = {};
const res = await buildConnections({ components, context });
const res = buildConnections({ components, context });
expect(res.connections).toBe(undefined);
});

test('buildConnections connections not an array', async () => {
test('buildConnections connections not an array', () => {
const components = {
connections: 'connections',
};
const res = await buildConnections({ components, context });
const res = buildConnections({ components, context });
expect(res).toEqual({
connections: 'connections',
});
});

test('buildConnections', async () => {
test('buildConnections', () => {
const components = {
connections: [
{
Expand All @@ -48,7 +48,7 @@ test('buildConnections', async () => {
},
],
};
const res = await buildConnections({ components, context });
const res = buildConnections({ components, context });
expect(res.connections).toEqual([
{
id: 'connection:connection1',
Expand All @@ -63,32 +63,32 @@ test('buildConnections', async () => {
]);
});

test('throw on missing id', async () => {
test('throw on missing id', () => {
const components = {
connections: [{ type: 'ConnectionType' }],
};
await expect(buildConnections({ components, context })).rejects.toThrow('Connection id missing.');
expect(() => buildConnections({ components, context })).toThrow('Connection id missing.');
});

test('connection id is not a string', async () => {
test('connection id is not a string', () => {
const components = {
connections: [{ id: 1 }],
};
await expect(buildConnections({ components, context })).rejects.toThrow(
expect(() => buildConnections({ components, context })).toThrow(
'Connection id is not a string. Received 1.'
);
});

test('throw on missing type', async () => {
test('throw on missing type', () => {
const components = {
connections: [{ id: 'connection1' }],
};
await expect(buildConnections({ components, context })).rejects.toThrow(
expect(() => buildConnections({ components, context })).toThrow(
'Connection type is not a string at connection "connection1". Received undefined.'
);
});

test('throw on Duplicate ids', async () => {
test('throw on Duplicate ids', () => {
const components = {
connections: [
{
Expand All @@ -101,7 +101,7 @@ test('throw on Duplicate ids', async () => {
},
],
};
await expect(buildConnections({ components, context })).rejects.toThrow(
expect(() => buildConnections({ components, context })).toThrow(
'Duplicate connectionId "connection1".'
);
});
18 changes: 8 additions & 10 deletions packages/build/src/build/buildMenu.js
Expand Up @@ -19,7 +19,7 @@
import { type } from '@lowdefy/helpers';
import createCheckDuplicateId from '../utils/createCheckDuplicateId.js';

async function buildDefaultMenu({ components, context }) {
function buildDefaultMenu({ components, context }) {
context.logger.warn('No menus found. Building default menu.');
const pages = type.isArray(components.pages) ? components.pages : [];
const menus = [
Expand Down Expand Up @@ -72,10 +72,10 @@ function loopItems({ parent, menuId, pages, missingPageWarnings, checkDuplicateM
}
}

async function buildMenu({ components, context }) {
function buildMenu({ components, context }) {
const pages = type.isArray(components.pages) ? components.pages : [];
if (type.isUndefined(components.menus) || components.menus.length === 0) {
components.menus = await buildDefaultMenu({ components, context });
components.menus = buildDefaultMenu({ components, context });
}
const missingPageWarnings = [];
const checkDuplicateMenuId = createCheckDuplicateId({ message: 'Duplicate menuId "{{ id }}".' });
Expand All @@ -100,13 +100,11 @@ async function buildMenu({ components, context }) {
checkDuplicateMenuItemId,
});
});
await Promise.all(
missingPageWarnings.map(async (warning) => {
await context.logger.warn(
`Page "${warning.pageId}" referenced in menu link "${warning.menuItemId}" not found.`
);
})
);
missingPageWarnings.map(async (warning) => {
context.logger.warn(
`Page "${warning.pageId}" referenced in menu link "${warning.menuItemId}" not found.`
);
});
return components;
}

Expand Down

0 comments on commit 2ef19d9

Please sign in to comment.