|
| 1 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 2 | +import {Component, DebugElement} from '@angular/core'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | +import {dispatchFakeEvent} from '@angular/cdk/testing'; |
| 5 | +import {MdOption, MdOptionModule} from './index'; |
| 6 | + |
| 7 | +describe('MdOption component', () => { |
| 8 | + |
| 9 | + beforeEach(async(() => { |
| 10 | + TestBed.configureTestingModule({ |
| 11 | + imports: [MdOptionModule], |
| 12 | + declarations: [OptionWithDisableRipple] |
| 13 | + }).compileComponents(); |
| 14 | + })); |
| 15 | + |
| 16 | + describe('ripples', () => { |
| 17 | + let fixture: ComponentFixture<OptionWithDisableRipple>; |
| 18 | + let optionDebugElement: DebugElement; |
| 19 | + let optionNativeElement: HTMLElement; |
| 20 | + let optionInstance: MdOption; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + fixture = TestBed.createComponent(OptionWithDisableRipple); |
| 24 | + fixture.detectChanges(); |
| 25 | + |
| 26 | + optionDebugElement = fixture.debugElement.query(By.directive(MdOption)); |
| 27 | + optionNativeElement = optionDebugElement.nativeElement; |
| 28 | + optionInstance = optionDebugElement.componentInstance; |
| 29 | + }); |
| 30 | + |
| 31 | + it('should show ripples by default', () => { |
| 32 | + expect(optionInstance.disableRipple).toBe(false, 'Expected ripples to be enabled by default'); |
| 33 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 34 | + .toBe(0, 'Expected no ripples to show up initially'); |
| 35 | + |
| 36 | + dispatchFakeEvent(optionNativeElement, 'mousedown'); |
| 37 | + dispatchFakeEvent(optionNativeElement, 'mouseup'); |
| 38 | + |
| 39 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 40 | + .toBe(1, 'Expected one ripple to show up after a fake click.'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not show ripples if the option is disabled', () => { |
| 44 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 45 | + .toBe(0, 'Expected no ripples to show up initially'); |
| 46 | + |
| 47 | + fixture.componentInstance.disabled = true; |
| 48 | + fixture.detectChanges(); |
| 49 | + |
| 50 | + dispatchFakeEvent(optionNativeElement, 'mousedown'); |
| 51 | + dispatchFakeEvent(optionNativeElement, 'mouseup'); |
| 52 | + |
| 53 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 54 | + .toBe(0, 'Expected no ripples to show up after click on a disabled option.'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should not show ripples if the ripples are disabled using disableRipple', () => { |
| 58 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 59 | + .toBe(0, 'Expected no ripples to show up initially'); |
| 60 | + |
| 61 | + fixture.componentInstance.disableRipple = true; |
| 62 | + fixture.detectChanges(); |
| 63 | + |
| 64 | + dispatchFakeEvent(optionNativeElement, 'mousedown'); |
| 65 | + dispatchFakeEvent(optionNativeElement, 'mouseup'); |
| 66 | + |
| 67 | + expect(optionNativeElement.querySelectorAll('.mat-ripple-element').length) |
| 68 | + .toBe(0, 'Expected no ripples to show up after click when ripples are disabled.'); |
| 69 | + }); |
| 70 | + |
| 71 | + }); |
| 72 | + |
| 73 | +}); |
| 74 | + |
| 75 | +@Component({ |
| 76 | + template: `<md-option [disableRipple]="disableRipple" [disabled]="disabled"></md-option>` |
| 77 | +}) |
| 78 | +export class OptionWithDisableRipple { |
| 79 | + disableRipple: boolean; |
| 80 | + disabled: boolean; |
| 81 | +} |
0 commit comments