Skip to content

Commit db06f77

Browse files
committed
Tests
1 parent 323e75d commit db06f77

File tree

1 file changed

+138
-8
lines changed

1 file changed

+138
-8
lines changed

src/input-tags.spec.js

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,156 @@
1-
describe('input-tags', () => {
1+
describe('Module: angularjs-input-tags -', () => {
22
let $componentController;
33

44
beforeEach(angular.mock.module('angularjs-input-tags'));
55
beforeEach(angular.mock.inject(_$componentController_ => {
66
$componentController = _$componentController_;
77
}));
88

9-
describe('addTag', () => {
10-
it('should emit `onTagAdding` callback', () => {
11-
const onTagAddingSpy = jasmine.createSpy('onTagAdding');
12-
const bindings = {onTagAdding: onTagAddingSpy};
13-
const ctrl = $componentController('inputTags', undefined, bindings);
9+
describe('Component: inputTags -', () => {
10+
describe('Event: onTagAdding', () => {
11+
it('should be emit', () => {
12+
const onTagAddingSpy = jasmine.createSpy('onTagAdding');
13+
const bindings = {onTagAdding: onTagAddingSpy};
14+
const ctrl = $componentController('inputTags', undefined, bindings);
15+
ctrl.$onInit();
16+
ctrl.addTag({code: 1, text: '1'});
17+
expect(onTagAddingSpy).toHaveBeenCalled();
18+
});
19+
20+
it('should be emit with formatted tag value', () => {
21+
const onTagAddingSpy = jasmine.createSpy('onTagAdding');
22+
const bindings = {onTagAdding: onTagAddingSpy};
23+
const ctrl = $componentController('inputTags', undefined, bindings);
24+
ctrl.$onInit();
25+
ctrl.addTag({code: 1, text: '1'});
26+
expect(onTagAddingSpy).toHaveBeenCalledWith({code: 1, text: '1'});
27+
});
28+
});
29+
30+
it('should add tag on list', () => {
31+
const ctrl = $componentController('inputTags');
1432
ctrl.$onInit();
1533
ctrl.addTag({code: 1, text: '1'});
16-
expect(onTagAddingSpy).toHaveBeenCalledWith({code: 1, text: '1'});
34+
expect(ctrl.tags).toContain({code: 1, text: '1'});
1735
});
1836

1937
it('should update tag list', () => {
2038
const ctrl = $componentController('inputTags');
2139
ctrl.$onInit();
2240
ctrl.addTag({code: 1, text: '1'});
23-
expect(ctrl.tags).toContain({code: 1, text: '1'});
41+
ctrl.addTag({code: 1, text: '1'});
42+
ctrl.addTag({code: 1, text: '1'});
43+
expect(ctrl.tags.length).toBe(1);
44+
});
45+
46+
it('should ', () => {
47+
const ctrl = $componentController('inputTags');
48+
ctrl.$onInit();
49+
ctrl.triggerFocus();
50+
expect(ctrl.autocompleteVisible).toBe(true);
51+
});
52+
53+
it('should ', () => {
54+
const ctrl = $componentController('inputTags');
55+
ctrl.$onInit();
56+
ctrl.triggerBlur();
57+
expect(ctrl.autocompleteVisible).toBe(false);
58+
});
59+
60+
it('should ', () => {
61+
const ctrl = $componentController('inputTags');
62+
ctrl.$onInit();
63+
ctrl.getTagText({text: 'test'});
64+
expect(ctrl.autocompleteVisible).toBe(false);
65+
});
66+
67+
it('should ', () => {
68+
const ctrl = $componentController('inputTags');
69+
ctrl.$onInit();
70+
const result = ctrl.track({text: '1'});
71+
expect(result).toBe('1');
72+
});
73+
74+
it('should emit `onTagRemoving` event', () => {
75+
const onTagRemovingSpy = jasmine.createSpy('onTagRemoving');
76+
const bindings = {
77+
onTagRemoving: onTagRemovingSpy,
78+
tags: ['Demo']
79+
};
80+
const ctrl = $componentController('inputTags', undefined, bindings);
81+
ctrl.$onInit();
82+
ctrl.removeTag({code: 1, text: '1'});
83+
expect(onTagRemovingSpy).toHaveBeenCalled();
84+
expect(onTagRemovingSpy).toHaveBeenCalledWith({code: 1, text: '1'});
85+
});
86+
87+
it('should remove matching element by code', () => {
88+
const ctrl = $componentController('inputTags');
89+
ctrl.$onInit();
90+
ctrl.tags = [];
91+
ctrl.addTag({code: 1, text: '1'});
92+
ctrl.removeTag({code: 1});
93+
expect(ctrl.tags.length).toBe(0);
94+
});
95+
});
96+
97+
describe('Component: inputTagsAutoComplete', () => {
98+
it('should emit `onTagAdd` event', () => {
99+
const onTagAddSpy = jasmine.createSpy('onTagAdd');
100+
const bindings = {onTagAdd: onTagAddSpy};
101+
const ctrl = $componentController('inputTagsAutoComplete', undefined, bindings);
102+
ctrl.$onInit();
103+
ctrl.addTag({code: 1, text: '0'});
104+
expect(onTagAddSpy).toHaveBeenCalled();
105+
});
106+
107+
it('should not emit `onTagAdd` event', () => {
108+
const onTagAddSpy = jasmine.createSpy('onTagAdd');
109+
const bindings = {onTagAdd: onTagAddSpy};
110+
const ctrl = $componentController('inputTagsAutoComplete', undefined, bindings);
111+
ctrl.$onInit();
112+
ctrl.addTag();
113+
expect(onTagAddSpy).toHaveBeenCalledTimes(1);
114+
});
115+
116+
it('should emit `onTagAdd` event with formatted tag value', () => {
117+
const onTagAddSpy = jasmine.createSpy('onTagAdd');
118+
const bindings = {onTagAdd: onTagAddSpy};
119+
const ctrl = $componentController('inputTagsAutoComplete', undefined, bindings);
120+
ctrl.$onInit();
121+
ctrl.addTag({code: 1, text: '1'});
122+
expect(onTagAddSpy).toHaveBeenCalledWith({tag: {code: 1, text: '1'}});
123+
});
124+
125+
it('should reset the tag list', () => {
126+
const ctrl = $componentController('inputTagsAutoComplete');
127+
ctrl.$onInit();
128+
expect(ctrl.path.length).toBe(0);
129+
});
130+
131+
it('should go to the selected element in the tree', () => {
132+
const ctrl = $componentController('inputTagsAutoComplete');
133+
ctrl.$onInit();
134+
const pathLength = ctrl.path.length;
135+
ctrl.next('subLevel');
136+
expect(ctrl.path.length).toBe(pathLength + 1);
137+
});
138+
139+
it('should back to the previous element in the tree when on the root', () => {
140+
const ctrl = $componentController('inputTagsAutoComplete');
141+
ctrl.$onInit();
142+
ctrl.path = [];
143+
ctrl.previous();
144+
expect(ctrl.path.length).toBe(0);
145+
});
146+
147+
it('should back to the previous element in the tree when in 2 sublevels', () => {
148+
const ctrl = $componentController('inputTagsAutoComplete');
149+
ctrl.$onInit();
150+
ctrl.next('subLevel');
151+
ctrl.next('subSubLevel');
152+
ctrl.previous();
153+
expect(ctrl.path.length).toBe(1);
24154
});
25155
});
26156
});

0 commit comments

Comments
 (0)