|
2 | 2 | * @jest-environment jsdom
|
3 | 3 | */
|
4 | 4 |
|
5 |
| -import { autoRefreshClient, getClientWithSpecificStorage, pkceClient } from './lib/clients' |
| 5 | +import { |
| 6 | + autoRefreshClient, |
| 7 | + getClientWithSpecificStorage, |
| 8 | + getClientWithSpecificStorageKey, |
| 9 | + pkceClient, |
| 10 | +} from './lib/clients' |
6 | 11 | import { mockUserCredentials } from './lib/utils'
|
7 | 12 |
|
8 | 13 | // Add structuredClone polyfill for jsdom
|
@@ -98,6 +103,94 @@ describe('GoTrueClient in browser environment', () => {
|
98 | 103 | expect(signinError).toBeNull()
|
99 | 104 | expect(signinData?.session).toBeDefined()
|
100 | 105 | })
|
| 106 | + |
| 107 | + it('should warn when two clients are created with the same storage key', () => { |
| 108 | + let consoleWarnSpy |
| 109 | + let consoleTraceSpy |
| 110 | + try { |
| 111 | + consoleWarnSpy = jest.spyOn(console, 'warn') |
| 112 | + consoleTraceSpy = jest.spyOn(console, 'trace') |
| 113 | + getClientWithSpecificStorageKey('test-storage-key') |
| 114 | + getClientWithSpecificStorageKey('test-storage-key') |
| 115 | + expect(consoleWarnSpy).toHaveBeenCalledTimes(1) |
| 116 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 117 | + expect.stringMatching( |
| 118 | + /GoTrueClient@test-storage-key:1 .* Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key./ |
| 119 | + ) |
| 120 | + ) |
| 121 | + expect(consoleTraceSpy).not.toHaveBeenCalled() |
| 122 | + } finally { |
| 123 | + consoleWarnSpy?.mockRestore() |
| 124 | + consoleTraceSpy?.mockRestore() |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + it('should warn & trace when two clients are created with the same storage key and debug is enabled', () => { |
| 129 | + let consoleWarnSpy |
| 130 | + let consoleTraceSpy |
| 131 | + try { |
| 132 | + consoleWarnSpy = jest.spyOn(console, 'warn') |
| 133 | + consoleTraceSpy = jest.spyOn(console, 'trace') |
| 134 | + getClientWithSpecificStorageKey('test-storage-key') |
| 135 | + getClientWithSpecificStorageKey('test-storage-key', { debug: true }) |
| 136 | + expect(consoleWarnSpy).toHaveBeenCalledTimes(1) |
| 137 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 138 | + expect.stringMatching( |
| 139 | + /GoTrueClient@test-storage-key:1 .* Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key./ |
| 140 | + ) |
| 141 | + ) |
| 142 | + expect(consoleTraceSpy).toHaveBeenCalledWith( |
| 143 | + expect.stringMatching( |
| 144 | + /GoTrueClient@test-storage-key:1 .* Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key./ |
| 145 | + ) |
| 146 | + ) |
| 147 | + } finally { |
| 148 | + consoleWarnSpy?.mockRestore() |
| 149 | + consoleTraceSpy?.mockRestore() |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + it('should not warn when two clients are created with differing storage keys', () => { |
| 154 | + let consoleWarnSpy |
| 155 | + let consoleTraceSpy |
| 156 | + try { |
| 157 | + consoleWarnSpy = jest.spyOn(console, 'warn') |
| 158 | + consoleTraceSpy = jest.spyOn(console, 'trace') |
| 159 | + getClientWithSpecificStorageKey('test-storage-key1') |
| 160 | + getClientWithSpecificStorageKey('test-storage-key2') |
| 161 | + expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 162 | + expect(consoleTraceSpy).not.toHaveBeenCalled() |
| 163 | + } finally { |
| 164 | + consoleWarnSpy?.mockRestore() |
| 165 | + consoleTraceSpy?.mockRestore() |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + it('should not warn only when a second client with a duplicate key is created', () => { |
| 170 | + let consoleWarnSpy |
| 171 | + let consoleTraceSpy |
| 172 | + try { |
| 173 | + consoleWarnSpy = jest.spyOn(console, 'warn') |
| 174 | + consoleTraceSpy = jest.spyOn(console, 'trace') |
| 175 | + getClientWithSpecificStorageKey('test-storage-key1') |
| 176 | + expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 177 | + getClientWithSpecificStorageKey('test-storage-key2') |
| 178 | + expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 179 | + getClientWithSpecificStorageKey('test-storage-key3') |
| 180 | + expect(consoleWarnSpy).not.toHaveBeenCalled() |
| 181 | + getClientWithSpecificStorageKey('test-storage-key2') |
| 182 | + expect(consoleWarnSpy).toHaveBeenCalledTimes(1) |
| 183 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 184 | + expect.stringMatching( |
| 185 | + /GoTrueClient@test-storage-key2:1 .* Multiple GoTrueClient instances detected in the same browser context. It is not an error, but this should be avoided as it may produce undefined behavior when used concurrently under the same storage key./ |
| 186 | + ) |
| 187 | + ) |
| 188 | + expect(consoleTraceSpy).not.toHaveBeenCalled() |
| 189 | + } finally { |
| 190 | + consoleWarnSpy?.mockRestore() |
| 191 | + consoleTraceSpy?.mockRestore() |
| 192 | + } |
| 193 | + }) |
101 | 194 | })
|
102 | 195 |
|
103 | 196 | describe('Callback URL handling', () => {
|
|
0 commit comments