|
| 1 | +/* |
| 2 | + * Copyright 2026 znai maintainers |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { |
| 18 | + readPageTabIdFromQuery, |
| 19 | + readTabIdFromQuery, |
| 20 | + writePageTabIdToQuery, |
| 21 | + writeTabIdToQuery, |
| 22 | +} from "./tabsQueryParams"; |
| 23 | + |
| 24 | +function setUrl(search: string) { |
| 25 | + window.history.replaceState(null, "", "/page" + search); |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + setUrl(""); |
| 30 | +}); |
| 31 | + |
| 32 | +describe("readTabIdFromQuery", () => { |
| 33 | + it("returns null when no tabId or no value matches this set", () => { |
| 34 | + expect(readTabIdFromQuery(["Python", "Java"])).toBeNull(); |
| 35 | + |
| 36 | + setUrl("?tabId=Mac"); |
| 37 | + expect(readTabIdFromQuery(["Python", "Java"])).toBeNull(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns the first value from the comma list that matches this set", () => { |
| 41 | + setUrl("?tabId=Python"); |
| 42 | + expect(readTabIdFromQuery(["Python", "Java"])).toEqual("Python"); |
| 43 | + |
| 44 | + setUrl("?tabId=Mac,Java,Linux"); |
| 45 | + expect(readTabIdFromQuery(["Python", "Java"])).toEqual("Java"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("tolerates percent-encoded commas and surrounding whitespace", () => { |
| 49 | + setUrl("?tabId=Mac%2CJava"); |
| 50 | + expect(readTabIdFromQuery(["Python", "Java"])).toEqual("Java"); |
| 51 | + |
| 52 | + setUrl("?tabId=Mac , Java"); |
| 53 | + expect(readTabIdFromQuery(["Python", "Java"])).toEqual("Java"); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe("writeTabIdToQuery", () => { |
| 58 | + it("sets tabId when none exists", () => { |
| 59 | + writeTabIdToQuery(["Python", "Java"], "Python"); |
| 60 | + expect(window.location.search).toEqual("?tabId=Python"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("appends when prior value belongs to a different set", () => { |
| 64 | + setUrl("?tabId=Mac"); |
| 65 | + writeTabIdToQuery(["Python", "Java"], "Python"); |
| 66 | + expect(window.location.search).toEqual("?tabId=Mac,Python"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("replaces only the slot belonging to this set in a multi value list", () => { |
| 70 | + setUrl("?tabId=Mac,Python,Blue"); |
| 71 | + writeTabIdToQuery(["Python", "Java"], "Java"); |
| 72 | + expect(window.location.search).toEqual("?tabId=Mac,Blue,Java"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("removes all prior matches for this set before appending", () => { |
| 76 | + setUrl("?tabId=Python,Mac,Java"); |
| 77 | + writeTabIdToQuery(["Python", "Java"], "Java"); |
| 78 | + expect(window.location.search).toEqual("?tabId=Mac,Java"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("preserves unrelated query parameters", () => { |
| 82 | + setUrl("?office=NYC&tabId=Mac"); |
| 83 | + writeTabIdToQuery(["Python", "Java"], "Python"); |
| 84 | + expect(window.location.search).toEqual("?office=NYC&tabId=Mac,Python"); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("readPageTabIdFromQuery", () => { |
| 89 | + it("returns the value when it matches the list, null otherwise", () => { |
| 90 | + expect(readPageTabIdFromQuery(["intro", "advanced"])).toBeNull(); |
| 91 | + |
| 92 | + setUrl("?pageTabId=advanced"); |
| 93 | + expect(readPageTabIdFromQuery(["intro", "advanced"])).toEqual("advanced"); |
| 94 | + |
| 95 | + setUrl("?pageTabId=unknown"); |
| 96 | + expect(readPageTabIdFromQuery(["intro", "advanced"])).toBeNull(); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("writePageTabIdToQuery", () => { |
| 101 | + it("sets or overwrites pageTabId", () => { |
| 102 | + writePageTabIdToQuery("intro"); |
| 103 | + expect(window.location.search).toEqual("?pageTabId=intro"); |
| 104 | + |
| 105 | + writePageTabIdToQuery("advanced"); |
| 106 | + expect(window.location.search).toEqual("?pageTabId=advanced"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("preserves tabId and other query parameters", () => { |
| 110 | + setUrl("?tabId=Python&office=NYC"); |
| 111 | + writePageTabIdToQuery("advanced"); |
| 112 | + expect(window.location.search).toEqual("?tabId=Python&office=NYC&pageTabId=advanced"); |
| 113 | + }); |
| 114 | +}); |
0 commit comments