]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
ce376c93832178fefbb612902b80e56a388bd621
[ceph-ci.git] /
1 import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
2 import { DEBOUNCE_TIMER, DynamicInputComboboxDirective } from './dynamic-input-combobox.directive';
3 import { Subject } from 'rxjs';
4 import { Component, EventEmitter } from '@angular/core';
5 import { ComboBoxItem } from '../models/combo-box.model';
6
7 @Component({
8   template: `<div cdDynamicInputCombobox
9                   [items]="[]"></div>`,
10 })
11 class MockComponent {
12   items: ComboBoxItem[] = [{ content: 'Item1', name: 'Item1' }];
13   searchSubject = new Subject<string>();
14   selectedItems: ComboBoxItem[] = [];
15   updatedItems = new EventEmitter<ComboBoxItem[]>();
16 }
17
18 describe('DynamicInputComboboxDirective', () => {
19
20   let component: MockComponent;
21   let fixture: ComponentFixture<MockComponent>;
22   let directive: DynamicInputComboboxDirective;
23
24   beforeEach(() => {
25     TestBed.configureTestingModule({
26       declarations: [DynamicInputComboboxDirective, MockComponent],
27     }).compileComponents();
28
29     fixture = TestBed.createComponent(MockComponent);
30     component = fixture.componentInstance;
31
32     directive = fixture.debugElement.children[0].injector.get(DynamicInputComboboxDirective);
33     fixture.detectChanges();
34   });
35
36   afterEach(() => {
37     directive.ngOnDestroy();
38   });
39
40   it('should create an instance', () => {
41     expect(directive).toBeTruthy();
42   });
43
44   it('should update items when input is given', fakeAsync(() => {
45     const newItem = 'NewItem';
46     directive.onInput(newItem);
47     tick(DEBOUNCE_TIMER);
48
49     expect(directive.items[0].content).toBe(newItem);
50   }));
51
52   it('should not unselect selected items', fakeAsync(() => {
53     const selectedItems: ComboBoxItem[] = [{
54       content: 'selectedItem',
55       name: 'selectedItem',
56       selected: true
57     }];
58
59     directive.items = selectedItems;
60
61     directive.onSelected(selectedItems);
62     tick(DEBOUNCE_TIMER);
63
64     directive.onInput(selectedItems[0].content);
65     tick(DEBOUNCE_TIMER);
66
67     expect(directive.items[0].content).toBe(selectedItems[0].content);
68     expect(directive.items[0].selected).toBeTruthy();
69   }))
70 });