|
| 1 | +import {HarnessLoader} from '@angular/cdk-experimental/testing'; |
| 2 | +import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed'; |
| 3 | +import {Component} from '@angular/core'; |
| 4 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 5 | +import {FormControl, ReactiveFormsModule} from '@angular/forms'; |
| 6 | +import {MatCheckboxModule} from '@angular/material/checkbox'; |
| 7 | +import {MatCheckboxModule as MatMdcCheckboxModule} from '../index'; |
| 8 | +import {MatCheckboxHarness} from './checkbox-harness'; |
| 9 | +import {MatCheckboxHarness as MatMdcCheckboxHarness} from './mdc-checkbox-harness'; |
| 10 | + |
| 11 | +let fixture: ComponentFixture<unknown>; |
| 12 | +let loader: HarnessLoader; |
| 13 | +let checkboxHarness: typeof MatCheckboxHarness; |
| 14 | + |
| 15 | +describe('MatCheckboxHarness', () => { |
| 16 | + describe('non-MDC-based', () => { |
| 17 | + beforeEach(async () => { |
| 18 | + await TestBed.configureTestingModule({ |
| 19 | + imports: [MatCheckboxModule, ReactiveFormsModule], |
| 20 | + declarations: [CheckboxHarnessTest], |
| 21 | + }).compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(CheckboxHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + checkboxHarness = MatCheckboxHarness; |
| 27 | + }); |
| 28 | + |
| 29 | + runTests(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('MDC-based', () => { |
| 33 | + beforeEach(async () => { |
| 34 | + await TestBed.configureTestingModule({ |
| 35 | + imports: [MatMdcCheckboxModule, ReactiveFormsModule], |
| 36 | + declarations: [CheckboxHarnessTest], |
| 37 | + }).compileComponents(); |
| 38 | + |
| 39 | + fixture = TestBed.createComponent(CheckboxHarnessTest); |
| 40 | + fixture.detectChanges(); |
| 41 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 42 | + // Public APIs are the same as MatCheckboxHarness, but cast is necessary because of different |
| 43 | + // private fields. |
| 44 | + checkboxHarness = MatMdcCheckboxHarness as any; |
| 45 | + }); |
| 46 | + |
| 47 | + runTests(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +/** Shared tests to run on both the original and MDC-based checkboxes. */ |
| 52 | +function runTests() { |
| 53 | + it('should load all checkbox harnesses', async () => { |
| 54 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 55 | + expect(checkboxes.length).toBe(2); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should load checkbox with exact label', async () => { |
| 59 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: 'First'})); |
| 60 | + expect(checkboxes.length).toBe(1); |
| 61 | + expect(await checkboxes[0].getLabelText()).toBe('First'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should load checkbox with regex label match', async () => { |
| 65 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness.with({label: /^s/i})); |
| 66 | + expect(checkboxes.length).toBe(1); |
| 67 | + expect(await checkboxes[0].getLabelText()).toBe('Second'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should get checked state', async () => { |
| 71 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 72 | + expect(await checkboxes[0].isChecked()).toBe(true); |
| 73 | + expect(await checkboxes[1].isChecked()).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should get indeterminate state', async () => { |
| 77 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 78 | + expect(await checkboxes[0].isIndeterminate()).toBe(false); |
| 79 | + expect(await checkboxes[1].isIndeterminate()).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should get disabled state', async () => { |
| 83 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 84 | + expect(await checkboxes[0].isDisabled()).toBe(false); |
| 85 | + expect(await checkboxes[1].isDisabled()).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should get required state', async () => { |
| 89 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 90 | + expect(await checkboxes[0].isRequired()).toBe(true); |
| 91 | + expect(await checkboxes[1].isRequired()).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should get valid state', async () => { |
| 95 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 96 | + expect(await checkboxes[0].isValid()).toBe(true); |
| 97 | + expect(await checkboxes[1].isValid()).toBe(true); |
| 98 | + await checkboxes[0].uncheck(); |
| 99 | + expect(await checkboxes[0].isValid()).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should get name', async () => { |
| 103 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); |
| 104 | + expect(await checkbox.getName()).toBe('first-name'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should get value', async () => { |
| 108 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); |
| 109 | + expect(await checkbox.getValue()).toBe('first-value'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should get aria-label', async () => { |
| 113 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); |
| 114 | + expect(await checkbox.getAriaLabel()).toBe('First checkbox'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should get aria-labelledby', async () => { |
| 118 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'Second'})); |
| 119 | + expect(await checkbox.getAriaLabelledby()).toBe('second-label'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should get label text', async () => { |
| 123 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 124 | + expect(await checkboxes[0].getLabelText()).toBe('First'); |
| 125 | + expect(await checkboxes[1].getLabelText()).toBe('Second'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should focus checkbox', async () => { |
| 129 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); |
| 130 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 131 | + await checkbox.foucs(); |
| 132 | + expect(getActiveElementTagName()).toBe('input'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should blur checkbox', async () => { |
| 136 | + const checkbox = await loader.getHarness(checkboxHarness.with({label: 'First'})); |
| 137 | + await checkbox.foucs(); |
| 138 | + expect(getActiveElementTagName()).toBe('input'); |
| 139 | + await checkbox.blur(); |
| 140 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should toggle checkbox', async () => { |
| 144 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 145 | + await checkboxes[0].toggle(); |
| 146 | + await checkboxes[1].toggle(); |
| 147 | + expect(await checkboxes[0].isChecked()).toBe(false); |
| 148 | + expect(await checkboxes[1].isChecked()).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should check checkbox', async () => { |
| 152 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 153 | + await checkboxes[0].check(); |
| 154 | + await checkboxes[1].check(); |
| 155 | + expect(await checkboxes[0].isChecked()).toBe(true); |
| 156 | + expect(await checkboxes[1].isChecked()).toBe(true); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should uncheck checkbox', async () => { |
| 160 | + const checkboxes = await loader.getAllHarnesses(checkboxHarness); |
| 161 | + await checkboxes[0].uncheck(); |
| 162 | + await checkboxes[1].uncheck(); |
| 163 | + expect(await checkboxes[0].isChecked()).toBe(false); |
| 164 | + expect(await checkboxes[1].isChecked()).toBe(false); |
| 165 | + }); |
| 166 | +} |
| 167 | + |
| 168 | +function getActiveElementTagName() { |
| 169 | + return document.activeElement ? document.activeElement.tagName.toLowerCase() : ''; |
| 170 | +} |
| 171 | + |
| 172 | +@Component({ |
| 173 | + template: ` |
| 174 | + <mat-checkbox |
| 175 | + [formControl]="ctrl" |
| 176 | + required |
| 177 | + name="first-name" |
| 178 | + value="first-value" |
| 179 | + aria-label="First checkbox"> |
| 180 | + First |
| 181 | + </mat-checkbox> |
| 182 | + <mat-checkbox indeterminate="true" disabled="true" aria-labelledby="second-label"> |
| 183 | + Second |
| 184 | + </mat-checkbox> |
| 185 | + <span id="second-label">Second checkbox</span> |
| 186 | + ` |
| 187 | +}) |
| 188 | +class CheckboxHarnessTest { |
| 189 | + ctrl = new FormControl(true); |
| 190 | +} |
| 191 | + |
0 commit comments