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

122 lines
5.2 KiB
JavaScript

import './index';
import watcher from 'core/mocks/watcher.js';
describe('Worker', () => {
describe('Component vnTravelThermographEdit', () => {
let controller;
let $scope;
let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('travel'));
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$scope = $rootScope.$new();
$httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
const $element = angular.element(`<vn-travel-thermograph-edit></vn-travel-thermograph-edit`);
controller = $componentController('vnTravelThermographEdit', {$element, $scope});
controller._travel = {id: 3};
controller.$params = {id: 3, thermographId: 6};
controller.$.watcher = watcher;
}));
describe('travel() setter', () => {
it('should set the travel data and then call setDefaultParams() and getAllowedContentTypes()', () => {
jest.spyOn(controller, 'setDefaultParams');
jest.spyOn(controller, 'getAllowedContentTypes');
controller._travel = undefined;
controller.travel = {
id: 3
};
expect(controller.setDefaultParams).toHaveBeenCalledWith();
expect(controller.travel).toBeDefined();
expect(controller.getAllowedContentTypes).toHaveBeenCalledWith();
});
});
describe('setDefaultParams()', () => {
it('should perform a GET query and define the dms property on controller', () => {
const thermographId = 6;
const expectedResponse = {
thermographFk: 6,
result: 'Ok',
dms: {
reference: '123456-01',
warehouseFk: 1,
companyFk: 442,
dmsTypeFk: 3,
description: 'Test'
}
};
const filterObj = {include: {relation: 'dms'}};
const filter = encodeURIComponent(JSON.stringify(filterObj));
const query = `TravelThermographs/${thermographId}?filter=${filter}`;
$httpBackend.expect('GET', query).respond(expectedResponse);
controller.setDefaultParams();
$httpBackend.flush();
expect(controller.thermograph).toBeDefined();
expect(controller.thermograph.reference).toEqual('123456-01');
expect(controller.thermograph.dmsTypeId).toEqual(3);
expect(controller.thermograph.state).toEqual('Ok');
});
});
describe('onFileChange()', () => {
it('should set dms hasFileAttached property to true if has any files', () => {
const files = [{id: 1, name: 'MyFile'}];
controller.thermograph = {hasFileAttached: false};
controller.onFileChange(files);
$scope.$apply();
expect(controller.thermograph.hasFileAttached).toBeTruthy();
});
});
describe('getAllowedContentTypes()', () => {
it('should make an HTTP GET request to get the allowed content types', () => {
const expectedResponse = ['image/png', 'image/jpg'];
$httpBackend.when('GET', `TravelThermographs/allowedContentTypes`).respond(expectedResponse);
$httpBackend.expect('GET', `TravelThermographs/allowedContentTypes`);
controller.getAllowedContentTypes();
$httpBackend.flush();
expect(controller.allowedContentTypes).toBeDefined();
expect(controller.allowedContentTypes).toEqual('image/png, image/jpg');
});
});
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();
});
});
});
});