From 6d88abc2a8389d6a3a8d33b6591b127a9088c12f Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 8 Jan 2025 10:22:42 -0700 Subject: [PATCH] test(toolbox): adding test for cookies that didn't yet exist --- .../snap-toolbox/src/cookies/cookies.test.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 packages/snap-toolbox/src/cookies/cookies.test.ts 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); + }); +});