|
1 |
| -import {TestBed, async} from '@angular/core/testing'; |
2 |
| -import {Component} from '@angular/core'; |
3 |
| -import {MdAutocompleteModule} from './index'; |
| 1 | +import {TestBed, async, ComponentFixture} from '@angular/core/testing'; |
| 2 | +import {Component, ViewChild} from '@angular/core'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | +import {MdAutocompleteModule, MdAutocompleteTrigger} from './index'; |
| 5 | +import {OverlayContainer} from '../core/overlay/overlay-container'; |
| 6 | +import {MdInputModule} from '../input/index'; |
4 | 7 |
|
5 | 8 | describe('MdAutocomplete', () => {
|
| 9 | + let overlayContainerElement: HTMLElement; |
6 | 10 |
|
7 | 11 | beforeEach(async(() => {
|
8 | 12 | TestBed.configureTestingModule({
|
9 |
| - imports: [MdAutocompleteModule.forRoot()], |
| 13 | + imports: [MdAutocompleteModule.forRoot(), MdInputModule.forRoot()], |
10 | 14 | declarations: [SimpleAutocomplete],
|
11 |
| - providers: [] |
| 15 | + providers: [ |
| 16 | + {provide: OverlayContainer, useFactory: () => { |
| 17 | + overlayContainerElement = document.createElement('div'); |
| 18 | + document.body.appendChild(overlayContainerElement); |
| 19 | + |
| 20 | + // remove body padding to keep consistent cross-browser |
| 21 | + document.body.style.padding = '0'; |
| 22 | + document.body.style.margin = '0'; |
| 23 | + |
| 24 | + return {getContainerElement: () => overlayContainerElement}; |
| 25 | + }}, |
| 26 | + ] |
12 | 27 | });
|
13 | 28 |
|
14 | 29 | TestBed.compileComponents();
|
15 | 30 | }));
|
16 | 31 |
|
17 |
| - it('should have a test', () => { |
18 |
| - expect(true).toBe(true); |
| 32 | + describe('panel toggling', () => { |
| 33 | + let fixture: ComponentFixture<SimpleAutocomplete>; |
| 34 | + let trigger: HTMLElement; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + fixture = TestBed.createComponent(SimpleAutocomplete); |
| 38 | + fixture.detectChanges(); |
| 39 | + |
| 40 | + trigger = fixture.debugElement.query(By.css('input')).nativeElement; |
| 41 | + }); |
| 42 | + |
| 43 | + it('should open the panel when the input is focused', () => { |
| 44 | + expect(fixture.componentInstance.trigger.panelOpen).toBe(false); |
| 45 | + dispatchEvent('focus', trigger); |
| 46 | + fixture.detectChanges(); |
| 47 | + |
| 48 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 49 | + .toBe(true, `Expected panel state to read open when input is focused.`); |
| 50 | + expect(overlayContainerElement.textContent) |
| 51 | + .toContain('Alabama', `Expected panel to display when input is focused.`); |
| 52 | + expect(overlayContainerElement.textContent) |
| 53 | + .toContain('California', `Expected panel to display when input is focused.`); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should open the panel programmatically', () => { |
| 57 | + expect(fixture.componentInstance.trigger.panelOpen).toBe(false); |
| 58 | + fixture.componentInstance.trigger.openPanel(); |
| 59 | + fixture.detectChanges(); |
| 60 | + |
| 61 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 62 | + .toBe(true, `Expected panel state to read open when opened programmatically.`); |
| 63 | + expect(overlayContainerElement.textContent) |
| 64 | + .toContain('Alabama', `Expected panel to display when opened programmatically.`); |
| 65 | + expect(overlayContainerElement.textContent) |
| 66 | + .toContain('California', `Expected panel to display when opened programmatically.`); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should close the panel when a click occurs outside it', async(() => { |
| 70 | + dispatchEvent('focus', trigger); |
| 71 | + fixture.detectChanges(); |
| 72 | + |
| 73 | + const backdrop = |
| 74 | + overlayContainerElement.querySelector('.cdk-overlay-backdrop') as HTMLElement; |
| 75 | + backdrop.click(); |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + fixture.whenStable().then(() => { |
| 79 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 80 | + .toBe(false, `Expected clicking outside the panel to set its state to closed.`); |
| 81 | + expect(overlayContainerElement.textContent) |
| 82 | + .toEqual('', `Expected clicking outside the panel to close the panel.`); |
| 83 | + }); |
| 84 | + })); |
| 85 | + |
| 86 | + it('should close the panel when an option is clicked', async(() => { |
| 87 | + dispatchEvent('focus', trigger); |
| 88 | + fixture.detectChanges(); |
| 89 | + |
| 90 | + const option = overlayContainerElement.querySelector('md-option') as HTMLElement; |
| 91 | + option.click(); |
| 92 | + fixture.detectChanges(); |
| 93 | + |
| 94 | + fixture.whenStable().then(() => { |
| 95 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 96 | + .toBe(false, `Expected clicking an option to set the panel state to closed.`); |
| 97 | + expect(overlayContainerElement.textContent) |
| 98 | + .toEqual('', `Expected clicking an option to close the panel.`); |
| 99 | + }); |
| 100 | + })); |
| 101 | + |
| 102 | + it('should close the panel when a newly created option is clicked', async(() => { |
| 103 | + fixture.componentInstance.states.unshift({code: 'TEST', name: 'test'}); |
| 104 | + fixture.detectChanges(); |
| 105 | + |
| 106 | + dispatchEvent('focus', trigger); |
| 107 | + fixture.detectChanges(); |
| 108 | + |
| 109 | + const option = overlayContainerElement.querySelector('md-option') as HTMLElement; |
| 110 | + option.click(); |
| 111 | + fixture.detectChanges(); |
| 112 | + |
| 113 | + fixture.whenStable().then(() => { |
| 114 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 115 | + .toBe(false, `Expected clicking a new option to set the panel state to closed.`); |
| 116 | + expect(overlayContainerElement.textContent) |
| 117 | + .toEqual('', `Expected clicking a new option to close the panel.`); |
| 118 | + }); |
| 119 | + })); |
| 120 | + |
| 121 | + it('should close the panel programmatically', async(() => { |
| 122 | + fixture.componentInstance.trigger.openPanel(); |
| 123 | + fixture.detectChanges(); |
| 124 | + |
| 125 | + fixture.componentInstance.trigger.closePanel(); |
| 126 | + fixture.detectChanges(); |
| 127 | + |
| 128 | + fixture.whenStable().then(() => { |
| 129 | + expect(fixture.componentInstance.trigger.panelOpen) |
| 130 | + .toBe(false, `Expected closing programmatically to set the panel state to closed.`); |
| 131 | + expect(overlayContainerElement.textContent) |
| 132 | + .toEqual('', `Expected closing programmatically to close the panel.`); |
| 133 | + }); |
| 134 | + })); |
| 135 | + |
19 | 136 | });
|
20 | 137 |
|
21 | 138 | });
|
22 | 139 |
|
23 | 140 | @Component({
|
24 | 141 | template: `
|
25 |
| - <md-autocomplete></md-autocomplete> |
| 142 | + <md-input-container> |
| 143 | + <input mdInput placeholder="State" [mdAutocomplete]="auto"> |
| 144 | + </md-input-container> |
| 145 | + |
| 146 | + <md-autocomplete #auto="mdAutocomplete"> |
| 147 | + <md-option *ngFor="let state of states" [value]="state.code"> {{ state.name }} </md-option> |
| 148 | + </md-autocomplete> |
26 | 149 | `
|
27 | 150 | })
|
28 |
| -class SimpleAutocomplete {} |
| 151 | +class SimpleAutocomplete { |
| 152 | + @ViewChild(MdAutocompleteTrigger) trigger: MdAutocompleteTrigger; |
| 153 | + |
| 154 | + states = [ |
| 155 | + {code: 'AL', name: 'Alabama'}, |
| 156 | + {code: 'CA', name: 'California'}, |
| 157 | + {code: 'FL', name: 'Florida'}, |
| 158 | + {code: 'KS', name: 'Kansas'}, |
| 159 | + {code: 'MA', name: 'Massachusetts'}, |
| 160 | + {code: 'NY', name: 'New York'}, |
| 161 | + {code: 'OR', name: 'Oregon'}, |
| 162 | + {code: 'PA', name: 'Pennsylvania'}, |
| 163 | + {code: 'TN', name: 'Tennessee'}, |
| 164 | + {code: 'VA', name: 'Virginia'}, |
| 165 | + {code: 'WY', name: 'Wyoming'}, |
| 166 | + ]; |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | +/** |
| 171 | + * TODO: Move this to core testing utility until Angular has event faking |
| 172 | + * support. |
| 173 | + * |
| 174 | + * Dispatches an event from an element. |
| 175 | + * @param eventName Name of the event |
| 176 | + * @param element The element from which the event will be dispatched. |
| 177 | + */ |
| 178 | +function dispatchEvent(eventName: string, element: HTMLElement): void { |
| 179 | + let event = document.createEvent('Event'); |
| 180 | + event.initEvent(eventName, true, true); |
| 181 | + element.dispatchEvent(event); |
| 182 | +} |
| 183 | + |
29 | 184 |
|
0 commit comments