@@ -9,7 +9,7 @@ afterEach(() => {
99const testComponent = (
1010 cookieName = 'cookieName' ,
1111 cookieValue : any = '' ,
12- parseJson = false ,
12+ opts = { isParsing : false } as any ,
1313 onMount = true
1414) => ( {
1515 template : `
@@ -26,7 +26,7 @@ const testComponent = (
2626 setup ( ) {
2727 const { cookie, getCookie, setCookie, removeCookie } = useCookie (
2828 cookieName ,
29- parseJson ,
29+ opts ,
3030 onMount
3131 )
3232
@@ -75,7 +75,9 @@ describe('useCookie', () => {
7575 it ( 'should correctly get and set the parseToJson object' , async ( ) => {
7676 const cookieName = 'cookieName'
7777 const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
78- const wrapper = mount ( testComponent ( cookieName , cookieValue , true ) )
78+ const wrapper = mount (
79+ testComponent ( cookieName , cookieValue , { isParsing : true } )
80+ )
7981 wrapper . find ( '#setCookie' ) . trigger ( 'click' )
8082 await wrapper . vm . $nextTick ( )
8183 expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( cookieValue . value1 )
@@ -87,4 +89,85 @@ describe('useCookie', () => {
8789 expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( cookieValue . value1 )
8890 expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( cookieValue . value2 )
8991 } )
92+
93+ it ( 'should correctly get using the deserializer' , async ( ) => {
94+ const cookieName = 'cookieName'
95+ const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
96+ const deserializerVal = { value1 : 'gatto' , value2 : 'topo' }
97+ const wrapper = mount (
98+ testComponent ( cookieName , cookieValue , {
99+ isParsing : true ,
100+ deserializer : ( ) => deserializerVal
101+ } )
102+ )
103+ await wrapper . vm . $nextTick ( )
104+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( deserializerVal . value1 )
105+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( deserializerVal . value2 )
106+ } )
107+
108+ it ( 'should ignore deserializer when isParsing is false' , async ( ) => {
109+ const cookieName = 'cookieName'
110+ const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
111+ const deserializerVal = { value1 : 'gatto' , value2 : 'topo' }
112+ const wrapper = mount (
113+ testComponent ( cookieName , cookieValue , {
114+ isParsing : false ,
115+ deserializer : ( ) => deserializerVal
116+ } )
117+ )
118+ await wrapper . vm . $nextTick ( )
119+ expect ( wrapper . find ( '#cookie' ) . html ( ) ) . toContain ( cookieValue . value1 )
120+ expect ( wrapper . find ( '#cookie' ) . html ( ) ) . toContain ( cookieValue . value2 )
121+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . not . toContain (
122+ deserializerVal . value1
123+ )
124+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . not . toContain (
125+ deserializerVal . value2
126+ )
127+ } )
128+
129+ it ( 'should correctly set using the serializer' , async ( ) => {
130+ const cookieName = 'cookieName'
131+ const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
132+ const serializerVal = { value1 : 'testValue1+1' , value2 : 'testValue2+1' }
133+ const wrapper = mount (
134+ testComponent ( cookieName , cookieValue , {
135+ isParsing : true ,
136+ serializer : ( obj : any ) => ( {
137+ value1 : `${ obj . value1 } +1` ,
138+ value2 : `${ obj . value2 } +1`
139+ } )
140+ } )
141+ )
142+ wrapper . find ( '#setCookie' ) . trigger ( 'click' )
143+ wrapper . find ( '#getCookie' ) . trigger ( 'click' )
144+ await wrapper . vm . $nextTick ( )
145+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( serializerVal . value1 )
146+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . toContain ( serializerVal . value2 )
147+ } )
148+
149+ it ( 'should ignore serializer when isParsing is false' , async ( ) => {
150+ const cookieName = 'cookieName'
151+ const cookieValue = { value1 : 'testValue1' , value2 : 'testValue2' }
152+ const serializerVal = { value1 : 'testValue1+1' , value2 : 'testValue2+1' }
153+ const wrapper = mount (
154+ testComponent ( cookieName , cookieValue , {
155+ isParsing : false ,
156+ serializer : ( obj : any ) => ( {
157+ value1 : `${ obj . value1 } +1` ,
158+ value2 : `${ obj . value2 } +1`
159+ } )
160+ } )
161+ )
162+ wrapper . find ( '#setCookie' ) . trigger ( 'click' )
163+ wrapper . find ( '#getCookie' ) . trigger ( 'click' )
164+ await wrapper . vm . $nextTick ( )
165+ expect ( wrapper . find ( '#cookie' ) . html ( ) ) . toContain ( '[object Object]' )
166+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . not . toContain (
167+ serializerVal . value1
168+ )
169+ expect ( wrapper . find ( '#cookieJson' ) . html ( ) ) . not . toContain (
170+ serializerVal . value2
171+ )
172+ } )
90173} )
0 commit comments