@@ -64,6 +64,27 @@ for (let i = 1; i <= 100; i++) {
64
64
manyItems . push ( { id : i , foo : 'Foo ' + i , bar : 'Bar ' + i , baz : 'Baz ' + i } ) ;
65
65
}
66
66
67
+ function ExampleSortTable ( ) {
68
+ let [ sortDescriptor , setSortDescriptor ] = React . useState ( { column : 'bar' , direction : 'ascending' } ) ;
69
+
70
+ return (
71
+ < TableView aria-label = "Table" sortDescriptor = { sortDescriptor } onSortChange = { setSortDescriptor } >
72
+ < TableHeader >
73
+ < Column key = "foo" allowsSorting > Foo</ Column >
74
+ < Column key = "bar" allowsSorting > Bar</ Column >
75
+ < Column key = "baz" > Baz</ Column >
76
+ </ TableHeader >
77
+ < TableBody >
78
+ < Row >
79
+ < Cell > Foo 1</ Cell >
80
+ < Cell > Bar 1</ Cell >
81
+ < Cell > Baz 1</ Cell >
82
+ </ Row >
83
+ </ TableBody >
84
+ </ TableView >
85
+ ) ;
86
+ }
87
+
67
88
describe ( 'TableView' , function ( ) {
68
89
let offsetWidth , offsetHeight ;
69
90
@@ -152,6 +173,7 @@ describe('TableView', function () {
152
173
153
174
for ( let header of headers ) {
154
175
expect ( header ) . not . toHaveAttribute ( 'aria-sort' ) ;
176
+ expect ( header ) . not . toHaveAttribute ( 'aria-describedby' ) ;
155
177
}
156
178
157
179
expect ( headers [ 0 ] ) . toHaveTextContent ( 'Foo' ) ;
@@ -233,6 +255,7 @@ describe('TableView', function () {
233
255
234
256
for ( let header of headers ) {
235
257
expect ( header ) . not . toHaveAttribute ( 'aria-sort' ) ;
258
+ expect ( header ) . not . toHaveAttribute ( 'aria-describedby' ) ;
236
259
}
237
260
238
261
let checkbox = within ( headers [ 0 ] ) . getByRole ( 'checkbox' ) ;
@@ -2242,6 +2265,22 @@ describe('TableView', function () {
2242
2265
triggerPress ( row ) ;
2243
2266
expect ( announce ) . toHaveBeenLastCalledWith ( 'Sam Smith not selected.' ) ;
2244
2267
} ) ;
2268
+
2269
+ it ( 'should announce changes in sort order' , function ( ) {
2270
+ let tree = render ( < ExampleSortTable /> ) ;
2271
+ let table = tree . getByRole ( 'grid' ) ;
2272
+ let columnheaders = within ( table ) . getAllByRole ( 'columnheader' ) ;
2273
+ expect ( columnheaders ) . toHaveLength ( 3 ) ;
2274
+
2275
+ triggerPress ( columnheaders [ 1 ] ) ;
2276
+ expect ( announce ) . toHaveBeenLastCalledWith ( 'sorted by column Bar in descending order' , 'assertive' , 500 ) ;
2277
+ triggerPress ( columnheaders [ 1 ] ) ;
2278
+ expect ( announce ) . toHaveBeenLastCalledWith ( 'sorted by column Bar in ascending order' , 'assertive' , 500 ) ;
2279
+ triggerPress ( columnheaders [ 0 ] ) ;
2280
+ expect ( announce ) . toHaveBeenLastCalledWith ( 'sorted by column Foo in ascending order' , 'assertive' , 500 ) ;
2281
+ triggerPress ( columnheaders [ 0 ] ) ;
2282
+ expect ( announce ) . toHaveBeenLastCalledWith ( 'sorted by column Foo in descending order' , 'assertive' , 500 ) ;
2283
+ } ) ;
2245
2284
} ) ;
2246
2285
} ) ;
2247
2286
@@ -3089,7 +3128,7 @@ describe('TableView', function () {
3089
3128
} ) ;
3090
3129
3091
3130
describe ( 'sorting' , function ( ) {
3092
- it ( 'should set aria-sort="none" on sortable column headers' , function ( ) {
3131
+ it ( 'should set the proper aria-describedby and aria- sort on sortable column headers' , function ( ) {
3093
3132
let tree = render (
3094
3133
< TableView aria-label = "Table" >
3095
3134
< TableHeader >
@@ -3113,9 +3152,14 @@ describe('TableView', function () {
3113
3152
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3114
3153
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3115
3154
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3155
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3156
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3157
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3158
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3159
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3116
3160
} ) ;
3117
3161
3118
- it ( 'should set aria-sort="ascending" on sorted column header' , function ( ) {
3162
+ it ( 'should set the proper aria-describedby and aria- sort on an ascending sorted column header' , function ( ) {
3119
3163
let tree = render (
3120
3164
< TableView aria-label = "Table" sortDescriptor = { { column : 'bar' , direction : 'ascending' } } >
3121
3165
< TableHeader >
@@ -3139,9 +3183,14 @@ describe('TableView', function () {
3139
3183
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3140
3184
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'ascending' ) ;
3141
3185
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3186
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3187
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3188
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3189
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3190
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3142
3191
} ) ;
3143
3192
3144
- it ( 'should set aria-sort="descending" on sorted column header' , function ( ) {
3193
+ it ( 'should set the proper aria-describedby and aria- sort on an descending sorted column header' , function ( ) {
3145
3194
let tree = render (
3146
3195
< TableView aria-label = "Table" sortDescriptor = { { column : 'bar' , direction : 'descending' } } >
3147
3196
< TableHeader >
@@ -3165,6 +3214,33 @@ describe('TableView', function () {
3165
3214
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3166
3215
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'descending' ) ;
3167
3216
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3217
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3218
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3219
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3220
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3221
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3222
+ } ) ;
3223
+
3224
+ it ( 'should add sort direction info to the column header\'s aria-describedby for Android' , function ( ) {
3225
+ let uaMock = jest . spyOn ( navigator , 'userAgent' , 'get' ) . mockImplementation ( ( ) => 'Android' ) ;
3226
+ let tree = render ( < ExampleSortTable /> ) ;
3227
+
3228
+ let table = tree . getByRole ( 'grid' ) ;
3229
+ let columnheaders = within ( table ) . getAllByRole ( 'columnheader' ) ;
3230
+ expect ( columnheaders ) . toHaveLength ( 3 ) ;
3231
+ expect ( columnheaders [ 0 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3232
+ expect ( columnheaders [ 1 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3233
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3234
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3235
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3236
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3237
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column, ascending' ) ;
3238
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3239
+
3240
+ triggerPress ( columnheaders [ 1 ] ) ;
3241
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column, descending' ) ;
3242
+
3243
+ uaMock . mockRestore ( ) ;
3168
3244
} ) ;
3169
3245
3170
3246
it ( 'should fire onSortChange when there is no existing sortDescriptor' , function ( ) {
@@ -3192,6 +3268,11 @@ describe('TableView', function () {
3192
3268
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3193
3269
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3194
3270
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3271
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3272
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3273
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3274
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3275
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3195
3276
3196
3277
triggerPress ( columnheaders [ 0 ] ) ;
3197
3278
@@ -3224,6 +3305,11 @@ describe('TableView', function () {
3224
3305
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'ascending' ) ;
3225
3306
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3226
3307
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3308
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3309
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3310
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3311
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3312
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3227
3313
3228
3314
triggerPress ( columnheaders [ 0 ] ) ;
3229
3315
@@ -3253,9 +3339,14 @@ describe('TableView', function () {
3253
3339
let table = tree . getByRole ( 'grid' ) ;
3254
3340
let columnheaders = within ( table ) . getAllByRole ( 'columnheader' ) ;
3255
3341
expect ( columnheaders ) . toHaveLength ( 3 ) ;
3342
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3256
3343
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'descending' ) ;
3257
3344
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3258
3345
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3346
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3347
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3348
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3349
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3259
3350
3260
3351
triggerPress ( columnheaders [ 0 ] ) ;
3261
3352
@@ -3288,6 +3379,11 @@ describe('TableView', function () {
3288
3379
expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-sort' , 'ascending' ) ;
3289
3380
expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-sort' , 'none' ) ;
3290
3381
expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-sort' ) ;
3382
+ expect ( columnheaders [ 0 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3383
+ expect ( document . getElementById ( columnheaders [ 0 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3384
+ expect ( columnheaders [ 1 ] ) . toHaveAttribute ( 'aria-describedby' ) ;
3385
+ expect ( document . getElementById ( columnheaders [ 1 ] . getAttribute ( 'aria-describedby' ) ) ) . toHaveTextContent ( 'sortable column' ) ;
3386
+ expect ( columnheaders [ 2 ] ) . not . toHaveAttribute ( 'aria-describedby' ) ;
3291
3387
3292
3388
triggerPress ( columnheaders [ 1 ] ) ;
3293
3389
0 commit comments