@@ -1182,3 +1182,65 @@ test('return a proper error message when no label is found and there is an aria-
1182
1182
</div>"
1183
1183
` )
1184
1184
} )
1185
+
1186
+ // https://github.com/testing-library/dom-testing-library/issues/723
1187
+ it ( 'gets form controls by label text on IE and other legacy browsers' , ( ) => {
1188
+ // Simulate lack of support for HTMLInputElement.prototype.labels
1189
+ const inputLabelsSpy = jest
1190
+ . spyOn ( HTMLInputElement . prototype , 'labels' , 'get' )
1191
+ . mockReturnValue ( undefined )
1192
+
1193
+ const { getByLabelText} = renderIntoDocument ( `
1194
+ <div>
1195
+ <label>
1196
+ 1st<input id="first-id" />
1197
+ </label>
1198
+ <div>
1199
+ <label for="second-id">2nd</label>
1200
+ <input id="second-id" />
1201
+ </div>
1202
+ <div>
1203
+ <label id="third-label">3rd</label>
1204
+ <input aria-labelledby="third-label" id="third-id" />
1205
+ </div>
1206
+ <div>
1207
+ <label for="fourth.id">4th</label>
1208
+ <input id="fourth.id" />
1209
+ </div>
1210
+ <div>
1211
+ <div>
1212
+ <label id="fifth-label-one">5th one</label>
1213
+ <label id="fifth-label-two">5th two</label>
1214
+ <input aria-labelledby="fifth-label-one fifth-label-two" id="fifth-id" />
1215
+ </div>
1216
+ <div>
1217
+ <input id="sixth-label-one" value="6th one"/>
1218
+ <input id="sixth-label-two" value="6th two"/>
1219
+ <label id="sixth-label-three">6th three</label>
1220
+ <input aria-labelledby="sixth-label-one sixth-label-two sixth-label-three" id="sixth-id" />
1221
+ </div>
1222
+ <div>
1223
+ <span id="seventh-label-one">7th one</span>
1224
+ <input aria-labelledby="seventh-label-one" id="seventh-id" />
1225
+ </div>
1226
+ <div>
1227
+ <label id="eighth.label">8th one</label>
1228
+ <input aria-labelledby="eighth.label" id="eighth.id" />
1229
+ </div>
1230
+ </div>
1231
+ ` )
1232
+ expect ( getByLabelText ( '1st' ) . id ) . toBe ( 'first-id' )
1233
+ expect ( getByLabelText ( '2nd' ) . id ) . toBe ( 'second-id' )
1234
+ expect ( getByLabelText ( '3rd' ) . id ) . toBe ( 'third-id' )
1235
+ expect ( getByLabelText ( '4th' ) . id ) . toBe ( 'fourth.id' )
1236
+ expect ( getByLabelText ( '5th one' ) . id ) . toBe ( 'fifth-id' )
1237
+ expect ( getByLabelText ( '5th two' ) . id ) . toBe ( 'fifth-id' )
1238
+ expect ( getByLabelText ( '6th one' ) . id ) . toBe ( 'sixth-id' )
1239
+ expect ( getByLabelText ( '6th two' ) . id ) . toBe ( 'sixth-id' )
1240
+ expect ( getByLabelText ( '6th one 6th two' ) . id ) . toBe ( 'sixth-id' )
1241
+ expect ( getByLabelText ( '6th one 6th two 6th three' ) . id ) . toBe ( 'sixth-id' )
1242
+ expect ( getByLabelText ( '7th one' ) . id ) . toBe ( 'seventh-id' )
1243
+ expect ( getByLabelText ( '8th one' ) . id ) . toBe ( 'eighth.id' )
1244
+
1245
+ inputLabelsSpy . mockRestore ( )
1246
+ } )
0 commit comments