-
Notifications
You must be signed in to change notification settings - Fork 0
feat: enhance API token handling and playground UI notifications #360
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b789496
feat: enhance API token handling and UI notifications
bernirosas fd24cb6
feat(detector): log message for missing API token
bernirosas e50afc1
docs(README): clarify token handling for event flushing
bernirosas ac680b0
docs(README): enhance token handling instructions and warnings
bernirosas 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
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,98 @@ | ||
| import { afterEach, beforeEach, expect, test, vi } from "vitest"; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| vi.restoreAllMocks(); | ||
| document.body.innerHTML = ""; | ||
| document.cookie = ""; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("captures impressions before token is set", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
| document.body.innerHTML = '<div data-ts-product="prod-1"></div>'; | ||
|
|
||
| const events: CustomEvent[] = []; | ||
| window.addEventListener("topsort", (e) => events.push(e as CustomEvent)); | ||
|
|
||
| await import("./detector"); | ||
|
|
||
| expect(events).toHaveLength(1); | ||
| expect(events[0]?.detail.type).toBe("Impression"); | ||
| expect(events[0]?.detail.product).toBe("prod-1"); | ||
| }); | ||
|
|
||
| test("captures clicks before token is set", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
| document.body.innerHTML = '<div data-ts-product="prod-click"></div>'; | ||
|
|
||
| const events: CustomEvent[] = []; | ||
| window.addEventListener("topsort", (e) => events.push(e as CustomEvent)); | ||
|
|
||
| await import("./detector"); | ||
|
|
||
| const card = document.querySelector<HTMLElement>("[data-ts-product]"); | ||
| card?.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
|
|
||
| const clickEvent = events.find((e) => e.detail.type === "Click"); | ||
| expect(clickEvent).toBeDefined(); | ||
| expect(clickEvent?.detail.product).toBe("prod-click"); | ||
| }); | ||
|
|
||
| test("observes dynamically added elements before token is set", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
|
|
||
| const events: CustomEvent[] = []; | ||
| window.addEventListener("topsort", (e) => events.push(e as CustomEvent)); | ||
|
|
||
| await import("./detector"); | ||
|
|
||
| const div = document.createElement("div"); | ||
| div.dataset.tsProduct = "prod-dynamic"; | ||
| document.body.appendChild(div); | ||
| await new Promise(process.nextTick); | ||
|
|
||
| expect(events.some((e) => e.detail.product === "prod-dynamic")).toBe(true); | ||
| }); | ||
|
|
||
| test("installs getter/setter on window.TS.token when no token is provided", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
| await import("./detector"); | ||
|
|
||
| const descriptor = Object.getOwnPropertyDescriptor(window.TS, "token"); | ||
| expect(typeof descriptor?.get).toBe("function"); | ||
| expect(typeof descriptor?.set).toBe("function"); | ||
| expect(descriptor?.configurable).toBe(true); | ||
| }); | ||
|
|
||
| test("does not install token watcher when token is already present", async () => { | ||
| window.TS = { token: "existing-token" }; | ||
| await import("./detector"); | ||
|
|
||
| // Plain data property — no getter/setter installed | ||
| const descriptor = Object.getOwnPropertyDescriptor(window.TS, "token"); | ||
| expect(descriptor?.get).toBeUndefined(); | ||
| expect(descriptor?.value).toBe("existing-token"); | ||
| }); | ||
|
|
||
| test("setting the token via watcher makes it readable through the getter", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
| await import("./detector"); | ||
|
|
||
| window.TS.token = "deferred-token"; | ||
|
|
||
| expect(window.TS.token).toBe("deferred-token"); | ||
| }); | ||
|
|
||
| test("setting the token twice updates the value", async () => { | ||
| window.TS = {} as typeof window.TS; | ||
| await import("./detector"); | ||
|
|
||
| window.TS.token = "token-v1"; | ||
| window.TS.token = "token-v2"; | ||
|
|
||
| expect(window.TS.token).toBe("token-v2"); | ||
| }); |
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
Oops, something went wrong.
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.