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