Skip to content

Commit 3ca2962

Browse files
Merge branch 'main' into overwrite-timestamp
2 parents 2981d57 + 280874b commit 3ca2962

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/components/Layout/index.spec.tsx

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import "@testing-library/jest-dom"
1818
import { withProviders } from "../../test-utils"
1919

2020
import Layout from "../Layout"
21+
import { testGapi } from "@/test-utils/gapi"
2122

2223
describe("Layout", () => {
2324
it("renders correctly with gapi undefined", async () => {
@@ -31,4 +32,90 @@ describe("Layout", () => {
3132
const content = await findByText("Content")
3233
expect(content).toBeVisible()
3334
})
34-
})
35+
36+
describe("redirect logic", () => {
37+
const originalLocation = window.location
38+
const gapi = {
39+
...testGapi(),
40+
auth2: {
41+
getAuthInstance: jest.fn(() => ({
42+
currentUser: {
43+
get: jest.fn(() => ({
44+
isSignedIn: jest.fn(() => true),
45+
getBasicProfile: jest.fn(() => ({
46+
getName: jest.fn(() => "Test User"),
47+
})),
48+
})),
49+
},
50+
isSignedIn: {
51+
get: jest.fn(() => true),
52+
listen: jest.fn(),
53+
},
54+
})),
55+
},
56+
}
57+
58+
beforeEach(() => {
59+
const mockReplace = jest.fn()
60+
delete (window as any).location
61+
window.location = {
62+
...originalLocation,
63+
replace: mockReplace,
64+
} as any
65+
})
66+
67+
afterEach(() => {
68+
window.location = originalLocation as any
69+
})
70+
71+
it("redirects from web.app to google for non-staging URLs", () => {
72+
window.location.hostname = "ga-dev-tools.web.app"
73+
window.location.href = "https://ga-dev-tools.web.app/feature"
74+
window.location.pathname = "/feature"
75+
const { wrapped, store } = withProviders(
76+
<Layout title="Page Title" pathname={"/"} description="my description">
77+
Content
78+
</Layout>
79+
)
80+
store.dispatch({ type: "setGapi", gapi })
81+
82+
renderer.render(wrapped)
83+
84+
expect(window.location.replace).toHaveBeenCalledWith(
85+
"https://ga-dev-tools.google/feature"
86+
)
87+
})
88+
89+
it("does not redirect for staging URLs", () => {
90+
window.location.hostname = "ga-dev-tools-staging.web.app"
91+
window.location.href = "https://ga-dev-tools-staging.web.app/feature"
92+
window.location.pathname = "/feature"
93+
const { wrapped, store } = withProviders(
94+
<Layout title="Page Title" pathname={"/"} description="my description">
95+
Content
96+
</Layout>
97+
)
98+
store.dispatch({ type: "setGapi", gapi })
99+
100+
renderer.render(wrapped)
101+
102+
expect(window.location.replace).not.toHaveBeenCalled()
103+
})
104+
105+
it("does not redirect for non-web.app URLs", () => {
106+
window.location.hostname = "localhost"
107+
window.location.href = "http://localhost/feature"
108+
window.location.pathname = "/feature"
109+
const { wrapped, store } = withProviders(
110+
<Layout title="Page Title" pathname={"/"} description="my description">
111+
Content
112+
</Layout>
113+
)
114+
store.dispatch({ type: "setGapi", gapi })
115+
116+
renderer.render(wrapped)
117+
118+
expect(window.location.replace).not.toHaveBeenCalled()
119+
})
120+
})
121+
})

src/components/Layout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const Template: React.FC<PropsWithChildren<LayoutProps & TemplateProps>> = ({
124124
useEffect(() => {
125125
//const timeout = setTimeout(() => {
126126
// Redirect to the new domain while preserving the path.
127-
if( window.location.hostname.indexOf('web.app') !== -1 ) {
127+
if( window.location.hostname.indexOf('web.app') !== -1 && !window.location.hostname.includes('staging')) {
128128
const newHostname = window.location.hostname.replace('web.app', 'google');
129129
const newLocation = window.location.href.replace( window.location.hostname, newHostname );
130130
window.location.replace(newLocation);

0 commit comments

Comments
 (0)