add test for replaceIdByPrimaryKey

Signed-off-by: Pierre Noël <pierre.noel@strapi.io>
This commit is contained in:
Pierre Noël 2020-03-10 16:01:10 +01:00
parent a1774db410
commit 15f4ae67d7
3 changed files with 68 additions and 13 deletions

View File

@ -0,0 +1,53 @@
const { replaceIdByPrimaryKey } = require('../primary-key');
describe('Primary Key', () => {
describe('replaceIdByPrimaryKey', () => {
const defaultPostgresModel = { primaryKey: 'id' };
const defaultMongooseModel = { primaryKey: '_id' };
const customModel = { primaryKey: 'aRandomPrimaryKey' };
describe('Model primary key is "id"', () => {
test('Params has "id"', () => {
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, defaultPostgresModel);
expect(result).toEqual({ id: '123', color: 'red' });
});
test(`Params doesn't have "id"`, () => {
const result = replaceIdByPrimaryKey({ color: 'red' }, defaultPostgresModel);
expect(result).toEqual({ color: 'red' });
});
});
describe('Model primary key is "_id"', () => {
test('Params has "_id"', () => {
const result = replaceIdByPrimaryKey({ _id: '123', color: 'red' }, defaultMongooseModel);
expect(result).toEqual({ _id: '123', color: 'red' });
});
test('Params has "id"', () => {
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, defaultMongooseModel);
expect(result).toEqual({ _id: '123', color: 'red' });
});
test(`Params doesn't have "id" nor "_id"`, () => {
const result = replaceIdByPrimaryKey({ color: 'red' }, defaultMongooseModel);
expect(result).toEqual({ color: 'red' });
});
});
describe('Model primary key is "aRandomPrimaryKey"', () => {
test('Params has "id"', () => {
const result = replaceIdByPrimaryKey({ id: '123', color: 'red' }, customModel);
expect(result).toEqual({ aRandomPrimaryKey: '123', color: 'red' });
});
test('Params has "aRandomPrimaryKey"', () => {
const result = replaceIdByPrimaryKey(
{ aRandomPrimaryKey: '123', color: 'red' },
customModel
);
expect(result).toEqual({ aRandomPrimaryKey: '123', color: 'red' });
});
test(`Params doesn't have "id" nor "aRandomPrimaryKey"`, () => {
const result = replaceIdByPrimaryKey({ color: 'red' }, customModel);
expect(result).toEqual({ color: 'red' });
});
});
});
});

View File

@ -2,13 +2,18 @@
const _ = require('lodash');
module.exports = {
replaceIdByPrimaryKey: (params, model) => {
const newParams = { ...params };
if (_.has(params, 'id')) {
delete newParams.id;
newParams[model.primaryKey] = params[model.primaryKey] || params.id;
}
return newParams;
},
/**
* If exists, rename the key "id" by the primary key name of the model ("_id" by default for mongoose).
*/
const replaceIdByPrimaryKey = (params, model) => {
const newParams = { ...params };
if (_.has(params, 'id')) {
delete newParams.id;
newParams[model.primaryKey] = params[model.primaryKey] || params.id;
}
return newParams;
};
module.exports = {
replaceIdByPrimaryKey,
};

View File

@ -6,10 +6,7 @@ let modelsUtils;
let rq;
describe.each([
[
'CONTENT MANAGER',
'/content-manager/explorer/application::withcomponent.withcomponent',
],
['CONTENT MANAGER', '/content-manager/explorer/application::withcomponent.withcomponent'],
['GENERATED API', '/withcomponents'],
])('[%s] => Non repeatable and Not required component', (_, path) => {
beforeAll(async () => {