|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Bayan Flow |
| 3 | + * Licensed under Elastic License 2.0 OR Commercial |
| 4 | + * See LICENSE for details. |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 8 | +import { render, waitFor } from '@testing-library/react'; |
| 9 | +import { MemoryRouter } from 'react-router-dom'; |
| 10 | +import GoogleOneTap from './GoogleOneTap'; |
| 11 | +import { useAuth } from '../hooks/useAuth'; |
| 12 | +import { initOneTap } from '../lib/googleIdentity'; |
| 13 | +import * as authService from '../services/authService'; |
| 14 | + |
| 15 | +vi.mock('../hooks/useAuth', () => ({ |
| 16 | + useAuth: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('../lib/googleIdentity', () => ({ |
| 20 | + initOneTap: vi.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock('../services/authService', () => ({ |
| 24 | + signInWithGoogleIdToken: vi.fn(async () => undefined), |
| 25 | +})); |
| 26 | + |
| 27 | +describe('GoogleOneTap', () => { |
| 28 | + beforeEach(() => { |
| 29 | + vi.mocked(useAuth).mockReset(); |
| 30 | + vi.mocked(initOneTap).mockReset(); |
| 31 | + vi.mocked(authService.signInWithGoogleIdToken).mockReset(); |
| 32 | + vi.mocked(initOneTap).mockResolvedValue({ |
| 33 | + cancel: vi.fn(), |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not initialize One Tap when user is authenticated', () => { |
| 38 | + vi.mocked(useAuth).mockReturnValue({ |
| 39 | + isConfigured: true, |
| 40 | + isLoading: false, |
| 41 | + isAuthenticated: true, |
| 42 | + }); |
| 43 | + |
| 44 | + render( |
| 45 | + <MemoryRouter initialEntries={['/']}> |
| 46 | + <GoogleOneTap /> |
| 47 | + </MemoryRouter> |
| 48 | + ); |
| 49 | + |
| 50 | + expect(initOneTap).not.toHaveBeenCalled(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('initializes One Tap on landing when signed out', async () => { |
| 54 | + vi.mocked(useAuth).mockReturnValue({ |
| 55 | + isConfigured: true, |
| 56 | + isLoading: false, |
| 57 | + isAuthenticated: false, |
| 58 | + }); |
| 59 | + |
| 60 | + render( |
| 61 | + <MemoryRouter initialEntries={['/']}> |
| 62 | + <GoogleOneTap /> |
| 63 | + </MemoryRouter> |
| 64 | + ); |
| 65 | + |
| 66 | + await waitFor(() => { |
| 67 | + expect(initOneTap).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('does not initialize One Tap on roadmap', () => { |
| 72 | + vi.mocked(useAuth).mockReturnValue({ |
| 73 | + isConfigured: true, |
| 74 | + isLoading: false, |
| 75 | + isAuthenticated: false, |
| 76 | + }); |
| 77 | + |
| 78 | + render( |
| 79 | + <MemoryRouter initialEntries={['/roadmap']}> |
| 80 | + <GoogleOneTap /> |
| 81 | + </MemoryRouter> |
| 82 | + ); |
| 83 | + |
| 84 | + expect(initOneTap).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments