|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { |
| 6 | + autoRefreshClient, |
| 7 | + getClientWithSpecificStorage, |
| 8 | + pkceClient |
| 9 | +} from './lib/clients' |
| 10 | +import { mockUserCredentials } from './lib/utils' |
| 11 | + |
| 12 | +// Add structuredClone polyfill for jsdom |
| 13 | +if (typeof structuredClone === 'undefined') { |
| 14 | + (global as any).structuredClone = (obj: any) => JSON.parse(JSON.stringify(obj)) |
| 15 | +} |
| 16 | + |
| 17 | +describe('GoTrueClient in browser environment', () => { |
| 18 | + beforeEach(() => { |
| 19 | + const mockLocalStorage = { |
| 20 | + getItem: jest.fn(), |
| 21 | + setItem: jest.fn(), |
| 22 | + removeItem: jest.fn(), |
| 23 | + } |
| 24 | + Object.defineProperty(window, 'localStorage', { |
| 25 | + value: mockLocalStorage, |
| 26 | + writable: true, |
| 27 | + }) |
| 28 | + |
| 29 | + // Mock window.location |
| 30 | + const mockLocation = { |
| 31 | + href: 'http://localhost:9999', |
| 32 | + assign: jest.fn(), |
| 33 | + replace: jest.fn(), |
| 34 | + reload: jest.fn(), |
| 35 | + toString: () => 'http://localhost:9999' |
| 36 | + } |
| 37 | + Object.defineProperty(window, 'location', { |
| 38 | + value: mockLocation, |
| 39 | + writable: true, |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should handle basic OAuth', async () => { |
| 44 | + const { data } = await pkceClient.signInWithOAuth({ |
| 45 | + provider: 'github', |
| 46 | + options: { |
| 47 | + redirectTo: 'http://localhost:9999/callback' |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + expect(data?.url).toBeDefined() |
| 52 | + expect(data?.url).toContain('provider=github') |
| 53 | + }) |
| 54 | + |
| 55 | + it('should handle multiple visibility changes', async () => { |
| 56 | + Object.defineProperty(document, 'visibilityState', { |
| 57 | + value: 'visible', |
| 58 | + writable: true, |
| 59 | + }) |
| 60 | + |
| 61 | + await autoRefreshClient.startAutoRefresh() |
| 62 | + |
| 63 | + document.dispatchEvent(new Event('visibilitychange')) |
| 64 | + Object.defineProperty(document, 'visibilityState', { |
| 65 | + value: 'hidden', |
| 66 | + writable: true, |
| 67 | + }) |
| 68 | + document.dispatchEvent(new Event('visibilitychange')) |
| 69 | + Object.defineProperty(document, 'visibilityState', { |
| 70 | + value: 'visible', |
| 71 | + writable: true, |
| 72 | + }) |
| 73 | + document.dispatchEvent(new Event('visibilitychange')) |
| 74 | + |
| 75 | + await autoRefreshClient.stopAutoRefresh() |
| 76 | + }) |
| 77 | + |
| 78 | + it('should handle PKCE flow', async () => { |
| 79 | + const { email, password } = mockUserCredentials() |
| 80 | + const pkceClient = getClientWithSpecificStorage({ |
| 81 | + getItem: jest.fn(), |
| 82 | + setItem: jest.fn(), |
| 83 | + removeItem: jest.fn(), |
| 84 | + }) |
| 85 | + |
| 86 | + const { data: signupData, error: signupError } = await pkceClient.signUp({ |
| 87 | + email, |
| 88 | + password, |
| 89 | + options: { |
| 90 | + emailRedirectTo: 'http://localhost:9999/callback' |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + expect(signupError).toBeNull() |
| 95 | + expect(signupData?.user).toBeDefined() |
| 96 | + |
| 97 | + const { data: signinData, error: signinError } = await pkceClient.signInWithPassword({ |
| 98 | + email, |
| 99 | + password, |
| 100 | + }) |
| 101 | + |
| 102 | + expect(signinError).toBeNull() |
| 103 | + expect(signinData?.session).toBeDefined() |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('Callback URL handling', () => { |
| 108 | + let mockFetch: jest.Mock |
| 109 | + let storedSession: string | null |
| 110 | + const mockStorage = { |
| 111 | + getItem: jest.fn(() => storedSession), |
| 112 | + setItem: jest.fn((key: string, value: string) => { |
| 113 | + storedSession = value |
| 114 | + }), |
| 115 | + removeItem: jest.fn(() => { |
| 116 | + storedSession = null |
| 117 | + }), |
| 118 | + } |
| 119 | + |
| 120 | + beforeEach(() => { |
| 121 | + mockFetch = jest.fn() |
| 122 | + global.fetch = mockFetch |
| 123 | + }) |
| 124 | + |
| 125 | + it('should handle implicit grant callback', async () => { |
| 126 | + // Set up URL with implicit grant callback parameters |
| 127 | + window.location.href = 'http://localhost:9999/callback#access_token=test-token&refresh_token=test-refresh-token&expires_in=3600&token_type=bearer&type=implicit' |
| 128 | + |
| 129 | + // Mock user info response |
| 130 | + mockFetch.mockImplementation((url: string) => { |
| 131 | + if (url.includes('/user')) { |
| 132 | + return Promise.resolve({ |
| 133 | + ok: true, |
| 134 | + json: () => Promise.resolve({ |
| 135 | + id: 'test-user', |
| 136 | + |
| 137 | + created_at: new Date().toISOString() |
| 138 | + }) |
| 139 | + }) |
| 140 | + } |
| 141 | + return Promise.resolve({ |
| 142 | + ok: true, |
| 143 | + json: () => Promise.resolve({ |
| 144 | + access_token: 'test-token', |
| 145 | + refresh_token: 'test-refresh-token', |
| 146 | + expires_in: 3600, |
| 147 | + token_type: 'bearer', |
| 148 | + user: { id: 'test-user' } |
| 149 | + }) |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + const client = getClientWithSpecificStorage(mockStorage) |
| 154 | + await client.initialize() |
| 155 | + |
| 156 | + const { data: { session } } = await client.getSession() |
| 157 | + expect(session).toBeDefined() |
| 158 | + expect(session?.access_token).toBe('test-token') |
| 159 | + expect(session?.refresh_token).toBe('test-refresh-token') |
| 160 | + }) |
| 161 | + |
| 162 | + it('should handle error in callback URL', async () => { |
| 163 | + // Set up URL with error parameters |
| 164 | + window.location.href = 'http://localhost:9999/callback#error=invalid_grant&error_description=Invalid+grant' |
| 165 | + |
| 166 | + mockFetch.mockImplementation((url: string) => { |
| 167 | + return Promise.resolve({ |
| 168 | + ok: false, |
| 169 | + json: () => Promise.resolve({ |
| 170 | + error: 'invalid_grant', |
| 171 | + error_description: 'Invalid grant' |
| 172 | + }) |
| 173 | + }) |
| 174 | + }) |
| 175 | + |
| 176 | + const client = getClientWithSpecificStorage(mockStorage) |
| 177 | + await client.initialize() |
| 178 | + |
| 179 | + const { data: { session } } = await client.getSession() |
| 180 | + expect(session).toBeNull() |
| 181 | + }) |
| 182 | +}) |
| 183 | + |
| 184 | +describe('GoTrueClient BroadcastChannel', () => { |
| 185 | + it('should handle multiple auth state change events', async () => { |
| 186 | + const mockBroadcastChannel = jest.fn().mockImplementation(() => ({ |
| 187 | + postMessage: jest.fn(), |
| 188 | + addEventListener: jest.fn(), |
| 189 | + removeEventListener: jest.fn(), |
| 190 | + close: jest.fn(), |
| 191 | + })) |
| 192 | + Object.defineProperty(window, 'BroadcastChannel', { |
| 193 | + value: mockBroadcastChannel, |
| 194 | + writable: true, |
| 195 | + }) |
| 196 | + |
| 197 | + const mockStorage = { |
| 198 | + getItem: jest.fn(), |
| 199 | + setItem: jest.fn(), |
| 200 | + removeItem: jest.fn(), |
| 201 | + } |
| 202 | + |
| 203 | + const client = getClientWithSpecificStorage(mockStorage) |
| 204 | + const mockCallback1 = jest.fn() |
| 205 | + const mockCallback2 = jest.fn() |
| 206 | + |
| 207 | + const { data: { subscription: sub1 } } = client.onAuthStateChange(mockCallback1) |
| 208 | + const { data: { subscription: sub2 } } = client.onAuthStateChange(mockCallback2) |
| 209 | + |
| 210 | + // Simulate a broadcast message |
| 211 | + const mockEvent = { |
| 212 | + data: { |
| 213 | + event: 'SIGNED_IN', |
| 214 | + session: { |
| 215 | + access_token: 'test-token', |
| 216 | + refresh_token: 'test-refresh-token', |
| 217 | + expires_in: 3600, |
| 218 | + token_type: 'bearer', |
| 219 | + user: { id: 'test-user' } |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + // Get the event listener that was registered |
| 225 | + const eventListener = mockBroadcastChannel.mock.results[0].value.addEventListener.mock.calls[0][1] |
| 226 | + eventListener(mockEvent) |
| 227 | + |
| 228 | + expect(mockCallback1).toHaveBeenCalledWith('SIGNED_IN', mockEvent.data.session) |
| 229 | + expect(mockCallback2).toHaveBeenCalledWith('SIGNED_IN', mockEvent.data.session) |
| 230 | + |
| 231 | + sub1.unsubscribe() |
| 232 | + sub2.unsubscribe() |
| 233 | + }) |
| 234 | +}) |
0 commit comments