-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from searchspring/cookie-tests
Toolbox Cookies Test
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |