Skip to content

Commit

Permalink
ENG-4468: filter out the fragments with hyphen in code
Browse files Browse the repository at this point in the history
  • Loading branch information
antromeo committed Jul 6, 2023
1 parent 6bd0956 commit e6f0697
Show file tree
Hide file tree
Showing 3 changed files with 823 additions and 17 deletions.
10 changes: 7 additions & 3 deletions lib/env-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,16 @@ const componentResponseProcessor = {
const schema = {
code: [{
key: 'code',
transform: function (code) { return code.trim().toLowerCase().replace(new RegExp('[\\s\\-_]+', 'g'), '_'); },
transform: function (code) {
return code.trim().replace(new RegExp(/[\s_]/, 'g'), '_');
},
}],
guiCode: 'guiCode',
};

return fragments.map(f => objectMapper(f, schema));
return fragments
.filter(f => !f.code.includes('-'))
.map(f => objectMapper(f, schema));
},
widget: async (componentsToFetch) => {
const widgets = await Promise.all(componentsToFetch.map(async (widget) => {
Expand Down Expand Up @@ -809,7 +813,7 @@ function canonizeCode (code) {
}

function canonizeCodeUnderline (code) {
return code.trim().toLowerCase().replace(new RegExp('[\\s\\-_]+', 'g'), '_');
return code.trim().replace(new RegExp(/[\s_]/, 'g'), '_');
}

const urlEncoder = function (payload) {
Expand Down
100 changes: 86 additions & 14 deletions test/env-bundler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,47 @@ const fs = require('fs');
const path = require('path');
const os = require('os');
const process = require('process');
const yaml = require('yaml');

const widgets = require('./mocks/widgets');
const widget = require('./mocks/widget');
const fragments = require('./mocks/fragments');
const resourceFile = require('./mocks/resource-file');
const resourceDirectory = require('./mocks/resource-directory');

const { setupEnvironment, collectAllComponents, generateBundle, ALL_TYPES } = require('../lib/env-bundler');
const { setupEnvironment, collectAllComponents, generateBundle } = require('../lib/env-bundler');

const apiUrlTable = {
widget: /\/api\/widgets/,
widgetDetails: /\/api\/widgets\/.+/,
fragments: /\/api\/fragments/,
resource: /\/api\/fileBrowser?protectedFolder=false&currentPath=.+/,
resourceDetails: /\/api\/fileBrowser\/file\?protectedFolder=false&currentPath=.+/,
};

jest.mock('axios');

class AxiosMock {
static async mockGetCalls () {
await axios.get.mockImplementation((url) => {
switch (true) {
case apiUrlTable.widget.test(url):
return Promise.resolve({ data: widgets });
case apiUrlTable.widgetDetails.test(url):
return Promise.resolve({ data: widget });
case apiUrlTable.fragments.test(url):
return Promise.resolve({ data: fragments });
case apiUrlTable.resourceDetails.test(url):
return Promise.resolve({ data: resourceFile });
case apiUrlTable.resource.test(url):
return Promise.resolve({ data: resourceDirectory });
default:
return Promise.resolve({ data: { payload: [] } });
}
});
}
}

describe('env-bundler', function () {
let tmpDir;

Expand Down Expand Up @@ -49,20 +80,11 @@ describe('env-bundler', function () {

afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
jest.restoreAllMocks();
});

it('Should save resources skipping the directories', async () => {
for (const componentType of ALL_TYPES) {
if (componentType === 'widget') {
axios.get.mockImplementationOnce(() => Promise.resolve({ data: widgets }));
} else {
axios.get.mockImplementationOnce(() => Promise.resolve({ data: { payload: [] } }));
}
}

axios.get.mockImplementationOnce(() => Promise.resolve({ data: widget }));
axios.get.mockImplementationOnce(() => Promise.resolve({ data: resourceFile }));
axios.get.mockImplementationOnce(() => Promise.resolve({ data: resourceDirectory }));
await AxiosMock.mockGetCalls();

await collectAllComponents();

Expand All @@ -86,13 +108,63 @@ describe('env-bundler', function () {
expect(fs.existsSync(expectedWidgetDescriptor)).toBe(true);
expect(fs.existsSync(expectedResource)).toBe(true);

expect(axios.get).toHaveBeenNthCalledWith(16,
expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining('/api/fileBrowser/file?protectedFolder=false&currentPath=bundles/my-bundle/widgets/my-widget/static/css/main.dbf0c21a.css'),
expect.any(Object),
);
expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining('/api/fileBrowser/file?protectedFolder=false&currentPath=bundles/my-bundle/widgets/my-widget'),
expect.any(Object),
);
});

it('Should save fragment filtering out those with hyphen in the code', async () => {
await AxiosMock.mockGetCalls();

const components = await collectAllComponents();

const options = {
generateBundle: true,
location: './',
code: 'test-directories',
description: 'test-directories',
};

await generateBundle(options, components);

const expectedDescriptor = path.resolve(tmpDir, 'descriptor.yaml');
const expectedWidgetDescriptor = path.resolve(tmpDir, 'widgets', 'my-widget-descriptor.yaml');
const expectedResource = path.resolve(tmpDir, 'resources', 'bundles', 'my-bundle', 'widgets', 'my-widget', 'static', 'css', 'main.dbf0c21a.css');

expect(fs.existsSync(expectedDescriptor)).toBe(true);
expect(fs.existsSync(expectedWidgetDescriptor)).toBe(true);
expect(fs.existsSync(expectedResource)).toBe(true);

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining('/api/fileBrowser/file?protectedFolder=false&currentPath=bundles/my-bundle/widgets/my-widget/static/css/main.dbf0c21a.css'),
expect.any(Object),
);
expect(axios.get).toHaveBeenNthCalledWith(17,
expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining('/api/fileBrowser/file?protectedFolder=false&currentPath=bundles/my-bundle/widgets/my-widget'),
expect.any(Object),
);

const descUtf8 = fs.readFileSync(path.resolve(tmpDir, 'descriptor.yaml'), 'utf8');
const descriptor = yaml.parse(descUtf8);

const actualFragments = await getFragments();
expect(descriptor.components.fragments.length).toBe(actualFragments.length);
const actualCodesOfFragments = actualFragments.map(f => f.code);
expect(actualCodesOfFragments.includes('-')).toBe(false);
});

async function getFragments () {
const fragmentList = fs.readdirSync(path.resolve(tmpDir, 'fragments'));
const promises = fragmentList.map(fragmentFileName => {
const fragmentDescUtf8 = fs.readFileSync((path.resolve(tmpDir, 'fragments', fragmentFileName)), 'utf8');
return yaml.parse(fragmentDescUtf8);
});

return Promise.all(promises);
}
});
Loading

0 comments on commit e6f0697

Please sign in to comment.