-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add export data function * add unit tests * fix review
- Loading branch information
Showing
3 changed files
with
233 additions
and
15 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,130 @@ | ||
// Copyright LEAV Solutions 2017 | ||
// This file is released under LGPL V3 | ||
// License text available at https://www.gnu.org/licenses/lgpl-3.0.txt | ||
import {IAttributeDomain} from 'domain/attribute/attributeDomain'; | ||
import {IRecordDomain} from 'domain/record/recordDomain'; | ||
import {ToAny} from 'utils/utils'; | ||
import {IQueryInfos} from '_types/queryInfos'; | ||
import exportDomain, {IExportDomainDeps} from './exportDomain'; | ||
import {AttributeFormats} from '../../_types/attribute'; | ||
import {when} from 'jest-when'; | ||
|
||
const depsBase: ToAny<IExportDomainDeps> = { | ||
'core.domain.record': jest.fn(), | ||
'core.domain.attribute': jest.fn(), | ||
'core.domain.library': jest.fn(), | ||
'core.domain.tasksManager': jest.fn(), | ||
'core.domain.helpers.validate': jest.fn(), | ||
'core.domain.helpers.updateTaskProgress': jest.fn(), | ||
'core.domain.eventsManager': jest.fn(), | ||
'core.utils': jest.fn(), | ||
translator: {}, | ||
config: {} | ||
}; | ||
|
||
describe('exportDomain', () => { | ||
const mockCtx: IQueryInfos = { | ||
userId: '1', | ||
queryId: 'exportDomainTest' | ||
}; | ||
|
||
describe('Export data', () => { | ||
it('should export data with different attributes types', async () => { | ||
const jsonMapping = JSON.stringify({ | ||
simple: 'bikes.bikes_label', | ||
link: 'bikes.bikes_activity.activities_label', | ||
preview: 'bikes.bikes_visual.files_previews.medium', | ||
no_value: 'bikes.no_value', | ||
shop_label: 'shops.shops_label' | ||
}); | ||
|
||
const mockAttributeDomain: Mockify<IAttributeDomain> = { | ||
getAttributeProperties: jest.fn() | ||
}; | ||
|
||
const attributeProperties = { | ||
bikes_label: {format: AttributeFormats.TEXT}, | ||
bikes_activity: {linked_library: 'activities'}, | ||
activities_label: {format: AttributeFormats.TEXT}, | ||
bikes_visual: {linked_library: 'files'}, | ||
files_previews: {format: AttributeFormats.EXTENDED}, | ||
no_value: {format: AttributeFormats.TEXT, linked_library: false}, | ||
shops_label: {format: AttributeFormats.TEXT} | ||
}; | ||
|
||
Object.entries(attributeProperties).forEach(([id, returnValue]) => | ||
when(mockAttributeDomain.getAttributeProperties) | ||
.calledWith({id, ctx: mockCtx}) | ||
.mockReturnValue({id, ...returnValue}) | ||
); | ||
|
||
const mockRecordDomain: Mockify<IRecordDomain> = { | ||
getRecordFieldValue: jest.fn() | ||
}; | ||
|
||
const fieldValues = [ | ||
{ | ||
library: 'bikes', | ||
recordId: 'bikeId', | ||
attributeId: 'bikes_label', | ||
returnValue: [{payload: 'bikeLabel'}] | ||
}, | ||
{ | ||
library: 'bikes', | ||
recordId: 'bikeId', | ||
attributeId: 'bikes_activity', | ||
returnValue: [{payload: {id: 'activityId'}}] | ||
}, | ||
{ | ||
library: 'activities', | ||
recordId: 'activityId', | ||
attributeId: 'activities_label', | ||
returnValue: [{payload: 'activityLabel'}] | ||
}, | ||
{ | ||
library: 'bikes', | ||
recordId: 'bikeId', | ||
attributeId: 'bikes_visual', | ||
returnValue: [{payload: {id: 'fileId'}}] | ||
}, | ||
{ | ||
library: 'files', | ||
recordId: 'fileId', | ||
attributeId: 'files_previews', | ||
returnValue: [{payload: JSON.stringify({medium: '/path/to/preview'})}] | ||
}, | ||
{library: 'bikes', recordId: 'bikeId', attributeId: 'no_value', returnValue: []}, | ||
{ | ||
library: 'shops', | ||
recordId: 'shopId', | ||
attributeId: 'shops_label', | ||
returnValue: [{payload: 'shopLabel'}] | ||
} | ||
]; | ||
|
||
fieldValues.forEach(({library, recordId, attributeId, returnValue}) => | ||
when(mockRecordDomain.getRecordFieldValue) | ||
.calledWith({library, record: {id: recordId}, attributeId, ctx: mockCtx}) | ||
.mockReturnValue(returnValue) | ||
); | ||
|
||
const domain = exportDomain({ | ||
...depsBase, | ||
'core.domain.record': mockRecordDomain as IRecordDomain, | ||
'core.domain.attribute': mockAttributeDomain as IAttributeDomain | ||
}); | ||
|
||
const data = await domain.exportData(jsonMapping, [{bikes: 'bikeId', shops: 'shopId'}], mockCtx); | ||
|
||
expect(data).toEqual([ | ||
{ | ||
link: 'activityLabel', | ||
no_value: '', | ||
preview: '/path/to/preview', | ||
simple: 'bikeLabel', | ||
shop_label: 'shopLabel' | ||
} | ||
]); | ||
}); | ||
}); | ||
}); |
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