1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2 import { ReactiveFormsModule, Validators } from '@angular/forms';
4 import { PopoverModule } from 'ngx-bootstrap/popover';
5 import { TooltipModule } from 'ngx-bootstrap/tooltip';
7 import { configureTestBed, i18nProviders } from '../../../../testing/unit-test-helper';
8 import { SelectOption } from './select-option.model';
9 import { SelectComponent } from './select.component';
11 describe('SelectComponent', () => {
12 let component: SelectComponent;
13 let fixture: ComponentFixture<SelectComponent>;
15 const selectOption = (filter: string) => {
16 component.filter.setValue(filter);
17 component.updateFilter();
18 component.selectOption();
22 declarations: [SelectComponent],
23 imports: [PopoverModule.forRoot(), TooltipModule, ReactiveFormsModule],
24 providers: i18nProviders
28 fixture = TestBed.createComponent(SelectComponent);
29 component = fixture.componentInstance;
30 fixture.detectChanges();
32 { name: 'option1', description: '', selected: false, enabled: true },
33 { name: 'option2', description: '', selected: false, enabled: true },
34 { name: 'option3', description: '', selected: false, enabled: true }
38 it('should create', () => {
39 expect(component).toBeTruthy();
42 it('should add item', () => {
44 component.triggerSelection(component.options[1]);
45 expect(component.data).toEqual(['option2']);
48 it('should update selected', () => {
49 component.data = ['option2'];
50 component.ngOnChanges();
51 expect(component.options[0].selected).toBe(false);
52 expect(component.options[1].selected).toBe(true);
55 it('should remove item', () => {
56 component.options.map((option) => {
57 option.selected = true;
60 component.data = ['option1', 'option2', 'option3'];
61 component.removeItem('option1');
62 expect(component.data).toEqual(['option2', 'option3']);
65 it('should not remove item that is not selected', () => {
66 component.options[0].selected = true;
67 component.data = ['option1'];
68 component.removeItem('option2');
69 expect(component.data).toEqual(['option1']);
72 describe('filter values', () => {
77 it('shows all options with no value set', () => {
78 expect(component.filteredOptions).toEqual(component.options);
81 it('shows one option that it filtered for', () => {
82 component.filter.setValue('2');
83 component.updateFilter();
84 expect(component.filteredOptions).toEqual([component.options[1]]);
87 it('shows all options after selecting something', () => {
88 component.filter.setValue('2');
89 component.updateFilter();
90 component.selectOption();
91 expect(component.filteredOptions).toEqual(component.options);
94 it('is not able to create by default with no value set', () => {
95 component.updateFilter();
96 expect(component.isCreatable()).toBeFalsy();
99 it('is not able to create by default with a value set', () => {
100 component.filter.setValue('2');
101 component.updateFilter();
102 expect(component.isCreatable()).toBeFalsy();
106 describe('automatically add selected options if not in options array', () => {
108 component.data = ['option1', 'option4'];
109 expect(component.options.length).toBe(3);
112 const expectedResult = () => {
113 expect(component.options.length).toBe(4);
114 expect(component.options[3]).toEqual(new SelectOption(true, 'option4', ''));
117 it('with no extra settings', () => {
118 component.ngOnInit();
122 it('with custom badges', () => {
123 component.customBadges = true;
124 component.ngOnInit();
128 it('with limit higher than selected', () => {
129 component.selectionLimit = 3;
130 component.ngOnInit();
134 it('with limit equal to selected', () => {
135 component.selectionLimit = 2;
136 component.ngOnInit();
140 it('with limit lower than selected', () => {
141 component.selectionLimit = 1;
142 component.ngOnInit();
147 describe('sorted array and options', () => {
149 component.customBadges = true;
150 component.customBadgeValidators = [Validators.pattern('[A-Za-z0-9_]+')];
151 component.data = ['c', 'b'];
152 component.options = [new SelectOption(true, 'd', ''), new SelectOption(true, 'a', '')];
153 component.ngOnInit();
156 it('has a sorted selection', () => {
157 expect(component.data).toEqual(['a', 'b', 'c', 'd']);
160 it('has a sorted options', () => {
161 const sortedOptions = [
162 new SelectOption(true, 'a', ''),
163 new SelectOption(true, 'b', ''),
164 new SelectOption(true, 'c', ''),
165 new SelectOption(true, 'd', '')
167 expect(component.options).toEqual(sortedOptions);
170 it('has a sorted selection after adding an item', () => {
171 selectOption('block');
172 expect(component.data).toEqual(['a', 'b', 'block', 'c', 'd']);
175 it('has a sorted options after adding an item', () => {
176 selectOption('block');
177 const sortedOptions = [
178 new SelectOption(true, 'a', ''),
179 new SelectOption(true, 'b', ''),
180 new SelectOption(true, 'block', ''),
181 new SelectOption(true, 'c', ''),
182 new SelectOption(true, 'd', '')
184 expect(component.options).toEqual(sortedOptions);
188 describe('with custom options', () => {
190 component.customBadges = true;
191 component.customBadgeValidators = [Validators.pattern('[A-Za-z0-9_]+')];
192 component.ngOnInit();
195 it('is not able to create with no value set', () => {
196 component.updateFilter();
197 expect(component.isCreatable()).toBeFalsy();
200 it('is able to create with a valid value set', () => {
201 component.filter.setValue('2');
202 component.updateFilter();
203 expect(component.isCreatable()).toBeTruthy();
206 it('is not able to create with a value set that already exist', () => {
207 component.filter.setValue('option2');
208 component.updateFilter();
209 expect(component.isCreatable()).toBeFalsy();
212 it('adds custom option', () => {
213 selectOption('customOption');
214 expect(component.options[0]).toEqual({
215 name: 'customOption',
220 expect(component.options.length).toBe(4);
221 expect(component.data).toEqual(['customOption']);
224 it('will not add an option that did not pass the validation', () => {
225 selectOption(' this does not pass ');
226 expect(component.options.length).toBe(3);
227 expect(component.data).toEqual([]);
228 expect(component.filter.invalid).toBeTruthy();
231 it('removes custom item selection by name', () => {
232 selectOption('customOption');
233 component.removeItem('customOption');
234 expect(component.data).toEqual([]);
235 expect(component.options.length).toBe(4);
236 expect(component.options[0]).toEqual({
237 name: 'customOption',
244 it('will not add an option that is already there', () => {
245 selectOption('option2');
246 expect(component.options.length).toBe(3);
247 expect(component.data).toEqual(['option2']);
250 it('will not add an option twice after each other', () => {
251 selectOption('onlyOnce');
252 expect(component.data).toEqual(['onlyOnce']);
253 selectOption('onlyOnce');
254 expect(component.data).toEqual([]);
255 selectOption('onlyOnce');
256 expect(component.data).toEqual(['onlyOnce']);
257 expect(component.options.length).toBe(4);
261 describe('if the selection limit is reached', function() {
263 component.selectionLimit = 2;
264 component.triggerSelection(component.options[0]);
265 component.triggerSelection(component.options[1]);
268 it('will not select more options', () => {
269 component.triggerSelection(component.options[2]);
270 expect(component.data).toEqual(['option1', 'option2']);
273 it('will unselect options that are selected', () => {
274 component.triggerSelection(component.options[1]);
275 expect(component.data).toEqual(['option1']);