-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(serialiser): serialiser does not compute hidden smart fields anym…
…ore (#1020)
- Loading branch information
1 parent
419acd9
commit 12bd547
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const RecordSerializer = require('../../../src/services/exposed/record-serializer'); | ||
const Schemas = require('../../../src/generators/schemas'); | ||
const usersSchema = require('../../fixtures/users-schema'); | ||
|
||
const getMockedContext = () => ({ | ||
configStore: { | ||
Implementation: { | ||
getModelName: (model) => model.name, | ||
getLianaName: () => 'name', | ||
getOrmVersion: () => 'version', | ||
}, | ||
integrator: {}, | ||
}, | ||
}); | ||
|
||
describe('services › exposed › record-serializer', () => { | ||
describe('when model is not provided', () => { | ||
it('should throw an error', () => { | ||
expect(() => new RecordSerializer(null, null, null, getMockedContext())).toThrow('RecordSerializer initialization error: missing first argument "model"'); | ||
}); | ||
}); | ||
|
||
describe('when model is provided', () => { | ||
describe('when model is incorrect', () => { | ||
it('should throw an error', () => { | ||
expect(() => new RecordSerializer(1, null, null, getMockedContext())).toThrow('RecordSerializer initialization error: "model" argument should be an object (ex: `{ name: "myModel" }`)'); | ||
}); | ||
}); | ||
|
||
describe('when no query is specified', () => { | ||
it('should compute all the smart fields', async () => { | ||
Schemas.schemas = { users: usersSchema }; | ||
const modelMock = { name: 'users' }; | ||
|
||
const serializer = new RecordSerializer(modelMock, null, null, getMockedContext()); | ||
|
||
const articlesSerialized = await serializer.serialize([{ | ||
hasAddress: true, | ||
id: '1', | ||
name: 'foo', | ||
}]); | ||
|
||
expect(articlesSerialized.data[0].attributes).toContainAllKeys(['hasAddress', 'id', 'name', 'smart']); | ||
}); | ||
}); | ||
|
||
describe('when query is specified', () => { | ||
describe('when smart field is not specified in query', () => { | ||
it('should not compute smart field', async () => { | ||
Schemas.schemas = { users: usersSchema }; | ||
const modelMock = { name: 'users' }; | ||
const queryFields = { fields: { users: 'hasAddress' } }; | ||
|
||
const serializer = new RecordSerializer(modelMock, null, queryFields, { | ||
configStore: { | ||
Implementation: { | ||
getModelName: (model) => model.name, | ||
getLianaName: () => 'forest-express-sequelize', | ||
getOrmVersion: () => '9.5.0', | ||
}, | ||
integrator: {}, | ||
}, | ||
}); | ||
|
||
const articlesSerialized = await serializer.serialize([{ | ||
hasAddress: true, | ||
id: '1', | ||
name: 'foo', | ||
}]); | ||
|
||
const { attributes } = articlesSerialized.data[0]; | ||
expect(attributes).toContainAllKeys(['hasAddress', 'id', 'name']); | ||
expect(attributes).not.toContainKey('smart'); | ||
}); | ||
}); | ||
describe('when smart field is specified in query', () => { | ||
it('should compute smart field', async () => { | ||
Schemas.schemas = { users: usersSchema }; | ||
const modelMock = { name: 'users' }; | ||
const queryFields = { fields: { users: 'hasAddress,id,smart' } }; | ||
|
||
const serializer = new RecordSerializer(modelMock, null, queryFields, getMockedContext()); | ||
|
||
const articlesSerialized = await serializer.serialize([{ | ||
hasAddress: true, | ||
id: '1', | ||
}]); | ||
|
||
const { attributes } = articlesSerialized.data[0]; | ||
expect(attributes).toContainAllKeys(['hasAddress', 'smart', 'id']); | ||
expect(attributes).not.toContainKey('name'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |