|
1 |
| -import { parseParametersFromURL, parseResponseAPIVersion } from '../src/lib/helpers' |
| 1 | +import { |
| 2 | + parseParametersFromURL, |
| 3 | + parseResponseAPIVersion, |
| 4 | + userNotAvailableProxy, |
| 5 | + uuid, |
| 6 | +} from '../src/lib/helpers' |
2 | 7 |
|
3 | 8 | describe('parseParametersFromURL', () => {
|
4 | 9 | it('should parse parameters from a URL with query params only', () => {
|
@@ -71,3 +76,85 @@ describe('parseResponseAPIVersion', () => {
|
71 | 76 | })
|
72 | 77 | })
|
73 | 78 | })
|
| 79 | + |
| 80 | +describe('uuid', () => { |
| 81 | + if ('crypto' in globalThis) { |
| 82 | + // nodejs 18, 20 don't have crypto |
| 83 | + |
| 84 | + it('should generate a uuid when crypto.randomUUID() throws an error', () => { |
| 85 | + const originalRandomUUID = crypto.randomUUID |
| 86 | + |
| 87 | + try { |
| 88 | + crypto.randomUUID = () => { |
| 89 | + throw new Error('Fail for test') |
| 90 | + } |
| 91 | + |
| 92 | + expect(typeof uuid()).toEqual('string') |
| 93 | + } finally { |
| 94 | + crypto.randomUUID = originalRandomUUID |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + it('should generate a uuid with crypto.randomUUID()', () => { |
| 99 | + const originalRandomUUID = crypto.randomUUID |
| 100 | + |
| 101 | + try { |
| 102 | + crypto.randomUUID = () => { |
| 103 | + return 'random-uuid' |
| 104 | + } |
| 105 | + |
| 106 | + expect(uuid()).toEqual('random-uuid') |
| 107 | + } finally { |
| 108 | + crypto.randomUUID = originalRandomUUID |
| 109 | + } |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + it('should generate a uuid', () => { |
| 114 | + expect(typeof uuid()).toEqual('string') |
| 115 | + }) |
| 116 | +}) |
| 117 | + |
| 118 | +describe('userNotAvailableProxy', () => { |
| 119 | + it('should show a warning when calling toString()', () => { |
| 120 | + expect(`${userNotAvailableProxy('REASON-0')}`).toMatchInlineSnapshot( |
| 121 | + `"{\\"WARNING\\":\\"@supabase/auth-js: Attempting to access a user object which is not available. Reason: REASON-0\\"}"` |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should show a warning when calling toString()', () => { |
| 126 | + const originalWarn = console.warn |
| 127 | + |
| 128 | + try { |
| 129 | + let numberOfWarnings = 0 |
| 130 | + console.warn = (...args: any[]) => { |
| 131 | + expect(args).toMatchInlineSnapshot(` |
| 132 | + Array [ |
| 133 | + "@supabase/auth-js: Accessing the \\"id\\" (or any other) property of the user object under session is not supported, returned value will be \\"@@@ not-available @@@\\". Reason: REASON-1", |
| 134 | + ] |
| 135 | + `) |
| 136 | + numberOfWarnings += 1 |
| 137 | + } |
| 138 | + |
| 139 | + const object = userNotAvailableProxy('REASON-1') |
| 140 | + expect(object.id).toMatchInlineSnapshot(`"@@@ not-available @@@"`) |
| 141 | + expect(object.created_at).toMatchInlineSnapshot(`"@@@ not-available @@@"`) |
| 142 | + expect(object.aud).toMatchInlineSnapshot(`"@@@ not-available @@@"`) |
| 143 | + expect(object.app_metadata).toMatchInlineSnapshot(` |
| 144 | + UnavailableObject { |
| 145 | + Symbol(WARNING): "@supabase/auth-js: User property object \\"app_metadata\\" is not available. Reason: REASON-1", |
| 146 | + } |
| 147 | + `) |
| 148 | + expect(object.user_metadata).toMatchInlineSnapshot(` |
| 149 | + UnavailableObject { |
| 150 | + Symbol(WARNING): "@supabase/auth-js: User property object \\"user_metadata\\" is not available. Reason: REASON-1", |
| 151 | + } |
| 152 | + `) |
| 153 | + expect(object.email).toMatchInlineSnapshot(`undefined`) |
| 154 | + |
| 155 | + expect(numberOfWarnings).toEqual(1) |
| 156 | + } finally { |
| 157 | + console.warn = originalWarn |
| 158 | + } |
| 159 | + }) |
| 160 | +}) |
0 commit comments