|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Platform, PlatformModule} from '@angular/cdk/platform'; |
| 4 | +import {Component} from '@angular/core'; |
| 5 | +import {ComponentFixture, inject, TestBed} from '@angular/core/testing'; |
| 6 | +import {MatButtonModule} from '@angular/material/button'; |
| 7 | +import {MatButtonModule as MatMdcButtonModule} from '../index'; |
| 8 | +import {MatButtonHarness} from './button-harness'; |
| 9 | +import {MatButtonHarness as MatMdcButtonHarness} from './mdc-button-harness'; |
| 10 | + |
| 11 | +let fixture: ComponentFixture<ButtonHarnessTest>; |
| 12 | +let loader: HarnessLoader; |
| 13 | +let buttonHarness: typeof MatButtonHarness; |
| 14 | +let platform: Platform; |
| 15 | + |
| 16 | +describe('MatButtonHarness', () => { |
| 17 | + describe('non-MDC-based', () => { |
| 18 | + beforeEach(async () => { |
| 19 | + await TestBed.configureTestingModule({ |
| 20 | + imports: [MatButtonModule, PlatformModule], |
| 21 | + declarations: [ButtonHarnessTest], |
| 22 | + }).compileComponents(); |
| 23 | + |
| 24 | + fixture = TestBed.createComponent(ButtonHarnessTest); |
| 25 | + fixture.detectChanges(); |
| 26 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 27 | + buttonHarness = MatButtonHarness; |
| 28 | + }); |
| 29 | + |
| 30 | + runTests(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('MDC-based', () => { |
| 34 | + beforeEach(async () => { |
| 35 | + await TestBed.configureTestingModule({ |
| 36 | + imports: [MatMdcButtonModule], |
| 37 | + declarations: [ButtonHarnessTest], |
| 38 | + }).compileComponents(); |
| 39 | + |
| 40 | + fixture = TestBed.createComponent(ButtonHarnessTest); |
| 41 | + fixture.detectChanges(); |
| 42 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 43 | + // Public APIs are the same as MatButtonHarness, but cast is necessary because of different |
| 44 | + // private fields. |
| 45 | + buttonHarness = MatMdcButtonHarness as any; |
| 46 | + }); |
| 47 | + |
| 48 | + runTests(); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +/** Shared tests to run on both the original and MDC-based buttons. */ |
| 53 | +function runTests() { |
| 54 | + beforeEach(inject([Platform], (p: Platform) => { |
| 55 | + platform = p; |
| 56 | + })); |
| 57 | + |
| 58 | + it('should load all button harnesses', async () => { |
| 59 | + const buttons = await loader.getAllHarnesses(buttonHarness); |
| 60 | + expect(buttons.length).toBe(14); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should load button with exact text', async () => { |
| 64 | + const buttons = await loader.getAllHarnesses(buttonHarness.with({text: 'Basic button'})); |
| 65 | + expect(buttons.length).toBe(1); |
| 66 | + expect(await buttons[0].getText()).toBe('Basic button'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should load button with regex label match', async () => { |
| 70 | + const buttons = await loader.getAllHarnesses(buttonHarness.with({text: /basic/i})); |
| 71 | + expect(buttons.length).toBe(2); |
| 72 | + expect(await buttons[0].getText()).toBe('Basic button'); |
| 73 | + expect(await buttons[1].getText()).toBe('Basic anchor'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should get disabled state', async () => { |
| 77 | + // Grab each combination of [enabled, disabled] ⨯ [button, anchor] |
| 78 | + const [disabledFlatButton, enabledFlatAnchor] = |
| 79 | + await loader.getAllHarnesses(buttonHarness.with({text: /flat/i})); |
| 80 | + const [enabledRaisedButton, disabledRaisedAnchor] = |
| 81 | + await loader.getAllHarnesses(buttonHarness.with({text: /raised/i})); |
| 82 | + |
| 83 | + expect(await enabledFlatAnchor.isDisabled()).toBe(false); |
| 84 | + expect(await disabledFlatButton.isDisabled()).toBe(true); |
| 85 | + expect(await enabledRaisedButton.isDisabled()).toBe(false); |
| 86 | + expect(await disabledRaisedAnchor.isDisabled()).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should get button text', async () => { |
| 90 | + const [firstButton, secondButton] = await loader.getAllHarnesses(buttonHarness); |
| 91 | + expect(await firstButton.getText()).toBe('Basic button'); |
| 92 | + expect(await secondButton.getText()).toBe('Flat button'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should focus and blur a button', async () => { |
| 96 | + const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'})); |
| 97 | + expect(getActiveElementId()).not.toBe('basic'); |
| 98 | + await button.foucs(); |
| 99 | + expect(getActiveElementId()).toBe('basic'); |
| 100 | + await button.blur(); |
| 101 | + expect(getActiveElementId()).not.toBe('basic'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should click a button', async () => { |
| 105 | + const button = await loader.getHarness(buttonHarness.with({text: 'Basic button'})); |
| 106 | + await button.click(); |
| 107 | + |
| 108 | + expect(fixture.componentInstance.clicked).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should not click a disabled button', async () => { |
| 112 | + // Older versions of Edge have a bug where `disabled` buttons are still clickable if |
| 113 | + // they contain child elements. We skip this check on Edge. |
| 114 | + // See https://stackoverflow.com/questions/32377026/disabled-button-is-clickable-on-edge-browser |
| 115 | + if (platform.EDGE) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const button = await loader.getHarness(buttonHarness.with({text: 'Flat button'})); |
| 120 | + await button.click(); |
| 121 | + |
| 122 | + expect(fixture.componentInstance.clicked).toBe(false); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +function getActiveElementId() { |
| 127 | + return document.activeElement ? document.activeElement.id : ''; |
| 128 | +} |
| 129 | + |
| 130 | +@Component({ |
| 131 | + // Include one of each type of button selector to ensure that they're all captured by |
| 132 | + // the harness's selector. |
| 133 | + template: ` |
| 134 | + <button id="basic" type="button" mat-button (click)="clicked = true"> |
| 135 | + Basic button |
| 136 | + </button> |
| 137 | + <button id="flat" type="button" mat-flat-button disabled (click)="clicked = true"> |
| 138 | + Flat button |
| 139 | + </button> |
| 140 | + <button id="raised" type="button" mat-raised-button>Raised button</button> |
| 141 | + <button id="stroked" type="button" mat-stroked-button>Stroked button</button> |
| 142 | + <button id="icon" type="button" mat-icon-button>Icon button</button> |
| 143 | + <button id="fab" type="button" mat-fab>Fab button</button> |
| 144 | + <button id="mini-fab" type="button" mat-mini-fab>Mini Fab button</button> |
| 145 | +
|
| 146 | + <a id="anchor-basic" mat-button>Basic anchor</a> |
| 147 | + <a id="anchor-flat" mat-flat-button>Flat anchor</a> |
| 148 | + <a id="anchor-raised" mat-raised-button disabled>Raised anchor</a> |
| 149 | + <a id="anchor-stroked" mat-stroked-button>Stroked anchor</a> |
| 150 | + <a id="anchor-icon" mat-icon-button>Icon anchor</a> |
| 151 | + <a id="anchor-fab" mat-fab>Fab anchor</a> |
| 152 | + <a id="anchor-mini-fab" mat-mini-fab>Mini Fab anchor</a> |
| 153 | + ` |
| 154 | +}) |
| 155 | +class ButtonHarnessTest { |
| 156 | + disabled = true; |
| 157 | + clicked = false; |
| 158 | +} |
| 159 | + |
0 commit comments