|
| 1 | +import {async, TestBed, ComponentFixture} from '@angular/core/testing'; |
| 2 | +import {MdChipsModule} from './index'; |
| 3 | +import {Component, DebugElement} from '@angular/core'; |
| 4 | +import {MdChipInput, MdChipInputEvent} from './chip-input'; |
| 5 | +import {By} from '@angular/platform-browser'; |
| 6 | +import {Dir} from '../core/rtl/dir'; |
| 7 | +import {FakeKeyboardEvent} from './chip-list.spec'; |
| 8 | +import {ENTER, COMMA} from '../core/keyboard/keycodes'; |
| 9 | + |
| 10 | +describe('MdChipInput', () => { |
| 11 | + let fixture: ComponentFixture<any>; |
| 12 | + let testChipInput: TestChipInput; |
| 13 | + let inputDebugElement: DebugElement; |
| 14 | + let inputNativeElement: HTMLElement; |
| 15 | + let chipInputDirective: MdChipInput; |
| 16 | + |
| 17 | + let dir = 'ltr'; |
| 18 | + |
| 19 | + beforeEach(async(() => { |
| 20 | + TestBed.configureTestingModule({ |
| 21 | + imports: [MdChipsModule], |
| 22 | + declarations: [TestChipInput], |
| 23 | + providers: [{ |
| 24 | + provide: Dir, useFactory: () => { |
| 25 | + return {value: dir.toLowerCase()}; |
| 26 | + } |
| 27 | + }] |
| 28 | + }); |
| 29 | + |
| 30 | + TestBed.compileComponents(); |
| 31 | + })); |
| 32 | + |
| 33 | + beforeEach(async(() => { |
| 34 | + fixture = TestBed.createComponent(TestChipInput); |
| 35 | + testChipInput = fixture.debugElement.componentInstance; |
| 36 | + fixture.detectChanges(); |
| 37 | + |
| 38 | + inputDebugElement = fixture.debugElement.query(By.directive(MdChipInput)); |
| 39 | + chipInputDirective = inputDebugElement.injector.get(MdChipInput) as MdChipInput; |
| 40 | + inputNativeElement = inputDebugElement.nativeElement; |
| 41 | + })); |
| 42 | + |
| 43 | + describe('basic behavior', () => { |
| 44 | + it('emits the (chipAdded) on enter keyup', () => { |
| 45 | + let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any; |
| 46 | + |
| 47 | + spyOn(testChipInput, 'add'); |
| 48 | + |
| 49 | + chipInputDirective._keydown(ENTER_EVENT); |
| 50 | + expect(testChipInput.add).toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('[addOnBlur]', () => { |
| 55 | + it('allows (chipAdded) when true', () => { |
| 56 | + spyOn(testChipInput, 'add'); |
| 57 | + |
| 58 | + testChipInput.addOnBlur = true; |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + chipInputDirective._blur(); |
| 62 | + expect(testChipInput.add).toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('disallows (chipAdded) when false', () => { |
| 66 | + spyOn(testChipInput, 'add'); |
| 67 | + |
| 68 | + testChipInput.addOnBlur = false; |
| 69 | + fixture.detectChanges(); |
| 70 | + |
| 71 | + chipInputDirective._blur(); |
| 72 | + expect(testChipInput.add).not.toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('[separatorKeys]', () => { |
| 77 | + it('does not emit (chipAdded) when a non-separator key is pressed', () => { |
| 78 | + let ENTER_EVENT = new FakeKeyboardEvent(ENTER, inputNativeElement) as any; |
| 79 | + spyOn(testChipInput, 'add'); |
| 80 | + |
| 81 | + testChipInput.separatorKeys = [COMMA]; |
| 82 | + fixture.detectChanges(); |
| 83 | + |
| 84 | + chipInputDirective._keydown(ENTER_EVENT); |
| 85 | + expect(testChipInput.add).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('emits (chipAdded) when a custom separator keys is pressed', () => { |
| 89 | + let COMMA_EVENT = new FakeKeyboardEvent(COMMA, inputNativeElement) as any; |
| 90 | + spyOn(testChipInput, 'add'); |
| 91 | + |
| 92 | + testChipInput.separatorKeys = [COMMA]; |
| 93 | + fixture.detectChanges(); |
| 94 | + |
| 95 | + chipInputDirective._keydown(COMMA_EVENT); |
| 96 | + expect(testChipInput.add).toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +@Component({ |
| 102 | + template: ` |
| 103 | + <md-chip-list> |
| 104 | + <input mdChipInput [addOnBlur]="addOnBlur" [separatorKeys]="separatorKeys" |
| 105 | + (chipAdded)="add($event)" /> |
| 106 | + </md-chip-list> |
| 107 | + ` |
| 108 | +}) |
| 109 | +class TestChipInput { |
| 110 | + addOnBlur: boolean = false; |
| 111 | + separatorKeys: number[] = [ENTER]; |
| 112 | + |
| 113 | + add(event: MdChipInputEvent) { |
| 114 | + } |
| 115 | +} |
0 commit comments