Skip to content

Commit

Permalink
fix(dumper): ignore stats model name when generating/updating project…
Browse files Browse the repository at this point in the history
… to avoid route conflicts (#216)
  • Loading branch information
jeffladiray authored Aug 25, 2021
1 parent b159a4f commit 649f802
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/services/dumper/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class Dumper {
}));
}

// HACK: If a table name is "sessions" or "stats" the generated routes will conflict with
// Forest Admin internal route (session or stats creation).
static shouldSkipRouteGenerationForModel(modelName) {
return ['sessions', 'stats'].includes(modelName.toLowerCase());
}

isLinuxBasedOs() {
return this.os.platform() === 'linux';
}
Expand Down Expand Up @@ -435,10 +441,11 @@ class Dumper {
if (!isUpdate) this.copyTemplate(projectPath, 'public/favicon.png', 'public/favicon.png');

modelNames.forEach((modelName) => {
// HACK: If a table name is "sessions" the generated routes will conflict with Forest Admin
// internal session creation route. As a workaround, we don't generate the route file.
// HACK: If a table name is "sessions" or "stats" the generated routes will conflict with
// Forest Admin internal route (session or stats creation).
// As a workaround, we don't generate the route file.
// TODO: Remove the if condition, once the routes paths refactored to prevent such conflict.
if (modelName !== 'sessions') {
if (!Dumper.shouldSkipRouteGenerationForModel(modelName)) {
this.writeRoute(projectPath, config, modelName);
}
});
Expand Down
25 changes: 24 additions & 1 deletion test/services/analyzer/dumper.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ describe('services > dumper (unit)', () => {
});

it('should call all the mandatory functions required to update project', async () => {
expect.assertions(18);
expect.assertions(19);

const mkdirpMock = jest.fn();
const dumper = createDumper({
Expand All @@ -621,6 +621,7 @@ describe('services > dumper (unit)', () => {
const writeDockerfileSpy = jest.spyOn(dumper, 'writeDockerfile').mockImplementation();
const writePackageJsonSpy = jest.spyOn(dumper, 'writePackageJson').mockImplementation();
const copyTemplateSpy = jest.spyOn(dumper, 'copyTemplate').mockImplementation();
jest.spyOn(Dumper, 'shouldSkipRouteGenerationForModel');

const schema = {
testModel: { fields: {}, references: [], options: {} },
Expand Down Expand Up @@ -658,6 +659,8 @@ describe('services > dumper (unit)', () => {
expect(writeDockerfileSpy).not.toHaveBeenCalled();
expect(writePackageJsonSpy).not.toHaveBeenCalled();

expect(Dumper.shouldSkipRouteGenerationForModel).toHaveBeenCalledWith('testModel');

// Copied files
expect(copyTemplateSpy).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -869,4 +872,24 @@ describe('services > dumper (unit)', () => {
expect(dumper.hasMultipleDatabaseStructure()).toStrictEqual(true);
});
});

describe('shouldSkipRouteGenerationForModel', () => {
describe('when a given model should be ignored', () => {
it('should return true', () => {
expect.assertions(2);

expect(Dumper.shouldSkipRouteGenerationForModel('stats')).toStrictEqual(true);
expect(Dumper.shouldSkipRouteGenerationForModel('Sessions')).toStrictEqual(true);
});
});

describe('when a given model should not be ignored', () => {
it('should return false', () => {
expect.assertions(2);

expect(Dumper.shouldSkipRouteGenerationForModel('users')).toStrictEqual(false);
expect(Dumper.shouldSkipRouteGenerationForModel('projects')).toStrictEqual(false);
});
});
});
});

0 comments on commit 649f802

Please sign in to comment.