@@ -18,6 +18,7 @@ import "@testing-library/jest-dom"
1818import { withProviders } from "../../test-utils"
1919
2020import Layout from "../Layout"
21+ import { testGapi } from "@/test-utils/gapi"
2122
2223describe ( "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+ } )
0 commit comments