11import { attrId } from "@mendix/widget-plugin-test-utils" ;
2+ import { makeObservable , observable } from "mobx" ;
23import { SortStoreHost } from "../stores/SortStoreHost" ;
34import { ObservableSortStore , SortInstruction } from "../types/store" ;
45
@@ -45,12 +46,6 @@ describe("SortStoreHost", () => {
4546 [ attrId ( "attr2" ) , "desc" ]
4647 ] ) ;
4748 } ) ;
48-
49- it ( "should return empty array after unobserving" , ( ) => {
50- sortStoreHost . observe ( mockStore ) ;
51- sortStoreHost . unobserve ( ) ;
52- expect ( sortStoreHost . sortOrder ) . toEqual ( [ ] ) ;
53- } ) ;
5449 } ) ;
5550
5651 describe ( "observe" , ( ) => {
@@ -76,10 +71,13 @@ describe("SortStoreHost", () => {
7671 } ) ;
7772
7873 describe ( "unobserve" , ( ) => {
79- it ( "should clear the internal store" , ( ) => {
74+ it ( "should not clear the internal store" , ( ) => {
8075 sortStoreHost . observe ( mockStore ) ;
8176 sortStoreHost . unobserve ( ) ;
82- expect ( sortStoreHost . sortOrder ) . toEqual ( [ ] ) ;
77+ expect ( sortStoreHost . sortOrder ) . toEqual ( [
78+ [ attrId ( "attr1" ) , "asc" ] ,
79+ [ attrId ( "attr2" ) , "desc" ]
80+ ] ) ;
8381 } ) ;
8482
8583 it ( "should be safe to call when no store is observed" , ( ) => {
@@ -200,20 +198,25 @@ describe("SortStoreHost", () => {
200198 expect ( sortStoreHost . usedBy ) . toBe ( "widget1" ) ;
201199
202200 sortStoreHost . unobserve ( ) ;
203- expect ( sortStoreHost . sortOrder ) . toEqual ( [ ] ) ;
201+ expect ( sortStoreHost . sortOrder ) . toEqual ( [
202+ [ attrId ( "attr1" ) , "asc" ] ,
203+ [ attrId ( "attr2" ) , "desc" ]
204+ ] ) ;
204205 expect ( sortStoreHost . usedBy ) . toBe ( "widget1" ) ; // Lock should remain
205206
206207 unlock ( ) ;
207208 expect ( sortStoreHost . usedBy ) . toBeNull ( ) ;
208209 } ) ;
209210
210211 it ( "should handle store changes after observation" , ( ) => {
211- const mutableStore = {
212- sortOrder : [ [ attrId ( "attr1" ) , "asc" ] ] as SortInstruction [ ] ,
213- setSortOrder : jest . fn ( ) ,
214- toJSON : jest . fn ( ) ,
215- fromJSON : jest . fn ( )
216- } ;
212+ const mutableStore = makeObservable (
213+ {
214+ sortOrder : [ [ attrId ( "attr1" ) , "asc" ] ] as SortInstruction [ ]
215+ } ,
216+ {
217+ sortOrder : observable . ref
218+ }
219+ ) ;
217220
218221 sortStoreHost . observe ( mutableStore ) ;
219222 expect ( sortStoreHost . sortOrder ) . toEqual ( [ [ attrId ( "attr1" ) , "asc" ] ] ) ;
0 commit comments