1
1
import Draggable from '../table/__mocks__/vuedraggable'
2
2
import { vi } from 'vitest'
3
- import { fireEvent , render } from '@testing-library/vue'
4
- import { ref } from 'vue-demi'
3
+ import {
4
+ render ,
5
+ fireEvent ,
6
+ queryByTestId ,
7
+ } from '@testing-library/vue'
8
+ import { nextTick , ref } from 'vue-demi'
5
9
import { defineTable } from '../table'
6
10
import Table from './TableStatic.vue'
7
11
@@ -39,13 +43,10 @@ const items = ref([
39
43
40
44
const sortableFields = defineTable ( [
41
45
{ key : 'id' } ,
42
- {
43
- key : 'name' ,
44
- sortable : true ,
45
- } ,
46
+ { key : 'name' } ,
46
47
{
47
48
key : 'gender' ,
48
- sortable : true ,
49
+ sortable : false ,
49
50
} ,
50
51
{
51
52
key : 'age' ,
@@ -358,15 +359,13 @@ it('should able to select all items (except disable one) in variant static', asy
358
359
} )
359
360
360
361
it ( 'should X field header have sortable class if have `sortable` property with `true` value & `sortable` prop is provided' , ( ) => {
361
- const items = ref ( [ ] )
362
- items . value = generateItems ( )
363
-
362
+ const items = ref ( generateItems ( ) )
364
363
const screen = render ( {
365
364
components : { Table } ,
366
365
template : `
367
366
<Table
368
367
:fields="sortableFields"
369
- v-model :items="items"
368
+ :items="items"
370
369
sortable
371
370
/>` ,
372
371
setup ( ) {
@@ -379,181 +378,99 @@ it('should X field header have sortable class if have `sortable` property with `
379
378
380
379
const heads = screen . queryAllByTestId ( 'table-static-header' )
381
380
382
- expect ( heads . at ( 0 ) ) . not . toHaveClass ( 'table-static__header--sortable' )
383
- expect ( heads . at ( 1 ) ) . toHaveClass ( 'table-static__header--sortable' )
381
+ expect ( heads . at ( 0 ) ) . toHaveClass ( 'table-static__header--sortable' )
382
+ expect ( heads . at ( 2 ) ) . not . toHaveClass ( 'table-static__header--sortable' )
384
383
} )
385
384
386
- it ( 'should X field header have caret up/down icon when sort by field' , async ( ) => {
387
- const items = ref ( [ ] )
388
- items . value = generateItems ( )
389
-
385
+ it ( 'should able modify sort by header using v-model:sort-by' , async ( ) => {
386
+ const items = ref ( generateItems ( ) )
387
+ const sortBy = ref ( { } )
390
388
const screen = render ( {
391
389
components : { Table } ,
392
390
template : `
393
391
<Table
392
+ v-model:sort-by="sortBy"
394
393
:fields="sortableFields"
395
- v-model:items="items"
396
- sortable
397
- />` ,
398
- setup ( ) {
399
- return {
400
- sortableFields,
401
- items,
402
- }
403
- } ,
404
- } )
405
-
406
- const heads = screen . queryAllByTestId ( 'table-static-header' )
407
-
408
- await fireEvent . click ( heads . at ( 1 ) )
409
-
410
- const sortUp = heads . at ( 1 ) . querySelector ( '.table-static__header__sort-up' )
411
-
412
- expect ( sortUp ) . toBeInTheDocument ( )
413
-
414
- await fireEvent . click ( heads . at ( 1 ) )
415
-
416
- const sortDown = heads . at ( 1 ) . querySelector ( '.table-static__header__sort-down' )
417
-
418
- expect ( sortDown ) . toBeInTheDocument ( )
419
- expect ( sortUp ) . not . toBeInTheDocument ( )
420
-
421
- await fireEvent . click ( heads . at ( 1 ) )
422
-
423
- expect ( sortDown ) . not . toBeInTheDocument ( )
424
- } )
425
-
426
- it ( 'should able to sort by field if `sortable` prop is provided' , async ( ) => {
427
- const items = ref ( [ ] )
428
- items . value = generateItems ( )
429
-
430
- const sorts = ref ( [ ] )
431
-
432
- const screen = render ( {
433
- components : { Table } ,
434
- template : `
435
- <Table
436
- :fields="sortableFields"
437
- v-model:items="items"
394
+ :items="items"
438
395
sortable
439
- @sort="sorts = $event"
440
396
/>` ,
441
397
setup ( ) {
442
398
return {
443
399
sortableFields,
444
400
items,
445
- sorts ,
401
+ sortBy ,
446
402
}
447
403
} ,
448
404
} )
449
405
450
- expect ( sorts . value ) . toHaveLength ( 0 )
451
-
452
406
const heads = screen . queryAllByTestId ( 'table-static-header' )
453
407
454
- await fireEvent . click ( heads . at ( 1 ) )
408
+ expect ( heads . at ( 0 ) ) . toHaveClass ( 'table-static__header--sortable' )
409
+ expect ( heads . at ( 2 ) ) . not . toHaveClass ( 'table-static__header--sortable' )
455
410
456
- expect ( items . value . at ( 0 ) . name ) . toStrictEqual ( 'Andi' )
457
- expect ( sorts . value ) . toHaveLength ( 1 )
411
+ await fireEvent . click ( heads . at ( 0 ) )
458
412
459
- await fireEvent . click ( heads . at ( 1 ) )
413
+ const icon = queryByTestId ( heads [ 0 ] , 'table-static-header-sort' )
460
414
461
- expect ( items . value . at ( 0 ) . name ) . toStrictEqual ( 'Jane' )
415
+ expect ( icon ) . toHaveAttribute ( 'active' , 'asc' )
416
+ expect ( sortBy . value ) . toStrictEqual ( { id : 'asc' } )
462
417
463
- await fireEvent . click ( heads . at ( 1 ) )
418
+ await fireEvent . click ( heads . at ( 0 ) )
464
419
465
- expect ( items . value . at ( 0 ) . name ) . toStrictEqual ( 'Dora ')
466
- } )
420
+ expect ( icon ) . toHaveAttribute ( 'active' , 'desc ')
421
+ expect ( sortBy . value ) . toStrictEqual ( { id : 'desc' } )
467
422
468
- it ( 'should be not sort by field if field not `sortable` eventhough prop `sortable` is provided when render table' , async ( ) => {
469
- const fields = defineTable ( [ { key : 'id' } , { key : 'name' } ] )
470
- const items = ref ( [
471
- {
472
- id : 1 ,
473
- name : 'Jane' ,
474
- } ,
475
- {
476
- id : 2 ,
477
- name : 'Tarjono' ,
478
- } ,
479
- ] )
480
- const sorts = ref ( [ ] )
481
-
482
- const screen = render ( {
483
- components : { Table } ,
484
- template : `
485
- <Table
486
- :fields="fields"
487
- v-model:items="items"
488
- sortable
489
- @sort="sorts = $event"
490
- />` ,
491
- setup ( ) {
492
- return {
493
- fields,
494
- items,
495
- sorts,
496
- }
497
- } ,
498
- } )
423
+ await fireEvent . click ( heads . at ( 0 ) )
499
424
500
- const heads = screen . queryAllByTestId ( 'table-static-header' )
425
+ expect ( icon ) . not . toHaveAttribute ( 'active' , 'desc' )
426
+ expect ( sortBy . value ) . toStrictEqual ( { id : undefined } )
501
427
502
- await fireEvent . click ( heads . at ( 1 ) )
428
+ sortBy . value = { name : 'desc' }
429
+ await nextTick ( )
503
430
504
- const sortUp = heads . at ( 1 ) . querySelector ( '. table-static__header__sort-up ')
431
+ const icon2 = queryByTestId ( heads [ 1 ] , ' table-static-header-sort ')
505
432
506
- expect ( sortUp ) . not . toBeInTheDocument ( )
507
- expect ( sorts . value ) . toHaveLength ( 0 )
433
+ expect ( icon ) . not . toHaveAttribute ( 'active' , 'asc' )
434
+ expect ( icon2 ) . toHaveAttribute ( 'active' , 'desc' )
508
435
} )
509
436
510
- it ( 'should able to sort by field if `sortable` prop is provided' , async ( ) => {
511
- const items = ref ( [ ] )
512
- items . value = generateItems ( )
513
-
514
- const sorts = ref ( [ ] )
515
-
437
+ it ( 'should have multiple value if sortable set to `multiple`' , async ( ) => {
438
+ const items = ref ( generateItems ( ) )
439
+ const sortBy = ref ( { } )
516
440
const screen = render ( {
517
441
components : { Table } ,
518
442
template : `
519
443
<Table
444
+ v-model:sort-by="sortBy"
520
445
:fields="sortableFields"
521
- v-model:items="items"
522
- sortable
523
- sortable-asyncronous
524
- @sort="sorts = $event"
446
+ :items="items"
447
+ sortable="multiple"
525
448
/>` ,
526
449
setup ( ) {
527
450
return {
528
451
sortableFields,
529
452
items,
530
- sorts ,
453
+ sortBy ,
531
454
}
532
455
} ,
533
456
} )
534
457
535
- expect ( sorts . value ) . toHaveLength ( 0 )
536
-
537
458
const heads = screen . queryAllByTestId ( 'table-static-header' )
538
459
539
- await fireEvent . click ( heads . at ( 2 ) ) // sort gender to ASC
540
- expect ( sorts . value ) . toHaveLength ( 1 )
541
- expect ( sorts . value . at ( 0 ) ) . toStrictEqual ( { key : 'gender' , value : 'asc' } )
460
+ expect ( heads . at ( 0 ) ) . toHaveClass ( 'table-static__header--sortable' )
461
+ expect ( heads . at ( 2 ) ) . not . toHaveClass ( 'table-static__header--sortable' )
542
462
543
- await fireEvent . click ( heads . at ( 3 ) ) // sort age to ASC
544
- expect ( sorts . value ) . toHaveLength ( 2 )
545
- expect ( sorts . value . at ( 1 ) ) . toStrictEqual ( { key : 'age' , value : 'asc' } )
463
+ await fireEvent . click ( heads . at ( 0 ) )
546
464
547
- await fireEvent . click ( heads . at ( 2 ) ) // sort gender to DESC
548
- expect ( sorts . value ) . toHaveLength ( 2 )
549
- expect ( sorts . value . at ( 0 ) ) . toStrictEqual ( { key : 'gender' , value : 'desc' } )
465
+ const icon = queryByTestId ( heads [ 0 ] , 'table-static-header-sort' )
466
+ const icon2 = queryByTestId ( heads [ 1 ] , 'table-static-header-sort' )
550
467
551
- await fireEvent . click ( heads . at ( 2 ) ) // clear gender sort
552
- expect ( sorts . value ) . toHaveLength ( 1 )
468
+ expect ( icon ) . toHaveAttribute ( 'active' , 'asc' )
469
+ expect ( sortBy . value ) . toStrictEqual ( { id : 'asc' } )
553
470
554
- await fireEvent . click ( heads . at ( 3 ) ) // sort age to DESC
555
- expect ( sorts . value . at ( 0 ) ) . toStrictEqual ( { key : 'age' , value : 'desc' } )
471
+ await fireEvent . click ( heads . at ( 1 ) )
556
472
557
- await fireEvent . click ( heads . at ( 3 ) ) // clear age sort
558
- expect ( sorts . value ) . toHaveLength ( 0 )
473
+ expect ( icon ) . toHaveAttribute ( 'active' , 'asc' )
474
+ expect ( icon2 ) . toHaveAttribute ( 'active' , 'asc' )
475
+ expect ( sortBy . value ) . toStrictEqual ( { id : 'asc' , name : 'asc' } )
559
476
} )
0 commit comments