@@ -4,13 +4,14 @@ import {Component} from '@angular/core';
44import { ComponentFixture , TestBed } from '@angular/core/testing' ;
55import { ReactiveFormsModule } from '@angular/forms' ;
66import { MatRadioModule } from '@angular/material/radio' ;
7- import { MatRadioButtonHarness } from './radio-harness' ;
7+ import { MatRadioButtonHarness , MatRadioGroupHarness } from './radio-harness' ;
88
99let fixture : ComponentFixture < MultipleRadioButtonsHarnessTest > ;
1010let loader : HarnessLoader ;
1111let radioButtonHarness : typeof MatRadioButtonHarness ;
12+ let radioGroupHarness : typeof MatRadioGroupHarness ;
1213
13- describe ( 'MatRadioButtonHarness ' , ( ) => {
14+ describe ( 'standard radio harnesses ' , ( ) => {
1415 describe ( 'non-MDC-based' , ( ) => {
1516 beforeEach ( async ( ) => {
1617 await TestBed
@@ -24,9 +25,11 @@ describe('MatRadioButtonHarness', () => {
2425 fixture . detectChanges ( ) ;
2526 loader = TestbedHarnessEnvironment . loader ( fixture ) ;
2627 radioButtonHarness = MatRadioButtonHarness ;
28+ radioGroupHarness = MatRadioGroupHarness ;
2729 } ) ;
2830
29- runTests ( ) ;
31+ describe ( 'MatRadioButtonHarness' , ( ) => runRadioButtonTests ( ) ) ;
32+ describe ( 'MatRadioGroupHarness' , ( ) => runRadioGroupTests ( ) ) ;
3033 } ) ;
3134
3235 describe (
@@ -36,11 +39,119 @@ describe('MatRadioButtonHarness', () => {
3639 } ) ;
3740} ) ;
3841
42+ /** Shared tests to run on both the original and MDC-based radio-group's. */
43+ function runRadioGroupTests ( ) {
44+ it ( 'should load all radio-group harnesses' , async ( ) => {
45+ const groups = await loader . getAllHarnesses ( radioGroupHarness ) ;
46+ expect ( groups . length ) . toBe ( 3 ) ;
47+ } ) ;
48+
49+ it ( 'should load radio-group with exact id' , async ( ) => {
50+ const groups = await loader . getAllHarnesses ( radioGroupHarness . with ( { id : 'my-group-2' } ) ) ;
51+ expect ( groups . length ) . toBe ( 1 ) ;
52+ } ) ;
53+
54+ it ( 'should load radio-group by name' , async ( ) => {
55+ let groups = await loader . getAllHarnesses ( radioGroupHarness . with ( { name : 'my-group-2-name' } ) ) ;
56+ expect ( groups . length ) . toBe ( 1 ) ;
57+ expect ( await groups [ 0 ] . getId ( ) ) . toBe ( 'my-group-2' ) ;
58+
59+ groups = await loader . getAllHarnesses ( radioGroupHarness . with ( { name : 'my-group-1-name' } ) ) ;
60+ expect ( groups . length ) . toBe ( 1 ) ;
61+ expect ( await groups [ 0 ] . getId ( ) ) . toBe ( 'my-group-1' ) ;
62+ } ) ;
63+
64+ it ( 'should throw when finding radio-group with specific name that has mismatched ' +
65+ 'radio-button names' ,
66+ async ( ) => {
67+ fixture . componentInstance . thirdGroupButtonName = 'other-name' ;
68+ fixture . detectChanges ( ) ;
69+
70+ let errorMessage : string | null = null ;
71+ try {
72+ await loader . getAllHarnesses ( radioGroupHarness . with ( { name : 'third-group-name' } ) ) ;
73+ } catch ( e ) {
74+ errorMessage = e . toString ( ) ;
75+ }
76+
77+ expect ( errorMessage )
78+ . toMatch (
79+ / l o c a t o r f o u n d a r a d i o - g r o u p w i t h n a m e " t h i r d - g r o u p - n a m e " .* h a v e m i s m a t c h i n g n a m e s / ) ;
80+ } ) ;
81+
82+ it ( 'should get name of radio-group' , async ( ) => {
83+ const groups = await loader . getAllHarnesses ( radioGroupHarness ) ;
84+ expect ( groups . length ) . toBe ( 3 ) ;
85+ expect ( await groups [ 0 ] . getName ( ) ) . toBe ( 'my-group-1-name' ) ;
86+ expect ( await groups [ 1 ] . getName ( ) ) . toBe ( 'my-group-2-name' ) ;
87+ expect ( await groups [ 2 ] . getName ( ) ) . toBe ( 'third-group-name' ) ;
88+
89+ fixture . componentInstance . secondGroupId = 'new-group' ;
90+ fixture . detectChanges ( ) ;
91+
92+ expect ( await groups [ 1 ] . getName ( ) ) . toBe ( 'new-group-name' ) ;
93+
94+ fixture . componentInstance . thirdGroupButtonName = 'other-button-name' ;
95+ fixture . detectChanges ( ) ;
96+
97+ let errorMessage : string | null = null ;
98+ try {
99+ await groups [ 2 ] . getName ( ) ;
100+ } catch ( e ) {
101+ errorMessage = e . toString ( ) ;
102+ }
103+
104+ expect ( errorMessage ) . toMatch ( / R a d i o b u t t o n s i n r a d i o - g r o u p h a v e m i s m a t c h i n g n a m e s ./ ) ;
105+ } ) ;
106+
107+ it ( 'should get id of radio-group' , async ( ) => {
108+ const groups = await loader . getAllHarnesses ( radioGroupHarness ) ;
109+ expect ( groups . length ) . toBe ( 3 ) ;
110+ expect ( await groups [ 0 ] . getId ( ) ) . toBe ( 'my-group-1' ) ;
111+ expect ( await groups [ 1 ] . getId ( ) ) . toBe ( 'my-group-2' ) ;
112+ expect ( await groups [ 2 ] . getId ( ) ) . toBe ( '' ) ;
113+
114+ fixture . componentInstance . secondGroupId = 'new-group-name' ;
115+ fixture . detectChanges ( ) ;
116+
117+ expect ( await groups [ 1 ] . getId ( ) ) . toBe ( 'new-group-name' ) ;
118+ } ) ;
119+
120+ it ( 'should get selected value of radio-group' , async ( ) => {
121+ const [ firstGroup , secondGroup ] = await loader . getAllHarnesses ( radioGroupHarness ) ;
122+ expect ( await firstGroup . getSelectedValue ( ) ) . toBe ( 'opt2' ) ;
123+ expect ( await secondGroup . getSelectedValue ( ) ) . toBe ( null ) ;
124+ } ) ;
125+
126+ it ( 'should get radio-button harnesses of radio-group' , async ( ) => {
127+ const groups = await loader . getAllHarnesses ( radioGroupHarness ) ;
128+ expect ( groups . length ) . toBe ( 3 ) ;
129+
130+ expect ( ( await groups [ 0 ] . getRadioButtons ( ) ) . length ) . toBe ( 3 ) ;
131+ expect ( ( await groups [ 1 ] . getRadioButtons ( ) ) . length ) . toBe ( 1 ) ;
132+ expect ( ( await groups [ 2 ] . getRadioButtons ( ) ) . length ) . toBe ( 2 ) ;
133+ } ) ;
134+
135+ it ( 'should get selected radio-button harnesses of radio-group' , async ( ) => {
136+ const groups = await loader . getAllHarnesses ( radioGroupHarness ) ;
137+ expect ( groups . length ) . toBe ( 3 ) ;
138+
139+ const groupOneSelected = await groups [ 0 ] . getSelectedRadioButton ( ) ;
140+ const groupTwoSelected = await groups [ 1 ] . getSelectedRadioButton ( ) ;
141+ const groupThreeSelected = await groups [ 2 ] . getSelectedRadioButton ( ) ;
142+
143+ expect ( groupOneSelected ) . not . toBeNull ( ) ;
144+ expect ( groupTwoSelected ) . toBeNull ( ) ;
145+ expect ( groupThreeSelected ) . toBeNull ( ) ;
146+ expect ( await groupOneSelected ! . getId ( ) ) . toBe ( 'opt2-group-one' ) ;
147+ } ) ;
148+ }
149+
39150/** Shared tests to run on both the original and MDC-based radio-button's. */
40- function runTests ( ) {
151+ function runRadioButtonTests ( ) {
41152 it ( 'should load all radio-button harnesses' , async ( ) => {
42153 const radios = await loader . getAllHarnesses ( radioButtonHarness ) ;
43- expect ( radios . length ) . toBe ( 4 ) ;
154+ expect ( radios . length ) . toBe ( 9 ) ;
44155 } ) ;
45156
46157 it ( 'should load radio-button with exact label' , async ( ) => {
@@ -85,6 +196,13 @@ function runTests() {
85196 expect ( await thirdRadio . getLabelText ( ) ) . toBe ( 'Option #3' ) ;
86197 } ) ;
87198
199+ it ( 'should get value' , async ( ) => {
200+ const [ firstRadio , secondRadio , thirdRadio ] = await loader . getAllHarnesses ( radioButtonHarness ) ;
201+ expect ( await firstRadio . getValue ( ) ) . toBe ( 'opt1' ) ;
202+ expect ( await secondRadio . getValue ( ) ) . toBe ( 'opt2' ) ;
203+ expect ( await thirdRadio . getValue ( ) ) . toBe ( 'opt3' ) ;
204+ } ) ;
205+
88206 it ( 'should get disabled state' , async ( ) => {
89207 const [ firstRadio ] = await loader . getAllHarnesses ( radioButtonHarness ) ;
90208 expect ( await firstRadio . isDisabled ( ) ) . toBe ( false ) ;
@@ -141,6 +259,7 @@ function runTests() {
141259 expect ( await radioButton . isRequired ( ) ) . toBe ( true ) ;
142260 } ) ;
143261}
262+
144263function getActiveElementTagName ( ) {
145264 return document . activeElement ? document . activeElement . tagName . toLowerCase ( ) : '' ;
146265}
@@ -157,12 +276,32 @@ function getActiveElementTagName() {
157276 Option #{{i + 1}}
158277 </mat-radio-button>
159278
160- <mat-radio-button id="required-radio" required name="acceptsTerms">
161- Accept terms of conditions
162- </mat-radio-button>
279+ <mat-radio-group id="my-group-1" name="my-group-1-name">
280+ <mat-radio-button *ngFor="let value of values"
281+ [checked]="value === 'opt2'"
282+ [value]="value"
283+ [id]="value + '-group-one'">
284+ {{value}}
285+ </mat-radio-button>
286+ </mat-radio-group>
287+
288+
289+ <mat-radio-group [id]="secondGroupId" [name]="secondGroupId + '-name'">
290+ <mat-radio-button id="required-radio" required [value]="true">
291+ Accept terms of conditions
292+ </mat-radio-button>
293+ </mat-radio-group>
294+
295+ <mat-radio-group [name]="thirdGroupName">
296+ <mat-radio-button [value]="true">First</mat-radio-button>
297+ <mat-radio-button [value]="false" [name]="thirdGroupButtonName"></mat-radio-button>
298+ </mat-radio-group>
163299 `
164300} )
165301class MultipleRadioButtonsHarnessTest {
166302 values = [ 'opt1' , 'opt2' , 'opt3' ] ;
167303 disableAll = false ;
304+ secondGroupId = 'my-group-2' ;
305+ thirdGroupName : string = 'third-group-name' ;
306+ thirdGroupButtonName : string | undefined = undefined ;
168307}
0 commit comments