axios-axios/tests/browser/cookies.browser.test.js
Jay d905b7598d
refactor: refresh test suite to be modernised (#7489)
* chore: port karma tests

* chore: port karma tests

* chore: port karma tests

* chore: tests

* chore: tests

* chore: tests

* chore: fix issues with port collisions

* refactor: utils tests

* refactor: utils tests

* refactor: utils tests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: tests to vitests

* refactor: ci

* chore: install pw deps

* chore: fixx ai feedback

* chore: wip compatability tests

* chore: wip compatability tests

* chore: wip compatability tests

* refactor: wip smoke

* chore: smoke test run

* chore: update unzip

* chore: update testing

* chore: update testing

* chore: update testing

* chore: update testing

* chore: update testing

* chore: skip tests that cannot run on node 16 and lower

* chore: fix 16x under tests

* chore: rest of tests

* fix: functions and runs

* feat: added tests for esm smoke

* feat: added smoke

* chore: ignore ai gen plans

* chore: ci fixes

* chore: fix small p2s
2026-03-12 15:27:09 +02:00

57 lines
1.3 KiB
JavaScript

import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import cookies from '../../lib/helpers/cookies.js';
const clearAllCookies = () => {
const expiry = new Date(Date.now() - 86400000).toUTCString();
for (const cookie of document.cookie.split(';')) {
const name = cookie.split('=')[0].trim();
if (!name) {
continue;
}
// Clear both default-path and root-path cookies for the same key.
document.cookie = `${name}=; expires=${expiry}`;
document.cookie = `${name}=; expires=${expiry}; path=/`;
}
};
describe('helpers::cookies (vitest browser)', () => {
beforeEach(() => {
clearAllCookies();
});
afterEach(() => {
clearAllCookies();
});
it('writes cookies', () => {
cookies.write('foo', 'baz');
expect(document.cookie).toBe('foo=baz');
});
it('reads cookies', () => {
cookies.write('foo', 'abc');
cookies.write('bar', 'def');
expect(cookies.read('foo')).toBe('abc');
expect(cookies.read('bar')).toBe('def');
});
it('removes cookies', () => {
cookies.write('foo', 'bar');
cookies.remove('foo');
expect(cookies.read('foo')).toBeNull();
});
it('uri encodes values', () => {
cookies.write('foo', 'bar baz%');
expect(document.cookie).toBe('foo=bar%20baz%25');
});
});