Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 88 additions & 1 deletion src/components/Layout/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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(
<Layout title="Page Title" pathname={"/"} description="my description">
Content
</Layout>
)
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(
<Layout title="Page Title" pathname={"/"} description="my description">
Content
</Layout>
)
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(
<Layout title="Page Title" pathname={"/"} description="my description">
Content
</Layout>
)
store.dispatch({ type: "setGapi", gapi })

renderer.render(wrapped)

expect(window.location.replace).not.toHaveBeenCalled()
})
})
})
2 changes: 1 addition & 1 deletion src/components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@
user,
}) => {
usePageView(title)
const { gaVersion, setGAVersion } = useGAVersion(pathname)

Check warning on line 120 in src/components/Layout/index.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

'setGAVersion' is assigned a value but never used

const [open, setOpen] = React.useState(false)

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);
Expand Down
Loading