|
| 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 {ReactiveFormsModule} from '@angular/forms'; |
| 6 | +import {MatRadioModule} from '@angular/material/radio'; |
| 7 | +import {MatRadioButtonHarness} from './radio-harness'; |
| 8 | + |
| 9 | +let fixture: ComponentFixture<MultipleRadioButtonsHarnessTest>; |
| 10 | +let loader: HarnessLoader; |
| 11 | +let radioButtonHarness: typeof MatRadioButtonHarness; |
| 12 | + |
| 13 | +describe('MatRadioButtonHarness', () => { |
| 14 | + describe('non-MDC-based', () => { |
| 15 | + beforeEach(async () => { |
| 16 | + await TestBed |
| 17 | + .configureTestingModule({ |
| 18 | + imports: [MatRadioModule, ReactiveFormsModule], |
| 19 | + declarations: [MultipleRadioButtonsHarnessTest], |
| 20 | + }) |
| 21 | + .compileComponents(); |
| 22 | + |
| 23 | + fixture = TestBed.createComponent(MultipleRadioButtonsHarnessTest); |
| 24 | + fixture.detectChanges(); |
| 25 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 26 | + radioButtonHarness = MatRadioButtonHarness; |
| 27 | + }); |
| 28 | + |
| 29 | + runTests(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe( |
| 33 | + 'MDC-based', |
| 34 | + () => { |
| 35 | + // TODO: run tests for MDC based radio-button once implemented. |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +/** Shared tests to run on both the original and MDC-based radio-button's. */ |
| 40 | +function runTests() { |
| 41 | + it('should load all radio-button harnesses', async () => { |
| 42 | + const radios = await loader.getAllHarnesses(radioButtonHarness); |
| 43 | + expect(radios.length).toBe(4); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should load radio-button with exact label', async () => { |
| 47 | + const radios = await loader.getAllHarnesses(radioButtonHarness.with({label: 'Option #2'})); |
| 48 | + expect(radios.length).toBe(1); |
| 49 | + expect(await radios[0].getId()).toBe('opt2'); |
| 50 | + expect(await radios[0].getLabelText()).toBe('Option #2'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should load radio-button with regex label match', async () => { |
| 54 | + const radios = await loader.getAllHarnesses(radioButtonHarness.with({label: /#3$/i})); |
| 55 | + expect(radios.length).toBe(1); |
| 56 | + expect(await radios[0].getId()).toBe('opt3'); |
| 57 | + expect(await radios[0].getLabelText()).toBe('Option #3'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should load radio-button with id', async () => { |
| 61 | + const radios = await loader.getAllHarnesses(radioButtonHarness.with({id: 'opt3'})); |
| 62 | + expect(radios.length).toBe(1); |
| 63 | + expect(await radios[0].getId()).toBe('opt3'); |
| 64 | + expect(await radios[0].getLabelText()).toBe('Option #3'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should load radio-buttons with same name', async () => { |
| 68 | + const radios = await loader.getAllHarnesses(radioButtonHarness.with({name: 'group1'})); |
| 69 | + expect(radios.length).toBe(2); |
| 70 | + |
| 71 | + expect(await radios[0].getId()).toBe('opt1'); |
| 72 | + expect(await radios[1].getId()).toBe('opt2'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should get checked state', async () => { |
| 76 | + const [uncheckedRadio, checkedRadio] = await loader.getAllHarnesses(radioButtonHarness); |
| 77 | + expect(await uncheckedRadio.isChecked()).toBe(false); |
| 78 | + expect(await checkedRadio.isChecked()).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should get label text', async () => { |
| 82 | + const [firstRadio, secondRadio, thirdRadio] = await loader.getAllHarnesses(radioButtonHarness); |
| 83 | + expect(await firstRadio.getLabelText()).toBe('Option #1'); |
| 84 | + expect(await secondRadio.getLabelText()).toBe('Option #2'); |
| 85 | + expect(await thirdRadio.getLabelText()).toBe('Option #3'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should get disabled state', async () => { |
| 89 | + const [firstRadio] = await loader.getAllHarnesses(radioButtonHarness); |
| 90 | + expect(await firstRadio.isDisabled()).toBe(false); |
| 91 | + |
| 92 | + fixture.componentInstance.disableAll = true; |
| 93 | + fixture.detectChanges(); |
| 94 | + |
| 95 | + expect(await firstRadio.isDisabled()).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should focus radio-button', async () => { |
| 99 | + const radioButton = await loader.getHarness(radioButtonHarness.with({id: 'opt2'})); |
| 100 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 101 | + await radioButton.focus(); |
| 102 | + expect(getActiveElementTagName()).toBe('input'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should blur radio-button', async () => { |
| 106 | + const radioButton = await loader.getHarness(radioButtonHarness.with({id: 'opt2'})); |
| 107 | + await radioButton.focus(); |
| 108 | + expect(getActiveElementTagName()).toBe('input'); |
| 109 | + await radioButton.blur(); |
| 110 | + expect(getActiveElementTagName()).not.toBe('input'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should check radio-button', async () => { |
| 114 | + const [uncheckedRadio, checkedRadio] = await loader.getAllHarnesses(radioButtonHarness); |
| 115 | + await uncheckedRadio.check(); |
| 116 | + expect(await uncheckedRadio.isChecked()).toBe(true); |
| 117 | + // Checked radio state should change since the two radio's |
| 118 | + // have the same name and only one can be selected. |
| 119 | + expect(await checkedRadio.isChecked()).toBe(false); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should not be able to check disabled radio-button', async () => { |
| 123 | + fixture.componentInstance.disableAll = true; |
| 124 | + fixture.detectChanges(); |
| 125 | + |
| 126 | + const radioButton = await loader.getHarness(radioButtonHarness.with({id: 'opt3'})); |
| 127 | + expect(await radioButton.isChecked()).toBe(false); |
| 128 | + await radioButton.check(); |
| 129 | + expect(await radioButton.isChecked()).toBe(false); |
| 130 | + |
| 131 | + fixture.componentInstance.disableAll = false; |
| 132 | + fixture.detectChanges(); |
| 133 | + |
| 134 | + expect(await radioButton.isChecked()).toBe(false); |
| 135 | + await radioButton.check(); |
| 136 | + expect(await radioButton.isChecked()).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should get required state', async () => { |
| 140 | + const radioButton = await loader.getHarness(radioButtonHarness.with({id: 'required-radio'})); |
| 141 | + expect(await radioButton.isRequired()).toBe(true); |
| 142 | + }); |
| 143 | +} |
| 144 | +function getActiveElementTagName() { |
| 145 | + return document.activeElement ? document.activeElement.tagName.toLowerCase() : ''; |
| 146 | +} |
| 147 | + |
| 148 | +@Component({ |
| 149 | + template: ` |
| 150 | + <mat-radio-button *ngFor="let value of values, let i = index" |
| 151 | + [name]="value === 'opt3' ? 'group2' : 'group1'" |
| 152 | + [disabled]="disableAll" |
| 153 | + [checked]="value === 'opt2'" |
| 154 | + [id]="value" |
| 155 | + [required]="value === 'opt2'" |
| 156 | + [value]="value"> |
| 157 | + Option #{{i + 1}} |
| 158 | + </mat-radio-button> |
| 159 | +
|
| 160 | + <mat-radio-button id="required-radio" required name="acceptsTerms"> |
| 161 | + Accept terms of conditions |
| 162 | + </mat-radio-button> |
| 163 | + ` |
| 164 | +}) |
| 165 | +class MultipleRadioButtonsHarnessTest { |
| 166 | + values = ['opt1', 'opt2', 'opt3']; |
| 167 | + disableAll = false; |
| 168 | +} |
0 commit comments