Skip to content

Commit

Permalink
Merge pull request #1237 from searchspring/cookie-tests
Browse files Browse the repository at this point in the history
Toolbox Cookies Test
  • Loading branch information
korgon authored Jan 8, 2025
2 parents 1490979 + 6d88abc commit c6ea22a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/snap-toolbox/src/cookies/cookies.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit c6ea22a

Please sign in to comment.