-
Notifications
You must be signed in to change notification settings - Fork 151
upgrade: cookies package upgrade for Solid 2.0 #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davedbase
wants to merge
3
commits into
solidjs-community:next
Choose a base branch
from
davedbase:update/v2/cookies
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| --- | ||
| "@solid-primitives/cookies": major | ||
| --- | ||
|
|
||
| Migrate to Solid.js v2.0 (beta.13) | ||
|
|
||
| ## Breaking Changes | ||
|
|
||
| **Peer dependencies**: `solid-js@^2.0.0-beta.13` and `@solidjs/web@^2.0.0-beta.13` are now required. | ||
|
|
||
| - `isServer` and `getRequestEvent` are now imported from `@solidjs/web` (were `solid-js/web`) | ||
| - `createEffect` follows the split compute/apply pattern required by Solid 2.0 — the internal cookie-sync effect now separates reactive tracking from the `document.cookie` write | ||
|
|
||
| ## New | ||
|
|
||
| - Full test suite added: 19 browser tests and 6 SSR tests covering `parseCookie`, `getCookiesString`, `createServerCookie`, and `createUserTheme` |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,229 @@ | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { createRoot, flush } from "solid-js"; | ||
| import { parseCookie, getCookiesString, createServerCookie, createUserTheme } from "../src/index.js"; | ||
|
|
||
| beforeEach(() => { | ||
| document.cookie.split(";").forEach(c => { | ||
| const key = c.split("=")[0]!.trim(); | ||
| if (key) document.cookie = `${key}=;max-age=0`; | ||
| }); | ||
| }); | ||
|
|
||
| // ── parseCookie ─────────────────────────────────────────────────────────────── | ||
|
|
||
| describe("parseCookie", () => { | ||
| it("extracts a value by key", () => { | ||
| expect(parseCookie("foo=bar", "foo")).toBe("bar"); | ||
| }); | ||
|
|
||
| it("extracts a value from multiple cookies", () => { | ||
| expect(parseCookie("foo=bar; baz=qux", "baz")).toBe("qux"); | ||
| }); | ||
|
|
||
| it("returns undefined for a missing key", () => { | ||
| expect(parseCookie("foo=bar", "missing")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined for an empty cookie string", () => { | ||
| expect(parseCookie("", "foo")).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| // ── getCookiesString ────────────────────────────────────────────────────────── | ||
|
|
||
| describe("getCookiesString", () => { | ||
| it("returns document.cookie on the client", () => { | ||
| document.cookie = "ck_test=hello"; | ||
| expect(getCookiesString()).toContain("ck_test=hello"); | ||
| }); | ||
| }); | ||
|
|
||
| // ── createServerCookie ──────────────────────────────────────────────────────── | ||
|
|
||
| describe("createServerCookie", () => { | ||
| it("initializes signal from the current cookie value", () => { | ||
| document.cookie = "ck_init=world"; | ||
| const dispose = createRoot(dispose => { | ||
| const [cookie] = createServerCookie("ck_init"); | ||
| expect(cookie()).toBe("world"); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("initializes to undefined when the cookie is absent", () => { | ||
| const dispose = createRoot(dispose => { | ||
| const [cookie] = createServerCookie("ck_absent"); | ||
| expect(cookie()).toBeUndefined(); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("setter updates the signal value", () => { | ||
| document.cookie = "ck_set=before"; | ||
| const { setCookie, dispose } = createRoot(dispose => { | ||
| const [, setCookie] = createServerCookie("ck_set"); | ||
| return { setCookie, dispose }; | ||
| }); | ||
| flush(); | ||
| setCookie("after"); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_set")).toBe("after"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("writes to document.cookie when value changes", () => { | ||
| document.cookie = "ck_write=old"; | ||
| const { setCookie, dispose } = createRoot(dispose => { | ||
| const [, setCookie] = createServerCookie("ck_write"); | ||
| return { setCookie, dispose }; | ||
| }); | ||
| flush(); | ||
| setCookie("new"); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_write")).toBe("new"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("does not overwrite the cookie when the serialized value is unchanged", () => { | ||
| document.cookie = "ck_same=abc"; | ||
| const { cookie, setCookie, dispose } = createRoot(dispose => { | ||
| const [cookie, setCookie] = createServerCookie("ck_same"); | ||
| return { cookie, setCookie, dispose }; | ||
| }); | ||
| flush(); | ||
| setCookie("abc"); | ||
| flush(); | ||
| expect(cookie()).toBe("abc"); | ||
| expect(parseCookie(document.cookie, "ck_same")).toBe("abc"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("applies a custom deserialize function", () => { | ||
| document.cookie = "ck_deser=42"; | ||
| const dispose = createRoot(dispose => { | ||
| const [count] = createServerCookie("ck_deser", { | ||
| deserialize: str => (str !== undefined ? parseInt(str, 10) : 0), | ||
| serialize: String, | ||
| }); | ||
| expect(count()).toBe(42); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("applies a custom serialize function", () => { | ||
| const { setTags, dispose } = createRoot(dispose => { | ||
| const [, setTags] = createServerCookie<string[]>("ck_ser", { | ||
| deserialize: str => (str ? str.split(",") : []), | ||
| serialize: val => val.join(","), | ||
| }); | ||
| return { setTags, dispose }; | ||
| }); | ||
| flush(); | ||
| setTags(["a", "b", "c"]); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_ser")).toBe("a,b,c"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("clears the cookie when value is set to undefined", () => { | ||
| document.cookie = "ck_clear=present"; | ||
| const { setCookie, dispose } = createRoot(dispose => { | ||
| const [, setCookie] = createServerCookie("ck_clear"); | ||
| return { setCookie, dispose }; | ||
| }); | ||
| flush(); | ||
| (setCookie as (v: string | undefined) => void)(undefined); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_clear")).toBeUndefined(); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("does not store the string 'undefined' when value is undefined", () => { | ||
| const { dispose } = createRoot(dispose => { | ||
| createServerCookie("ck_undef"); | ||
| return { dispose }; | ||
| }); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_undef")).not.toBe("undefined"); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("respects a custom cookieMaxAge option", () => { | ||
| const { setCookie, dispose } = createRoot(dispose => { | ||
| const [, setCookie] = createServerCookie("ck_age", { cookieMaxAge: 60 }); | ||
| return { setCookie, dispose }; | ||
| }); | ||
| flush(); | ||
| setCookie("x"); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_age")).toBe("x"); | ||
| dispose(); | ||
| }); | ||
| }); | ||
|
|
||
| // ── createUserTheme ─────────────────────────────────────────────────────────── | ||
|
|
||
| describe("createUserTheme", () => { | ||
| it("reads a stored theme from the cookie", () => { | ||
| document.cookie = "ck_theme=dark"; | ||
| const dispose = createRoot(dispose => { | ||
| const [theme] = createUserTheme("ck_theme"); | ||
| expect(theme()).toBe("dark"); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("returns undefined for an unrecognized theme value", () => { | ||
| document.cookie = "ck_theme2=blue"; | ||
| const dispose = createRoot(dispose => { | ||
| const [theme] = createUserTheme("ck_theme2"); | ||
| expect(theme()).toBeUndefined(); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("returns undefined when no cookie is set and no default is provided", () => { | ||
| const dispose = createRoot(dispose => { | ||
| const [theme] = createUserTheme("ck_theme3"); | ||
| expect(theme()).toBeUndefined(); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("uses the defaultValue when cookie is absent", () => { | ||
| const dispose = createRoot(dispose => { | ||
| const [theme] = createUserTheme("ck_theme4", { defaultValue: "light" }); | ||
| expect(theme()).toBe("light"); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("accepts 'light' and 'dark' as valid values", () => { | ||
| document.cookie = "ck_theme5=light"; | ||
| const dispose = createRoot(dispose => { | ||
| const [theme] = createUserTheme("ck_theme5"); | ||
| expect(theme()).toBe("light"); | ||
| return dispose; | ||
| }); | ||
| dispose(); | ||
| }); | ||
|
|
||
| it("persists a theme change to document.cookie", () => { | ||
| const { setTheme, dispose } = createRoot(dispose => { | ||
| const [, setTheme] = createUserTheme("ck_theme6"); | ||
| return { setTheme, dispose }; | ||
| }); | ||
| flush(); | ||
| setTheme("dark"); | ||
| flush(); | ||
| expect(parseCookie(document.cookie, "ck_theme6")).toBe("dark"); | ||
| dispose(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.