diff --git a/src/components/Layout/index.spec.tsx b/src/components/Layout/index.spec.tsx index 916977a0f..e8ffb4311 100644 --- a/src/components/Layout/index.spec.tsx +++ b/src/components/Layout/index.spec.tsx @@ -18,6 +18,7 @@ import "@testing-library/jest-dom" import { withProviders } from "../../test-utils" import Layout from "../Layout" +import { testGapi } from "@/test-utils/gapi" describe("Layout", () => { it("renders correctly with gapi undefined", async () => { @@ -31,4 +32,90 @@ describe("Layout", () => { const content = await findByText("Content") expect(content).toBeVisible() }) -}) + + describe("redirect logic", () => { + const originalLocation = window.location + const gapi = { + ...testGapi(), + auth2: { + getAuthInstance: jest.fn(() => ({ + currentUser: { + get: jest.fn(() => ({ + isSignedIn: jest.fn(() => true), + getBasicProfile: jest.fn(() => ({ + getName: jest.fn(() => "Test User"), + })), + })), + }, + isSignedIn: { + get: jest.fn(() => true), + listen: jest.fn(), + }, + })), + }, + } + + beforeEach(() => { + const mockReplace = jest.fn() + delete (window as any).location + window.location = { + ...originalLocation, + replace: mockReplace, + } as any + }) + + afterEach(() => { + window.location = originalLocation as any + }) + + it("redirects from web.app to google for non-staging URLs", () => { + window.location.hostname = "ga-dev-tools.web.app" + window.location.href = "https://ga-dev-tools.web.app/feature" + window.location.pathname = "/feature" + const { wrapped, store } = withProviders( + + Content + + ) + store.dispatch({ type: "setGapi", gapi }) + + renderer.render(wrapped) + + expect(window.location.replace).toHaveBeenCalledWith( + "https://ga-dev-tools.google/feature" + ) + }) + + it("does not redirect for staging URLs", () => { + window.location.hostname = "ga-dev-tools-staging.web.app" + window.location.href = "https://ga-dev-tools-staging.web.app/feature" + window.location.pathname = "/feature" + const { wrapped, store } = withProviders( + + Content + + ) + store.dispatch({ type: "setGapi", gapi }) + + renderer.render(wrapped) + + expect(window.location.replace).not.toHaveBeenCalled() + }) + + it("does not redirect for non-web.app URLs", () => { + window.location.hostname = "localhost" + window.location.href = "http://localhost/feature" + window.location.pathname = "/feature" + const { wrapped, store } = withProviders( + + Content + + ) + store.dispatch({ type: "setGapi", gapi }) + + renderer.render(wrapped) + + expect(window.location.replace).not.toHaveBeenCalled() + }) + }) +}) \ No newline at end of file diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index de6890868..b09a8cb0e 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -124,7 +124,7 @@ const Template: React.FC> = ({ useEffect(() => { //const timeout = setTimeout(() => { // Redirect to the new domain while preserving the path. - if( window.location.hostname.indexOf('web.app') !== -1 ) { + if( window.location.hostname.indexOf('web.app') !== -1 && !window.location.hostname.includes('staging')) { const newHostname = window.location.hostname.replace('web.app', 'google'); const newLocation = window.location.href.replace( window.location.hostname, newHostname ); window.location.replace(newLocation);