salix/modules/travel/front/thermograph/edit/index.spec.js

121 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-02-28 07:19:20 +00:00
import './index';
2020-02-28 13:18:55 +00:00
import watcher from 'core/mocks/watcher.js';
2020-02-28 07:19:20 +00:00
describe('Worker', () => {
2020-02-28 13:18:55 +00:00
describe('Component vnTravelThermographEdit', () => {
2020-02-28 07:19:20 +00:00
let controller;
let $scope;
let $httpBackend;
2020-02-28 13:18:55 +00:00
let $httpParamSerializer;
2020-02-28 07:19:20 +00:00
2020-02-28 13:18:55 +00:00
beforeEach(ngModule('travel'));
2020-02-28 07:19:20 +00:00
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
2020-02-28 07:19:20 +00:00
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
2020-02-28 13:18:55 +00:00
$httpParamSerializer = _$httpParamSerializer_;
2020-03-18 11:55:22 +00:00
const $element = angular.element(`<vn-travel-thermograph-edit></vn-travel-thermograph-edit`);
2020-02-28 13:18:55 +00:00
controller = $componentController('vnTravelThermographEdit', {$element, $scope});
controller._travel = {id: 3};
controller.$params = {id: 3, thermographId: 6};
controller.$.watcher = watcher;
2020-02-28 07:19:20 +00:00
}));
2020-02-28 13:18:55 +00:00
describe('travel() setter', () => {
it('should set the travel data and then call setDefaultParams() and getAllowedContentTypes()', () => {
2020-02-28 07:19:20 +00:00
jest.spyOn(controller, 'setDefaultParams');
jest.spyOn(controller, 'getAllowedContentTypes');
2020-02-28 13:18:55 +00:00
controller._travel = undefined;
controller.travel = {
id: 3
2020-02-28 07:19:20 +00:00
};
expect(controller.setDefaultParams).toHaveBeenCalledWith();
2020-02-28 13:18:55 +00:00
expect(controller.travel).toBeDefined();
2020-02-28 07:19:20 +00:00
expect(controller.getAllowedContentTypes).toHaveBeenCalledWith();
});
});
describe('setDefaultParams()', () => {
it('should perform a GET query and define the dms property on controller', () => {
2020-02-28 13:18:55 +00:00
const thermographId = 6;
2020-02-28 07:19:20 +00:00
const expectedResponse = {
2020-02-28 13:18:55 +00:00
thermographFk: 6,
result: 'Ok',
dms: {
reference: '123456-01',
warehouseFk: 1,
companyFk: 442,
dmsTypeFk: 3,
description: 'Test'
}
2020-02-28 07:19:20 +00:00
};
2020-02-28 13:18:55 +00:00
const filterObj = {include: {relation: 'dms'}};
const filter = encodeURIComponent(JSON.stringify(filterObj));
const query = `TravelThermographs/${thermographId}?filter=${filter}`;
$httpBackend.expect('GET', query).respond(expectedResponse);
2020-02-28 07:19:20 +00:00
controller.setDefaultParams();
$httpBackend.flush();
2020-02-28 13:18:55 +00:00
expect(controller.thermograph).toBeDefined();
expect(controller.thermograph.reference).toEqual('123456-01');
expect(controller.thermograph.dmsTypeId).toEqual(3);
expect(controller.thermograph.state).toEqual('Ok');
2020-02-28 07:19:20 +00:00
});
});
describe('onFileChange()', () => {
it('should set dms hasFileAttached property to true if has any files', () => {
const files = [{id: 1, name: 'MyFile'}];
2020-02-28 13:18:55 +00:00
controller.thermograph = {hasFileAttached: false};
2020-02-28 07:19:20 +00:00
controller.onFileChange(files);
$scope.$apply();
2020-02-28 13:18:55 +00:00
expect(controller.thermograph.hasFileAttached).toBeTruthy();
2020-02-28 07:19:20 +00:00
});
});
describe('getAllowedContentTypes()', () => {
it('should make an HTTP GET request to get the allowed content types', () => {
const expectedResponse = ['image/png', 'image/jpg'];
$httpBackend.expect('GET', `TravelThermographs/allowedContentTypes`).respond(expectedResponse);
2020-02-28 07:19:20 +00:00
controller.getAllowedContentTypes();
$httpBackend.flush();
expect(controller.allowedContentTypes).toBeDefined();
expect(controller.allowedContentTypes).toEqual('image/png, image/jpg');
});
});
2020-02-28 13:18:55 +00:00
describe('contentTypesInfo()', () => {
it('should return a description with a list of allowed content types', () => {
controller.allowedContentTypes = ['image/png', 'image/jpg'];
const expectedTypes = controller.allowedContentTypes.join(', ');
const expectedResult = `Allowed content types: ${expectedTypes}`;
jest.spyOn(controller.$translate, 'instant').mockReturnValue(expectedResult);
const result = controller.contentTypesInfo;
expect(result).toEqual(expectedResult);
});
});
describe('onSubmit()', () => {
it('should make an HTTP POST request to save the form data', () => {
jest.spyOn(controller.$.watcher, 'updateOriginalData');
const files = [{id: 1, name: 'MyFile'}];
controller.thermograph = {files};
const serializedParams = $httpParamSerializer(controller.thermograph);
const query = `travels/${controller.$params.id}/updateThermograph?${serializedParams}`;
$httpBackend.expect('POST', query).respond({});
controller.onSubmit();
$httpBackend.flush();
});
});
2020-02-28 07:19:20 +00:00
});
});