Sfoglia il codice sorgente

test: add tests for cookies utility function

satnaing 2 mesi fa
parent
commit
3362929498
1 ha cambiato i file con 35 aggiunte e 0 eliminazioni
  1. 35 0
      src/lib/cookies.test.ts

+ 35 - 0
src/lib/cookies.test.ts

@@ -0,0 +1,35 @@
+import { beforeEach, describe, expect, it } from 'vitest'
+import { getCookie, removeCookie, setCookie } from './cookies'
+
+describe('cookies', () => {
+  const uniqueName = () => `test_cookie_${Math.random().toString(36).slice(2)}`
+
+  beforeEach(() => {
+    for (const part of document.cookie.split(';')) {
+      const name = part.split('=')[0]?.trim()
+      if (name?.startsWith('test_cookie_')) {
+        removeCookie(name)
+      }
+    }
+  })
+
+  it('stores a value that can be read back', () => {
+    const name = uniqueName()
+    const value = 'hello-world'
+
+    setCookie(name, value)
+
+    expect(getCookie(name)).toBe(value)
+  })
+
+  it('clears a value so it is no longer readable', () => {
+    const name = uniqueName()
+
+    setCookie(name, 'x')
+    expect(getCookie(name)).toBe('x')
+
+    removeCookie(name)
+
+    expect(getCookie(name)).toBeUndefined()
+  })
+})