import './autocomplete.js'; import template from './autocomplete.html'; describe('Component vnAutocomplete', () => { let $element; let $scope; let $httpBackend; let controller; let data = {id: 1, name: 'Bruce Wayne'}; beforeEach(() => { angular.mock.module('client'); }); beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$timeout_) => { $scope = $rootScope.$new(); $element = angular.element(`
${template}
`); $httpBackend = _$httpBackend_; $httpBackend.when('GET', /\/locale\/\w+\/[a-z]{2}\.json/).respond({}); controller = _$componentController_('vnAutocomplete', {$element, $scope, $httpBackend, $transclude: null}); })); describe('field() setter/getter', () => { it(`should set field controllers property`, () => { controller.field = data.id; expect(controller.field).toEqual(data.id); }); 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; expect(controller.selection).toEqual(data); }); 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; expect(controller.selection).toEqual(data); }); 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; expect(controller.selection).toEqual(null); }); 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; let filter = { fields: ['id', 'name'], where: {id: data.id} }; let json = encodeURIComponent(JSON.stringify(filter)); $httpBackend.expectGET(`localhost?filter=${json}`); controller.field = data.id; $httpBackend.flush(); }); }); });