Skip to content

Commit

Permalink
feat: move data field to hash (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenhuugiatri authored Jan 8, 2025
1 parent 9ba2878 commit de29f52
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
61 changes: 60 additions & 1 deletion packages/waypoint/__tests__/common/popup.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"

import { openPopup, replaceUrl } from "../../common/popup"
import { buildUrlWithQuery, openPopup, replaceUrl } from "../../common/popup"

describe("Popup and URL functions", () => {
beforeEach(() => {
Expand All @@ -21,6 +21,65 @@ describe("Popup and URL functions", () => {
vi.restoreAllMocks()
})

describe("buildUrlWithQuery", () => {
test("returns the original URL when no query is provided", () => {
const inputUrl = "https://example.com/"
const result = buildUrlWithQuery(inputUrl)
expect(result.toString()).toBe(inputUrl)
})

test("appends query parameters to the URL", () => {
const inputUrl = "https://example.com"
const query = { foo: "bar", baz: "qux" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?foo=bar&baz=qux")
})

test("skips null or undefined values in query", () => {
const inputUrl = "https://example.com"
const query = { foo: "bar", baz: null, qux: undefined }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?foo=bar")
})

test("adds hashed parameters to the URL hash", () => {
const inputUrl = "https://example.com"
const query = { data: "value", foo: "bar" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?foo=bar#data=value")
})

test("encodes objects as JSON strings", () => {
const inputUrl = "https://example.com"
const query = { data: { key: "value" }, foo: "bar" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe(
"https://example.com/?foo=bar#data=%7B%22key%22%3A%22value%22%7D",
)
})

test("handles a mix of hashed and regular parameters", () => {
const inputUrl = "https://example.com"
const query = { data: "hashValue", foo: "bar", baz: "qux" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?foo=bar&baz=qux#data=hashValue")
})

test("preserves existing search parameters in the input URL", () => {
const inputUrl = "https://example.com/?existing=param"
const query = { foo: "bar" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?existing=param&foo=bar")
})

test("overwrites existing search parameters if they conflict", () => {
const inputUrl = "https://example.com/?foo=oldValue"
const query = { foo: "newValue" }
const result = buildUrlWithQuery(inputUrl, query)
expect(result.toString()).toBe("https://example.com/?foo=newValue")
})
})

describe("openPopup", () => {
test("includes query parameters in the popup URL", () => {
const mockUrl = "https://example.com"
Expand Down
21 changes: 13 additions & 8 deletions packages/waypoint/common/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ type PopupConfig = {
width?: number
height?: number
}
export const HASHED_PARAMS = ["data"]

const buildUrlWithQuery = (inputUrl: string, query?: Record<string, UrlParams>): URL => {
export const buildUrlWithQuery = (inputUrl: string, query?: Record<string, UrlParams>): URL => {
const url = new URL(inputUrl)
if (query) {
Object.entries(query).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
url.searchParams.set(key, value.toString())
}
})
}
if (!query) return url
Object.entries(query).forEach(([key, value]) => {
if (value === null || value === undefined) return
if (HASHED_PARAMS.includes(key)) {
url.hash = `${key}=${encodeURIComponent(
typeof value === "object" ? JSON.stringify(value) : value.toString(),
)}`
return
}
url.searchParams.set(key, value.toString())
})
return url
}

Expand Down
2 changes: 1 addition & 1 deletion packages/waypoint/common/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Generated by genversion.
export const version = "3.2.2"
export const version = "4.0.1"
2 changes: 1 addition & 1 deletion packages/waypoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"repository": {
"url": "https://github.com/skymavis/waypoint-js"
},
"version": "4.0.0",
"version": "4.0.1",
"license": "MIT",
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit de29f52

Please sign in to comment.