diff --git a/packages/snap-toolbox/src/cookies/cookies.test.ts b/packages/snap-toolbox/src/cookies/cookies.test.ts new file mode 100644 index 000000000..72e1f4f9f --- /dev/null +++ b/packages/snap-toolbox/src/cookies/cookies.test.ts @@ -0,0 +1,49 @@ +import { cookies } from './cookies'; + +describe('cookies', () => { + afterEach(() => { + // clear all cookies + document.cookie.split(';').forEach(function (c) { + document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); + }); + }); + + it('should set and get a cookie', () => { + cookies.set('testCookie', 'testValue'); + expect(cookies.get('testCookie')).toBe('testValue'); + }); + + it('should handle special characters in cookie values', () => { + const specialValue = 'test=value with spaces&special@chars'; + cookies.set('testCookie', specialValue); + expect(cookies.get('testCookie')).toBe(specialValue); + }); + + it('should unset cookie', () => { + cookies.set('testCookie', 'testValue'); + cookies.unset('testCookie'); + expect(cookies.get('testCookie')).toBe(''); + }); + + it('should unset cookie with domain', () => { + cookies.set('testCookie', 'testValue', undefined, undefined, 'example.com'); + cookies.unset('testCookie', 'example.com'); + expect(cookies.get('testCookie')).toBe(''); + }); + + it('should return empty string for non-existent cookie', () => { + expect(cookies.get('nonExistentCookie')).toBe(''); + }); + + it('should properly URI encode values when setting cookies', () => { + const valueWithSpecialChars = '?key=value&something=other;stuff=here'; + cookies.set('testCookie', valueWithSpecialChars); + expect(document.cookie).toContain(encodeURIComponent(valueWithSpecialChars)); + }); + + it('should properly URI decode values when getting cookies', () => { + const valueWithSpecialChars = '?key=value&something=other;stuff=here'; + document.cookie = `testCookie=${encodeURIComponent(valueWithSpecialChars)}`; + expect(cookies.get('testCookie')).toBe(valueWithSpecialChars); + }); +});