forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlState.test.ts
More file actions
164 lines (141 loc) · 5.61 KB
/
Copy pathurlState.test.ts
File metadata and controls
164 lines (141 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { describe, it, expect, beforeEach, vi } from "vitest";
import {
getSearchStateFromUrl,
updateUrlWithSearchState,
buildSearchQueryString,
DEFAULT_SEARCH_STATE,
} from "./urlState";
describe("urlState", () => {
beforeEach(() => {
// Reset URL to clean state
window.history.replaceState(null, "", "/");
});
describe("getSearchStateFromUrl", () => {
it("should return empty state when no query params", () => {
window.history.replaceState(null, "", "/?");
const state = getSearchStateFromUrl();
expect(state.searchQuery).toBeUndefined();
expect(state.selectedCategory).toBeUndefined();
expect(state.priceRange).toBeUndefined();
});
it("should parse search query from URL", () => {
window.history.replaceState(null, "", "/?q=architecture");
const state = getSearchStateFromUrl();
expect(state.searchQuery).toBe("architecture");
});
it("should parse category from URL", () => {
window.history.replaceState(
null,
"",
"/?category=Software%20Development",
);
const state = getSearchStateFromUrl();
expect(state.selectedCategory).toBe("Software Development");
});
it("should parse price range from URL", () => {
window.history.replaceState(null, "", "/?priceMin=5&priceMax=20");
const state = getSearchStateFromUrl();
expect(state.priceRange).toEqual([5, 20]);
});
it("should parse sort option from URL", () => {
window.history.replaceState(null, "", "/?sort=price-low");
const state = getSearchStateFromUrl();
expect(state.sortBy).toBe("price-low");
});
it("should parse all params together", () => {
window.history.replaceState(
null,
"",
"/?q=test&category=Sales&tag=AI&priceMin=0&priceMax=15&sort=sales&creator=GD1234&availability=inactive",
);
const state = getSearchStateFromUrl();
expect(state.searchQuery).toBe("test");
expect(state.selectedCategory).toBe("Sales");
expect(state.selectedTag).toBe("AI");
expect(state.priceRange).toEqual([0, 15]);
expect(state.sortBy).toBe("sales");
expect(state.selectedCreator).toBe("GD1234");
expect(state.selectedAvailability).toBe("inactive");
});
});
describe("buildSearchQueryString", () => {
it("should build empty string when no state provided", () => {
const qs = buildSearchQueryString({});
expect(qs).toBe("");
});
it("should build query string with search query", () => {
const qs = buildSearchQueryString({ searchQuery: "architecture" });
expect(qs).toContain("q=architecture");
});
it("should not include default price range", () => {
const qs = buildSearchQueryString({ priceRange: [0, 25] });
expect(qs).not.toContain("priceMin");
expect(qs).not.toContain("priceMax");
});
it("should include non-default price range", () => {
const qs = buildSearchQueryString({ priceRange: [5, 20] });
expect(qs).toContain("priceMin=5");
expect(qs).toContain("priceMax=20");
});
it("should not include default sort", () => {
const qs = buildSearchQueryString({ sortBy: "recent" });
expect(qs).not.toContain("sort");
});
it("should include non-default sort", () => {
const qs = buildSearchQueryString({ sortBy: "price-low" });
expect(qs).toContain("sort=price-low");
});
it("should include creator and availability in query string", () => {
const qs = buildSearchQueryString({
selectedCreator: "GD1234",
selectedAvailability: "inactive",
});
expect(qs).toContain("creator=GD1234");
expect(qs).toContain("availability=inactive");
});
it("should not include default availability in query string", () => {
const qs = buildSearchQueryString({
selectedAvailability: "active",
});
expect(qs).not.toContain("availability");
});
});
describe("updateUrlWithSearchState", () => {
it("should update URL without full page reload", () => {
const replaceSpy = vi.spyOn(window.history, "replaceState");
updateUrlWithSearchState({ searchQuery: "test" });
expect(replaceSpy).toHaveBeenCalled();
expect(window.location.search).toContain("q=test");
});
it("should remove params when values are cleared", () => {
window.history.replaceState(null, "", "/?q=test&category=Sales");
updateUrlWithSearchState({ searchQuery: "", selectedCategory: "" });
expect(window.location.search).not.toContain("q");
expect(window.location.search).not.toContain("category");
});
it("should update URL with creator and availability, and clear them", () => {
updateUrlWithSearchState({
selectedCreator: "GD5678",
selectedAvailability: "all",
});
expect(window.location.search).toContain("creator=GD5678");
expect(window.location.search).toContain("availability=all");
updateUrlWithSearchState({
selectedCreator: "",
selectedAvailability: "active",
});
expect(window.location.search).not.toContain("creator");
expect(window.location.search).not.toContain("availability");
});
});
describe("DEFAULT_SEARCH_STATE", () => {
it("should have correct defaults", () => {
expect(DEFAULT_SEARCH_STATE.searchQuery).toBe("");
expect(DEFAULT_SEARCH_STATE.selectedCategory).toBe("");
expect(DEFAULT_SEARCH_STATE.priceRange).toEqual([0, 25]);
expect(DEFAULT_SEARCH_STATE.sortBy).toBe("recent");
expect(DEFAULT_SEARCH_STATE.selectedCreator).toBe("");
expect(DEFAULT_SEARCH_STATE.selectedAvailability).toBe("active");
});
});
});