const axios = require('axios'); const models = require('vn-loopback/server/server').models; describe('Docuware core', () => { const fileCabinetCode = 'deliveryNote'; beforeAll(async() => { process.env.NODE_ENV = 'testing'; const docuwareInfo = await models.Docuware.findOne({ where: { code: fileCabinetCode } }); spyOn(axios, 'get').and.callFake(url => { if (url.includes('IdentityServiceInfo')) return {data: {IdentityServiceUrl: 'IdentityServiceUrl'}}; if (url.includes('IdentityServiceUrl')) return {data: {token_endpoint: 'token_endpoint'}}; if (url.includes('dialogs')) { return { data: { Dialog: [ { DisplayName: 'find', Id: 'getDialogTest' } ] } }; } if (url.includes('FileCabinets')) { return {data: { FileCabinet: [ { Name: docuwareInfo.fileCabinetName, Id: 'getFileCabinetTest' } ] }}; } }); spyOn(axios, 'post').and.callFake(url => { if (url.includes('token_endpoint')) { return {data: { access_token: 'access_token', token_type: 'bearer', expires_in: 10000 }}; } if (url.includes('DialogExpression')) { return {data: { Items: [{ Fields: [ { ItemElementName: 'integer', FieldLabel: 'firstRequiredField', Item: 1 }, { ItemElementName: 'string', FieldLabel: 'secondRequiredField', Item: 'myName' }, { ItemElementName: 'integer', FieldLabel: 'notRequiredField', Item: 2 } ] }] } }; } }); }); afterAll(() => { delete process.env.NODE_ENV; }); describe('getOptions()', () => { it('should return url and headers', async() => { const result = await models.Docuware.getOptions(); expect(result.url).toBeDefined(); expect(result.headers).toBeDefined(); }); }); describe('Dialog()', () => { it('should return dialogId', async() => { const result = await models.Docuware.getDialog('deliveryNote', 'find', 'randomFileCabinetId'); expect(result).toEqual('getDialogTest'); }); }); describe('getFileCabinet()', () => { it('should return fileCabinetId', async() => { const result = await models.Docuware.getFileCabinet(fileCabinetCode); expect(result).toEqual('getFileCabinetTest'); }); }); describe('get()', () => { it('should return data without parse', async() => { const [result] = await models.Docuware.get('deliveryNote'); expect(result.firstRequiredField).toEqual(1); }); it('should return data with parse', async() => { const parse = { 'firstRequiredField': 'id', 'secondRequiredField': 'name', }; const [result] = await models.Docuware.get('deliveryNote', null, parse); expect(result.id).toEqual(1); expect(result.name).toEqual('myName'); expect(result.notRequiredField).not.toBeDefined(); }); }); describe('getById()', () => { it('should return data', async() => { spyOn(models.Docuware, 'get'); await models.Docuware.getById('deliveryNote', 1); expect(models.Docuware.get).toHaveBeenCalledWith( 'deliveryNote', {condition: [Object({DBName: 'N__ALBAR_N', Value: [1]})]}, undefined ); }); }); });