salix/client/core/src/components/autocomplete/autocomplete.spec.js

75 lines
2.5 KiB
JavaScript
Raw Normal View History

import './autocomplete.js';
2018-03-09 13:15:30 +00:00
import template from './autocomplete.html';
describe('Component vnAutocomplete', () => {
2018-03-09 13:15:30 +00:00
let $element;
let $scope;
let $httpBackend;
let controller;
2018-03-09 13:15:30 +00:00
let data = {id: 1, name: 'Bruce Wayne'};
beforeEach(() => {
angular.mock.module('client');
});
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$timeout_) => {
$scope = $rootScope.$new();
2018-03-09 13:15:30 +00:00
$element = angular.element(`<div>${template}</div>`);
$httpBackend = _$httpBackend_;
2018-01-30 13:48:21 +00:00
$httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({});
2018-03-09 13:15:30 +00:00
controller = _$componentController_('vnAutocomplete', {$element, $scope, $httpBackend, $transclude: null});
}));
2018-03-09 13:15:30 +00:00
describe('field() setter/getter', () => {
it(`should set field controllers property`, () => {
controller.field = data.id;
2018-03-09 13:15:30 +00:00
expect(controller.field).toEqual(data.id);
});
2018-03-09 13:15:30 +00:00
it(`should set selection finding an existing item in the initialData property`, () => {
controller.valueField = 'id';
controller.showField = 'name';
controller.initialData = data;
controller.field = data.id;
2018-03-09 13:15:30 +00:00
expect(controller.selection).toEqual(data);
});
2018-03-09 13:15:30 +00:00
it(`should set selection finding an existing item in the data property`, () => {
controller.valueField = 'id';
controller.showField = 'name';
controller.data = [data];
controller.field = data.id;
2018-03-09 13:15:30 +00:00
expect(controller.selection).toEqual(data);
});
2018-03-09 13:15:30 +00:00
it(`should set selection to null when can't find an existing item in the data property`, () => {
controller.valueField = 'id';
controller.showField = 'name';
controller.field = data.id;
2018-03-09 13:15:30 +00:00
expect(controller.selection).toEqual(null);
});
2018-03-09 13:15:30 +00:00
it(`should perform a query if the item id isn't present in the data property`, () => {
controller.valueField = 'id';
controller.showField = 'name';
controller.url = 'localhost';
controller.field = data.id;
2018-03-09 13:15:30 +00:00
let filter = {
fields: ['id', 'name'],
where: {id: data.id}
};
let json = encodeURIComponent(JSON.stringify(filter));
2017-09-27 06:09:10 +00:00
2018-03-09 13:15:30 +00:00
$httpBackend.expectGET(`localhost?filter=${json}`);
controller.field = data.id;
$httpBackend.flush();
});
});
});