1+ import { submitComponentEvent as submitComponentEventMock } from '../client/ophan/ophan' ;
12import { getRedirectUrl , initializeFedCM } from './GoogleOneTap.importable' ;
23
4+ jest . mock ( '../client/ophan/ophan' , ( ) => ( {
5+ submitComponentEvent : jest . fn ( ) ,
6+ } ) ) ;
7+
38const mockWindow = ( {
49 get,
510 enableFedCM = true ,
@@ -58,6 +63,10 @@ const mockWindow = ({
5863} ;
5964
6065describe ( 'GoogleOneTap' , ( ) => {
66+ beforeEach ( ( ) => {
67+ jest . clearAllMocks ( ) ;
68+ } ) ;
69+
6170 it ( 'should return the correct signin URL after constructing it with the provided stage and token' , ( ) => {
6271 expect (
6372 getRedirectUrl ( {
@@ -99,6 +108,29 @@ describe('GoogleOneTap', () => {
99108
100109 await initializeFedCM ( { isSignedIn : false } ) ;
101110
111+ expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
112+ 1 ,
113+ {
114+ component : {
115+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
116+ } ,
117+ action : 'DETECT' ,
118+ value : 'SUPPORTED' ,
119+ } ,
120+ 'Web' ,
121+ ) ;
122+
123+ expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
124+ 2 ,
125+ {
126+ component : {
127+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
128+ } ,
129+ action : 'SIGN_IN' ,
130+ } ,
131+ 'Web' ,
132+ ) ;
133+
102134 expect ( form . action ) . toBe (
103135 'https://profile.theguardian.com/signin/google-one-tap?returnUrl=https%3A%2F%2Fwww.theguardian.com%2Fuk' ,
104136 ) ;
@@ -123,6 +155,29 @@ describe('GoogleOneTap', () => {
123155
124156 await initializeFedCM ( { isSignedIn : false } ) ;
125157
158+ expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
159+ 1 ,
160+ {
161+ component : {
162+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
163+ } ,
164+ action : 'DETECT' ,
165+ value : 'SUPPORTED' ,
166+ } ,
167+ 'Web' ,
168+ ) ;
169+
170+ expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
171+ 2 ,
172+ {
173+ component : {
174+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
175+ } ,
176+ action : 'CLOSE' ,
177+ } ,
178+ 'Web' ,
179+ ) ;
180+
126181 expect ( navigatorGet ) . toHaveBeenCalledWith ( {
127182 identity : {
128183 context : 'continue' ,
@@ -155,6 +210,18 @@ describe('GoogleOneTap', () => {
155210 'window.navigator.credentials.get failed' ,
156211 ) ;
157212
213+ expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
214+ 1 ,
215+ {
216+ component : {
217+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
218+ } ,
219+ action : 'DETECT' ,
220+ value : 'SUPPORTED' ,
221+ } ,
222+ 'Web' ,
223+ ) ;
224+
158225 expect ( navigatorGet ) . toHaveBeenCalledWith ( {
159226 identity : {
160227 context : 'continue' ,
@@ -183,6 +250,18 @@ describe('GoogleOneTap', () => {
183250
184251 await initializeFedCM ( { isSignedIn : false } ) ;
185252
253+ expect ( submitComponentEventMock ) . toHaveBeenCalledTimes ( 1 ) ;
254+ expect ( submitComponentEventMock ) . toHaveBeenCalledWith (
255+ {
256+ component : {
257+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
258+ } ,
259+ action : 'DETECT' ,
260+ value : 'NOT_SUPPORTED' ,
261+ } ,
262+ 'Web' ,
263+ ) ;
264+
186265 expect ( navigatorGet ) . not . toHaveBeenCalled ( ) ;
187266 expect ( form . submit ) . not . toHaveBeenCalled ( ) ;
188267 } ) ;
@@ -196,18 +275,17 @@ describe('GoogleOneTap', () => {
196275
197276 await initializeFedCM ( { isSignedIn : true } ) ;
198277
199- expect ( navigatorGet ) . not . toHaveBeenCalled ( ) ;
200- expect ( form . submit ) . not . toHaveBeenCalled ( ) ;
201- } ) ;
202-
203- it ( 'should not initializeFedCM when user is not in test' , async ( ) => {
204- const navigatorGet = jest . fn ( ) ;
205-
206- const { form } = mockWindow ( {
207- get : navigatorGet ,
208- } ) ;
209-
210- await initializeFedCM ( { isSignedIn : true } ) ;
278+ expect ( submitComponentEventMock ) . toHaveBeenCalledTimes ( 1 ) ;
279+ expect ( submitComponentEventMock ) . toHaveBeenCalledWith (
280+ {
281+ component : {
282+ componentType : 'SIGN_IN_GOOGLE_ONE_TAP' ,
283+ } ,
284+ action : 'DETECT' ,
285+ value : 'SUPPORTED' ,
286+ } ,
287+ 'Web' ,
288+ ) ;
211289
212290 expect ( navigatorGet ) . not . toHaveBeenCalled ( ) ;
213291 expect ( form . submit ) . not . toHaveBeenCalled ( ) ;
0 commit comments