@@ -189,33 +189,33 @@ describe('<LibraryAuthoringPage />', () => {
189
189
axiosMock . onGet ( getContentLibraryApiUrl ( libraryData . id ) ) . reply ( 200 , libraryData ) ;
190
190
191
191
const {
192
- getByRole, getByText, queryByText, findByText, findAllByText,
192
+ getByRole, getAllByText , getByText, queryByText, findByText, findAllByText,
193
193
} = render ( < RootWrapper /> ) ;
194
194
195
- // Ensure the search endpoint is called:
196
- // Call 1: To fetch searchable/filterable/sortable library data
197
- // Call 2: To fetch the recently modified components only
198
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
195
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
199
196
200
197
expect ( await findByText ( 'Content library' ) ) . toBeInTheDocument ( ) ;
201
198
expect ( ( await findAllByText ( libraryData . title ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
202
199
203
200
expect ( queryByText ( 'You have not added any content to this library yet.' ) ) . not . toBeInTheDocument ( ) ;
204
201
205
- expect ( getByText ( 'Recently Modified' ) ) . toBeInTheDocument ( ) ;
202
+ // "Recently Modified" header + sort shown
203
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ;
206
204
expect ( getByText ( 'Collections (0)' ) ) . toBeInTheDocument ( ) ;
207
205
expect ( getByText ( 'Components (6)' ) ) . toBeInTheDocument ( ) ;
208
206
expect ( ( await findAllByText ( 'Test HTML Block' ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
209
207
210
208
// Navigate to the components tab
211
209
fireEvent . click ( getByRole ( 'tab' , { name : 'Components' } ) ) ;
212
- expect ( queryByText ( 'Recently Modified' ) ) . not . toBeInTheDocument ( ) ;
210
+ // "Recently Modified" default sort shown
211
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 1 ) ;
213
212
expect ( queryByText ( 'Collections (0)' ) ) . not . toBeInTheDocument ( ) ;
214
213
expect ( queryByText ( 'Components (6)' ) ) . not . toBeInTheDocument ( ) ;
215
214
216
215
// Navigate to the collections tab
217
216
fireEvent . click ( getByRole ( 'tab' , { name : 'Collections' } ) ) ;
218
- expect ( queryByText ( 'Recently Modified' ) ) . not . toBeInTheDocument ( ) ;
217
+ // "Recently Modified" default sort shown
218
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 1 ) ;
219
219
expect ( queryByText ( 'Collections (0)' ) ) . not . toBeInTheDocument ( ) ;
220
220
expect ( queryByText ( 'Components (6)' ) ) . not . toBeInTheDocument ( ) ;
221
221
expect ( queryByText ( 'There are 6 components in this library' ) ) . not . toBeInTheDocument ( ) ;
@@ -224,7 +224,8 @@ describe('<LibraryAuthoringPage />', () => {
224
224
// Go back to Home tab
225
225
// This step is necessary to avoid the url change leak to other tests
226
226
fireEvent . click ( getByRole ( 'tab' , { name : 'Home' } ) ) ;
227
- expect ( getByText ( 'Recently Modified' ) ) . toBeInTheDocument ( ) ;
227
+ // "Recently Modified" header + sort shown
228
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ;
228
229
expect ( getByText ( 'Collections (0)' ) ) . toBeInTheDocument ( ) ;
229
230
expect ( getByText ( 'Components (6)' ) ) . toBeInTheDocument ( ) ;
230
231
} ) ;
@@ -239,10 +240,7 @@ describe('<LibraryAuthoringPage />', () => {
239
240
expect ( await findByText ( 'Content library' ) ) . toBeInTheDocument ( ) ;
240
241
expect ( ( await findAllByText ( libraryData . title ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
241
242
242
- // Ensure the search endpoint is called:
243
- // Call 1: To fetch searchable/filterable/sortable library data
244
- // Call 2: To fetch the recently modified components only
245
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
243
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
246
244
247
245
expect ( getByText ( 'You have not added any content to this library yet.' ) ) . toBeInTheDocument ( ) ;
248
246
} ) ;
@@ -304,16 +302,13 @@ describe('<LibraryAuthoringPage />', () => {
304
302
expect ( await findByText ( 'Content library' ) ) . toBeInTheDocument ( ) ;
305
303
expect ( ( await findAllByText ( libraryData . title ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
306
304
307
- // Ensure the search endpoint is called:
308
- // Call 1: To fetch searchable/filterable/sortable library data
309
- // Call 2: To fetch the recently modified components only
310
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
305
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
311
306
312
307
fireEvent . change ( getByRole ( 'searchbox' ) , { target : { value : 'noresults' } } ) ;
313
308
314
309
// Ensure the search endpoint is called again, only once more since the recently modified call
315
310
// should not be impacted by the search
316
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 3 , searchEndpoint , 'post' ) ; } ) ;
311
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
317
312
318
313
expect ( getByText ( 'No matching components found in this library.' ) ) . toBeInTheDocument ( ) ;
319
314
@@ -396,15 +391,13 @@ describe('<LibraryAuthoringPage />', () => {
396
391
getByRole, getByText, queryByText, getAllByText, findAllByText,
397
392
} = render ( < RootWrapper /> ) ;
398
393
399
- // Ensure the search endpoint is called:
400
- // Call 1: To fetch searchable/filterable/sortable library data
401
- // Call 2: To fetch the recently modified components only
402
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
394
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
403
395
404
396
expect ( getByText ( 'Content library' ) ) . toBeInTheDocument ( ) ;
405
397
expect ( ( await findAllByText ( libraryData . title ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
406
398
407
- await waitFor ( ( ) => { expect ( getByText ( 'Recently Modified' ) ) . toBeInTheDocument ( ) ; } ) ;
399
+ // "Recently Modified" header + sort shown
400
+ await waitFor ( ( ) => { expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ; } ) ;
408
401
expect ( getByText ( 'Collections (0)' ) ) . toBeInTheDocument ( ) ;
409
402
expect ( getByText ( 'Components (6)' ) ) . toBeInTheDocument ( ) ;
410
403
expect ( getAllByText ( 'Test HTML Block' ) [ 0 ] ) . toBeInTheDocument ( ) ;
@@ -416,15 +409,17 @@ describe('<LibraryAuthoringPage />', () => {
416
409
417
410
// Clicking on "View All" button should navigate to the Components tab
418
411
fireEvent . click ( getByText ( 'View All' ) ) ;
419
- expect ( queryByText ( 'Recently Modified' ) ) . not . toBeInTheDocument ( ) ;
412
+ // "Recently Modified" default sort shown
413
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 1 ) ;
420
414
expect ( queryByText ( 'Collections (0)' ) ) . not . toBeInTheDocument ( ) ;
421
415
expect ( queryByText ( 'Components (6)' ) ) . not . toBeInTheDocument ( ) ;
422
416
expect ( getAllByText ( 'Test HTML Block' ) [ 0 ] ) . toBeInTheDocument ( ) ;
423
417
424
418
// Go back to Home tab
425
419
// This step is necessary to avoid the url change leak to other tests
426
420
fireEvent . click ( getByRole ( 'tab' , { name : 'Home' } ) ) ;
427
- expect ( getByText ( 'Recently Modified' ) ) . toBeInTheDocument ( ) ;
421
+ // "Recently Modified" header + sort shown
422
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ;
428
423
expect ( getByText ( 'Collections (0)' ) ) . toBeInTheDocument ( ) ;
429
424
expect ( getByText ( 'Components (6)' ) ) . toBeInTheDocument ( ) ;
430
425
} ) ;
@@ -438,15 +433,13 @@ describe('<LibraryAuthoringPage />', () => {
438
433
getByText, queryByText, getAllByText, findAllByText,
439
434
} = render ( < RootWrapper /> ) ;
440
435
441
- // Ensure the search endpoint is called:
442
- // Call 1: To fetch searchable/filterable/sortable library data
443
- // Call 2: To fetch the recently modified components only
444
- await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 2 , searchEndpoint , 'post' ) ; } ) ;
436
+ await waitFor ( ( ) => { expect ( fetchMock ) . toHaveFetchedTimes ( 1 , searchEndpoint , 'post' ) ; } ) ;
445
437
446
438
expect ( getByText ( 'Content library' ) ) . toBeInTheDocument ( ) ;
447
439
expect ( ( await findAllByText ( libraryData . title ) ) [ 0 ] ) . toBeInTheDocument ( ) ;
448
440
449
- await waitFor ( ( ) => { expect ( getByText ( 'Recently Modified' ) ) . toBeInTheDocument ( ) ; } ) ;
441
+ // "Recently Modified" header + sort shown
442
+ await waitFor ( ( ) => { expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ; } ) ;
450
443
expect ( getByText ( 'Collections (0)' ) ) . toBeInTheDocument ( ) ;
451
444
expect ( getByText ( 'Components (2)' ) ) . toBeInTheDocument ( ) ;
452
445
expect ( getAllByText ( 'Test HTML Block' ) [ 0 ] ) . toBeInTheDocument ( ) ;
@@ -463,35 +456,49 @@ describe('<LibraryAuthoringPage />', () => {
463
456
fetchMock . post ( searchEndpoint , returnEmptyResult , { overwriteRoutes : true } ) ;
464
457
465
458
const {
466
- findByTitle, getAllByText, getByText , getByTitle,
459
+ findByTitle, getAllByText, getByRole , getByTitle,
467
460
} = render ( < RootWrapper /> ) ;
468
461
469
462
expect ( await findByTitle ( 'Sort search results' ) ) . toBeInTheDocument ( ) ;
470
463
471
- const testSortOption = ( async ( optionText , sortBy ) => {
472
- if ( optionText ) {
473
- fireEvent . click ( getByTitle ( 'Sort search results' ) ) ;
474
- fireEvent . click ( getByText ( optionText ) ) ;
475
- }
464
+ const testSortOption = ( async ( optionText , sortBy , isDefault ) => {
465
+ // Open the drop-down menu
466
+ fireEvent . click ( getByTitle ( 'Sort search results' ) ) ;
467
+
468
+ // Click the option with the given text
469
+ // Since the sort drop-down also shows the selected sort
470
+ // option in its toggle button, we need to make sure we're
471
+ // clicking on the last one found.
472
+ const options = getAllByText ( optionText ) ;
473
+ expect ( options . length ) . toBeGreaterThan ( 0 ) ;
474
+ fireEvent . click ( options [ options . length - 1 ] ) ;
475
+
476
+ // Did the search happen with the expected sort option?
476
477
const bodyText = sortBy ? `"sort":["${ sortBy } "]` : '"sort":[]' ;
477
- const searchText = sortBy ? `?sort=${ encodeURIComponent ( sortBy ) } ` : '' ;
478
478
await waitFor ( ( ) => {
479
479
expect ( fetchMock ) . toHaveBeenLastCalledWith ( searchEndpoint , {
480
480
body : expect . stringContaining ( bodyText ) ,
481
481
method : 'POST' ,
482
482
headers : expect . anything ( ) ,
483
483
} ) ;
484
484
} ) ;
485
+
486
+ // Is the sort option stored in the query string?
487
+ const searchText = isDefault ? '' : `?sort=${ encodeURIComponent ( sortBy ) } ` ;
485
488
expect ( window . location . search ) . toEqual ( searchText ) ;
489
+
490
+ // Is the selected sort option shown in the toggle button (if not default)
491
+ // as well as in the drop-down menu?
492
+ expect ( getAllByText ( optionText ) . length ) . toEqual ( isDefault ? 1 : 2 ) ;
486
493
} ) ;
487
494
488
- await testSortOption ( 'Title, A-Z' , 'display_name:asc' ) ;
489
- await testSortOption ( 'Title, Z-A' , 'display_name:desc' ) ;
490
- await testSortOption ( 'Newest' , 'created:desc' ) ;
491
- await testSortOption ( 'Oldest' , 'created:asc' ) ;
495
+ await testSortOption ( 'Title, A-Z' , 'display_name:asc' , false ) ;
496
+ await testSortOption ( 'Title, Z-A' , 'display_name:desc' , false ) ;
497
+ await testSortOption ( 'Newest' , 'created:desc' , false ) ;
498
+ await testSortOption ( 'Oldest' , 'created:asc' , false ) ;
492
499
493
500
// Sorting by Recently Published also excludes unpublished components
494
- await testSortOption ( 'Recently Published' , 'last_published:desc' ) ;
501
+ await testSortOption ( 'Recently Published' , 'last_published:desc' , false ) ;
495
502
await waitFor ( ( ) => {
496
503
expect ( fetchMock ) . toHaveBeenLastCalledWith ( searchEndpoint , {
497
504
body : expect . stringContaining ( 'last_published IS NOT NULL' ) ,
@@ -500,8 +507,22 @@ describe('<LibraryAuthoringPage />', () => {
500
507
} ) ;
501
508
} ) ;
502
509
503
- // Clearing filters clears the url search param and uses default sort
504
- fireEvent . click ( getAllByText ( 'Clear Filters' ) [ 0 ] ) ;
505
- await testSortOption ( '' , '' ) ;
510
+ // Re-selecting the previous sort option resets sort to default "Recently Modified"
511
+ await testSortOption ( 'Recently Published' , 'modified:desc' , true ) ;
512
+ expect ( getAllByText ( 'Recently Modified' ) . length ) . toEqual ( 2 ) ;
513
+
514
+ // Enter a keyword into the search box
515
+ const searchBox = getByRole ( 'searchbox' ) ;
516
+ fireEvent . change ( searchBox , { target : { value : 'words to find' } } ) ;
517
+
518
+ // Default sort option changes to "Most Relevant"
519
+ expect ( getAllByText ( 'Most Relevant' ) . length ) . toEqual ( 2 ) ;
520
+ await waitFor ( ( ) => {
521
+ expect ( fetchMock ) . toHaveBeenLastCalledWith ( searchEndpoint , {
522
+ body : expect . stringContaining ( '"sort":[]' ) ,
523
+ method : 'POST' ,
524
+ headers : expect . anything ( ) ,
525
+ } ) ;
526
+ } ) ;
506
527
} ) ;
507
528
} ) ;
0 commit comments