salix/modules/client/front/address/index/index.spec.js

86 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-08-09 12:42:02 +00:00
import './index';
import crudModel from 'core/mocks/crud-model';
2018-08-09 12:42:02 +00:00
describe('Client', () => {
describe('Component vnClientAddressIndex', () => {
let controller;
let $scope;
let $stateParams;
2018-08-09 12:42:02 +00:00
let $httpBackend;
beforeEach(ngModule('client'));
2018-08-09 12:42:02 +00:00
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, $rootScope, _$stateParams_, _$httpBackend_) => {
$stateParams = _$stateParams_;
$stateParams.id = 1;
2018-08-09 12:42:02 +00:00
$httpBackend = _$httpBackend_;
$scope = $rootScope.$new();
2020-03-17 10:17:50 +00:00
const $element = angular.element('<vn-client-address-index></vn-client-address-index>');
controller = $componentController('vnClientAddressIndex', {$element, $scope});
controller.client = {id: 101, defaultAddressFk: 121};
2020-03-17 10:17:50 +00:00
controller.$.model = crudModel;
2018-08-09 12:42:02 +00:00
}));
describe('setDefault()', () => {
it('should perform a PATCH and set a value to defaultAddressFk property', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'sortAddresses');
let address = {id: 1};
let data = {defaultAddressFk: address.id};
let expectedResult = {defaultAddressFk: address.id};
2018-08-09 12:42:02 +00:00
$httpBackend.when('PATCH', `Clients/1`, data).respond(200, expectedResult);
$httpBackend.expect('PATCH', `Clients/1`, data);
2018-08-09 12:42:02 +00:00
controller.setDefault(address);
$httpBackend.flush();
expect(controller.client.defaultAddressFk).toEqual(1);
expect(controller.sortAddresses).toHaveBeenCalledWith();
});
});
describe('isDefaultAddress()', () => {
it('should return true if a passed address is the current default one', () => {
let address = {id: 121};
let result = controller.isDefaultAddress(address);
expect(result).toBeTruthy();
});
it('should return false if a passed address is the current default one', () => {
let address = {id: 1};
let result = controller.isDefaultAddress(address);
expect(result).toBeFalsy();
});
});
describe('sortAddresses()', () => {
it('should return an array of addresses sorted by client defaultAddressFk', () => {
controller.client.defaultAddressFk = 123;
controller.addresses = [
{id: 121, nickname: 'My address one'},
{id: 122, nickname: 'My address two'},
{id: 123, nickname: 'My address three'}];
controller.sortAddresses();
expect(controller.addresses[0].id).toEqual(123);
2018-08-09 12:42:02 +00:00
});
});
2020-06-12 08:53:33 +00:00
describe('exprBuilder()', () => {
it('should return a filter based on a search by id', () => {
const filter = controller.exprBuilder('search', '123');
expect(filter).toEqual({id: '123'});
});
it('should return a filter based on a search by name', () => {
const filter = controller.exprBuilder('search', 'Bruce Wayne');
expect(filter).toEqual({nickname: {like: '%Bruce Wayne%'}});
});
});
2018-08-09 12:42:02 +00:00
});
});