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