|
1 |
| -import { Component, ViewChild } from '@angular/core'; |
| 1 | +import { Component, ViewChild, ViewChildren, QueryList } from '@angular/core'; |
2 | 2 | import {
|
3 | 3 | async,
|
4 | 4 | TestBed
|
5 | 5 | } from '@angular/core/testing';
|
6 | 6 | import { FormsModule } from '@angular/forms';
|
7 | 7 | import { By } from '@angular/platform-browser';
|
| 8 | +import { IgxIconModule } from '../icon/index'; |
| 9 | +import { IgxChipsModule } from './chips.module'; |
| 10 | +import { IgxChipComponent } from './chip.component'; |
8 | 11 | import { IgxChipsAreaComponent } from './chips-area.component';
|
9 | 12 |
|
| 13 | +@Component({ |
| 14 | + template: ` |
| 15 | + <igx-chips-area #chipsArea> |
| 16 | + <igx-chip #chipElem *ngFor="let chip of chipList" |
| 17 | + [id]="chip.id" [draggable]="chip.draggable" [removable]="chip.removable" [selectable]="chip.selectable"> |
| 18 | + <igx-icon igxPrefix fontSet="material" [name]="'drag_indicator'"></igx-icon> |
| 19 | + <span #label [class]="'igx-chip__text'">{{chip.text}}</span> |
| 20 | + <igx-icon class="igx-chip__dir-icon" igxConnector fontSet="material" [name]="'forward'"></igx-icon> |
| 21 | + </igx-chip> |
| 22 | + </igx-chips-area> |
| 23 | + ` |
| 24 | +}) |
| 25 | +export class TestChipComponent { |
| 26 | + |
| 27 | + public chipList = [ |
| 28 | + { id: 'Country', text: 'Country', removable: false, selectable: false, draggable: true }, |
| 29 | + { id: 'City', text: 'City', removable: true, selectable: true, draggable: true } |
| 30 | + ]; |
| 31 | + |
| 32 | + @ViewChild('chipsArea', { read: IgxChipsAreaComponent}) |
| 33 | + public chipsArea: IgxChipsAreaComponent; |
| 34 | + |
| 35 | + @ViewChildren('chipElem', { read: IgxChipComponent}) |
| 36 | + public chips: QueryList<IgxChipComponent>; |
| 37 | +} |
| 38 | + |
10 | 39 | describe('IgxChipsArea', () => {
|
| 40 | + const CHIP_ITEM_AREA = 'igx-chip__item chip-area'; |
| 41 | + const CHIP_CONNECTOR = 'igx-chip__connecto'; |
| 42 | + |
11 | 43 | beforeEach(async(() => {
|
12 | 44 | TestBed.configureTestingModule({
|
13 | 45 | declarations: [
|
| 46 | + TestChipComponent |
14 | 47 | ],
|
15 |
| - imports: [] |
| 48 | + imports: [FormsModule, IgxIconModule, IgxChipsModule] |
16 | 49 | }).compileComponents();
|
17 | 50 | }));
|
| 51 | + |
| 52 | + it('should add chips when adding data items ', () => { |
| 53 | + const fix = TestBed.createComponent(TestChipComponent); |
| 54 | + fix.detectChanges(); |
| 55 | + |
| 56 | + const chipArea = fix.debugElement.queryAll(By.directive(IgxChipsAreaComponent)); |
| 57 | + const chipAreaComponent = fix.componentInstance; |
| 58 | + expect(chipArea[0].nativeElement.children.length).toEqual(2); |
| 59 | + |
| 60 | + chipAreaComponent.chipList.push({ id: 'Town', text: 'Town', removable: true, selectable: true, draggable: true }); |
| 61 | + |
| 62 | + fix.detectChanges(); |
| 63 | + |
| 64 | + expect(chipArea[0].nativeElement.children.length).toEqual(3); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should remove chips when removing data items ', () => { |
| 68 | + const fix = TestBed.createComponent(TestChipComponent); |
| 69 | + fix.detectChanges(); |
| 70 | + |
| 71 | + const chipArea = fix.debugElement.queryAll(By.directive(IgxChipsAreaComponent)); |
| 72 | + const chipAreaComponent = fix.componentInstance; |
| 73 | + expect(chipArea[0].nativeElement.children.length).toEqual(2); |
| 74 | + |
| 75 | + chipAreaComponent.chipList.pop(); |
| 76 | + fix.detectChanges(); |
| 77 | + |
| 78 | + expect(chipArea[0].nativeElement.children.length).toEqual(1); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should change data in chips when data item is changed', () => { |
| 82 | + const fix = TestBed.createComponent(TestChipComponent); |
| 83 | + fix.detectChanges(); |
| 84 | + |
| 85 | + const chipArea = fix.debugElement.queryAll(By.directive(IgxChipsAreaComponent)); |
| 86 | + const chipAreaComponent = fix.componentInstance; |
| 87 | + |
| 88 | + expect(chipArea[0].nativeElement.children[0].innerHTML).toContain('Country'); |
| 89 | + |
| 90 | + chipAreaComponent.chipList[0].text = 'New text'; |
| 91 | + fix.detectChanges(); |
| 92 | + |
| 93 | + expect(chipArea[0].nativeElement.children[0].innerHTML).toContain('New text'); |
| 94 | + }); |
18 | 95 | });
|
0 commit comments