@@ -14,13 +14,19 @@ describe('NestedSort', () => {
1414 document . body . innerHTML = `<div id="${ DYNAMIC_LIST_WRAPPER_ID } "></div>`
1515 } )
1616
17- describe ( 'upon initiation' , ( ) => {
18- describe ( 'list items attributes' , ( ) => {
19- it ( 'should add the draggable attribute to all the list items' , ( ) => {
20- const ns = initDataDrivenList ( )
21- Array . from ( ns . getSortableList ( ) . getElementsByTagName ( 'li' ) ) . forEach ( li => {
22- expect ( li . getAttribute ( 'draggable' ) ) . toBe ( 'true' )
23- } )
17+ describe ( 'upon instantiation' , ( ) => {
18+ it ( 'should not invoke the initDragAndDrop method when the init option is falsy' , ( ) => {
19+ const spy = jest . spyOn ( NestedSort . prototype , 'initDragAndDrop' )
20+ initDataDrivenList ( { init : false } )
21+ expect ( spy ) . not . toHaveBeenCalled ( )
22+ } )
23+
24+ it ( 'should invoke the initDragAndDrop method when the init option is true or undefined' , ( ) => {
25+ [ undefined , true ] . forEach ( init => {
26+ const spy = jest . spyOn ( NestedSort . prototype , 'initDragAndDrop' )
27+ initDataDrivenList ( { init } )
28+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
29+ spy . mockRestore ( )
2430 } )
2531 } )
2632 } )
@@ -905,6 +911,72 @@ describe('NestedSort', () => {
905911 } )
906912 } )
907913
914+ describe ( 'initDragAndDrop method' , ( ) => {
915+ describe ( 'when drag and drop is already initialised' , ( ) => {
916+ let ns
917+ beforeEach ( ( ) => {
918+ ns = initDataDrivenList ( { init : true } )
919+ } )
920+
921+ it ( 'should go through the early return' , ( ) => {
922+ const spy1 = jest . spyOn ( ns , 'toggleListEventListeners' )
923+ const spy2 = jest . spyOn ( ns , 'initPlaceholderList' )
924+ const spy3 = jest . spyOn ( ns , 'toggleListItemAttributes' )
925+
926+ ns . initDragAndDrop ( )
927+
928+ expect ( spy1 ) . not . toHaveBeenCalled ( )
929+ expect ( spy2 ) . not . toHaveBeenCalled ( )
930+ expect ( spy3 ) . not . toHaveBeenCalled ( )
931+ } )
932+ } )
933+
934+ describe ( 'when drag and drop is NOT initialised' , ( ) => {
935+ let ns
936+ beforeEach ( ( ) => {
937+ ns = initDataDrivenList ( { init : false } )
938+ } )
939+
940+ it ( 'should invoke the toggleListEventListeners method with no arguments' , ( ) => {
941+ const spy = jest . spyOn ( ns , 'toggleListEventListeners' )
942+ ns . initDragAndDrop ( )
943+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
944+ expect ( spy ) . toHaveBeenCalledWith ( )
945+ } )
946+
947+ it ( 'should invoke the initPlaceholderList method with no arguments' , ( ) => {
948+ const spy = jest . spyOn ( ns , 'initPlaceholderList' )
949+ ns . initDragAndDrop ( )
950+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
951+ expect ( spy ) . toHaveBeenCalledWith ( )
952+ } )
953+
954+ it ( 'should invoke the toggleListItemAttributes method with no arguments' , ( ) => {
955+ const spy = jest . spyOn ( ns , 'toggleListItemAttributes' )
956+ ns . initDragAndDrop ( )
957+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
958+ expect ( spy ) . toHaveBeenCalledWith ( )
959+ } )
960+
961+ it ( 'should set the initialised property value to true' , ( ) => {
962+ expect ( ns . initialised ) . toBe ( false )
963+ ns . initDragAndDrop ( )
964+ expect ( ns . initialised ) . toBe ( true )
965+ } )
966+ } )
967+ } )
968+
969+ describe ( 'init method' , ( ) => {
970+ it ( 'should invoke the initDragAndDrop' , ( ) => {
971+ const spy = jest . spyOn ( NestedSort . prototype , 'initDragAndDrop' )
972+ const ns = initDataDrivenList ( { init : false } )
973+
974+ expect ( spy ) . not . toHaveBeenCalled ( )
975+ ns . init ( )
976+ expect ( spy ) . toHaveBeenCalledTimes ( 1 )
977+ } )
978+ } )
979+
908980 describe ( 'destroy method' , ( ) => {
909981 it ( 'should invoke the toggleListEventListeners method with correct arguments' , ( ) => {
910982 const ns = initDataDrivenList ( )
@@ -925,5 +997,39 @@ describe('NestedSort', () => {
925997 expect ( spy ) . toHaveBeenCalledTimes ( 1 )
926998 expect ( spy ) . toHaveBeenCalledWith ( false )
927999 } )
1000+
1001+ it ( 'should set the initialised property value to false' , ( ) => {
1002+ const ns = initDataDrivenList ( { init : true } )
1003+ ns . destroy ( )
1004+ expect ( ns . initialised ) . toBe ( false )
1005+ } )
1006+ } )
1007+
1008+ describe ( 'toggleListItemAttributes method' , ( ) => {
1009+ it ( 'should set the draggable attribute value to true on all the list items when no argument is passed to it' , ( ) => {
1010+ const ns = initDataDrivenList ( { init : false } )
1011+ Array . from ( ns . getSortableList ( ) . getElementsByTagName ( 'li' ) ) . forEach ( li => {
1012+ expect ( li . getAttribute ( 'draggable' ) ) . toBe ( null )
1013+ } ) // to make sure instantiation has not set the attribute value
1014+
1015+ ns . toggleListItemAttributes ( )
1016+
1017+ Array . from ( ns . getSortableList ( ) . getElementsByTagName ( 'li' ) ) . forEach ( li => {
1018+ expect ( li . getAttribute ( 'draggable' ) ) . toBe ( 'true' )
1019+ } )
1020+ } )
1021+
1022+ it ( 'should set the draggable attribute value to false on all the list items when the passed argument equals false' , ( ) => {
1023+ const ns = initDataDrivenList ( { init : true } )
1024+ Array . from ( ns . getSortableList ( ) . getElementsByTagName ( 'li' ) ) . forEach ( li => {
1025+ expect ( li . getAttribute ( 'draggable' ) ) . toBe ( 'true' )
1026+ } ) // to make sure instantiation has set the attribute value to true
1027+
1028+ ns . toggleListItemAttributes ( false )
1029+
1030+ Array . from ( ns . getSortableList ( ) . getElementsByTagName ( 'li' ) ) . forEach ( li => {
1031+ expect ( li . getAttribute ( 'draggable' ) ) . toBe ( 'false' )
1032+ } )
1033+ } )
9281034 } )
9291035} )
0 commit comments