2017-09-26 08:26:21 +00:00
|
|
|
import './autocomplete.js';
|
2018-03-09 13:15:30 +00:00
|
|
|
import template from './autocomplete.html';
|
2017-09-26 08:26:21 +00:00
|
|
|
|
|
|
|
describe('Component vnAutocomplete', () => {
|
2018-03-09 13:15:30 +00:00
|
|
|
let $element;
|
2017-09-26 08:26:21 +00:00
|
|
|
let $scope;
|
|
|
|
let $httpBackend;
|
2017-10-17 12:24:40 +00:00
|
|
|
let controller;
|
2017-09-26 08:26:21 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
let data = {id: 1, name: 'Bruce Wayne'};
|
|
|
|
|
2017-09-26 08:26:21 +00:00
|
|
|
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>`);
|
2017-09-26 08:26:21 +00:00
|
|
|
$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});
|
2017-09-26 08:26:21 +00:00
|
|
|
}));
|
|
|
|
|
2018-07-02 11:13:26 +00:00
|
|
|
describe('url() setter', () => {
|
|
|
|
it(`should set url controllers property and call refreshSelection`, () => {
|
|
|
|
spyOn(controller, "refreshSelection");
|
|
|
|
controller.url = "url";
|
|
|
|
|
|
|
|
expect(controller.url).toEqual("url");
|
|
|
|
expect(controller.refreshSelection).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
describe('field() setter/getter', () => {
|
|
|
|
it(`should set field controllers property`, () => {
|
|
|
|
controller.field = data.id;
|
2017-09-26 08:26:21 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
expect(controller.field).toEqual(data.id);
|
2017-09-26 08:26:21 +00:00
|
|
|
});
|
2017-09-26 12:10:55 +00:00
|
|
|
|
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;
|
2017-09-26 12:10:55 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
expect(controller.selection).toEqual(data);
|
2017-09-26 12:10:55 +00:00
|
|
|
});
|
|
|
|
|
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;
|
2017-09-26 12:10:55 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
expect(controller.selection).toEqual(data);
|
2017-09-26 12:10:55 +00:00
|
|
|
});
|
|
|
|
|
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;
|
2017-09-26 12:10:55 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
expect(controller.selection).toEqual(null);
|
2017-09-26 12:10:55 +00:00
|
|
|
});
|
2017-09-27 12:10:03 +00:00
|
|
|
|
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;
|
2017-09-27 12:10:03 +00:00
|
|
|
|
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-05-25 15:25:35 +00:00
|
|
|
$httpBackend.whenGET(`localhost?filter=${json}`).respond({});
|
2018-03-09 13:15:30 +00:00
|
|
|
$httpBackend.expectGET(`localhost?filter=${json}`);
|
|
|
|
controller.field = data.id;
|
2017-09-28 10:37:45 +00:00
|
|
|
$httpBackend.flush();
|
2017-10-03 09:32:25 +00:00
|
|
|
});
|
2017-09-28 10:37:45 +00:00
|
|
|
});
|
2017-09-26 08:26:21 +00:00
|
|
|
});
|