@@ -4,13 +4,14 @@ import {Component} from '@angular/core';
4
4
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
5
5
import { ReactiveFormsModule } from '@angular/forms' ;
6
6
import { MatRadioModule } from '@angular/material/radio' ;
7
- import { MatRadioButtonHarness } from './radio-harness' ;
7
+ import { MatRadioButtonHarness , MatRadioGroupHarness } from './radio-harness' ;
8
8
9
9
let fixture : ComponentFixture < MultipleRadioButtonsHarnessTest > ;
10
10
let loader : HarnessLoader ;
11
11
let radioButtonHarness : typeof MatRadioButtonHarness ;
12
+ let radioGroupHarness : typeof MatRadioGroupHarness ;
12
13
13
- describe ( 'MatRadioButtonHarness ' , ( ) => {
14
+ describe ( 'standard radio harnesses ' , ( ) => {
14
15
describe ( 'non-MDC-based' , ( ) => {
15
16
beforeEach ( async ( ) => {
16
17
await TestBed
@@ -24,9 +25,11 @@ describe('MatRadioButtonHarness', () => {
24
25
fixture . detectChanges ( ) ;
25
26
loader = TestbedHarnessEnvironment . loader ( fixture ) ;
26
27
radioButtonHarness = MatRadioButtonHarness ;
28
+ radioGroupHarness = MatRadioGroupHarness ;
27
29
} ) ;
28
30
29
- runTests ( ) ;
31
+ describe ( 'MatRadioButtonHarness' , ( ) => runRadioButtonTests ( ) ) ;
32
+ describe ( 'MatRadioGroupHarness' , ( ) => runRadioGroupTests ( ) ) ;
30
33
} ) ;
31
34
32
35
describe (
@@ -36,11 +39,119 @@ describe('MatRadioButtonHarness', () => {
36
39
} ) ;
37
40
} ) ;
38
41
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
+
39
150
/** Shared tests to run on both the original and MDC-based radio-button's. */
40
- function runTests ( ) {
151
+ function runRadioButtonTests ( ) {
41
152
it ( 'should load all radio-button harnesses' , async ( ) => {
42
153
const radios = await loader . getAllHarnesses ( radioButtonHarness ) ;
43
- expect ( radios . length ) . toBe ( 4 ) ;
154
+ expect ( radios . length ) . toBe ( 9 ) ;
44
155
} ) ;
45
156
46
157
it ( 'should load radio-button with exact label' , async ( ) => {
@@ -85,6 +196,13 @@ function runTests() {
85
196
expect ( await thirdRadio . getLabelText ( ) ) . toBe ( 'Option #3' ) ;
86
197
} ) ;
87
198
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
+
88
206
it ( 'should get disabled state' , async ( ) => {
89
207
const [ firstRadio ] = await loader . getAllHarnesses ( radioButtonHarness ) ;
90
208
expect ( await firstRadio . isDisabled ( ) ) . toBe ( false ) ;
@@ -141,6 +259,7 @@ function runTests() {
141
259
expect ( await radioButton . isRequired ( ) ) . toBe ( true ) ;
142
260
} ) ;
143
261
}
262
+
144
263
function getActiveElementTagName ( ) {
145
264
return document . activeElement ? document . activeElement . tagName . toLowerCase ( ) : '' ;
146
265
}
@@ -157,12 +276,32 @@ function getActiveElementTagName() {
157
276
Option #{{i + 1}}
158
277
</mat-radio-button>
159
278
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>
163
299
`
164
300
} )
165
301
class MultipleRadioButtonsHarnessTest {
166
302
values = [ 'opt1' , 'opt2' , 'opt3' ] ;
167
303
disableAll = false ;
304
+ secondGroupId = 'my-group-2' ;
305
+ thirdGroupName : string = 'third-group-name' ;
306
+ thirdGroupButtonName : string | undefined = undefined ;
168
307
}
0 commit comments