|
| 1 | +import { PostgrestClient } from '../src/index' |
| 2 | +import { Database } from './types.override' |
| 3 | + |
| 4 | +const REST_URL = 'http://localhost:3000' |
| 5 | + |
| 6 | +test('custom fetch function', async () => { |
| 7 | + const customFetch = jest.fn().mockImplementation(() => |
| 8 | + Promise.resolve({ |
| 9 | + ok: true, |
| 10 | + status: 200, |
| 11 | + statusText: 'OK', |
| 12 | + text: () => Promise.resolve('[]'), |
| 13 | + }) |
| 14 | + ) |
| 15 | + |
| 16 | + const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, { |
| 17 | + fetch: customFetch, |
| 18 | + }) |
| 19 | + |
| 20 | + await postgrestWithCustomFetch.from('users').select() |
| 21 | + |
| 22 | + expect(customFetch).toHaveBeenCalledWith( |
| 23 | + expect.stringContaining(REST_URL), |
| 24 | + expect.objectContaining({ |
| 25 | + method: 'GET', |
| 26 | + headers: expect.any(Object), |
| 27 | + }) |
| 28 | + ) |
| 29 | +}) |
| 30 | + |
| 31 | +test('handles undefined global fetch', async () => { |
| 32 | + // Store original fetch |
| 33 | + const originalFetch = globalThis.fetch |
| 34 | + // Delete global fetch to simulate environments where it's undefined |
| 35 | + delete (globalThis as any).fetch |
| 36 | + |
| 37 | + try { |
| 38 | + const postgrestClient = new PostgrestClient<Database>(REST_URL) |
| 39 | + const result = await postgrestClient.from('users').select() |
| 40 | + expect(result).toMatchInlineSnapshot(` |
| 41 | + Object { |
| 42 | + "count": null, |
| 43 | + "data": Array [ |
| 44 | + Object { |
| 45 | + "age_range": "[1,2)", |
| 46 | + "catchphrase": "'cat' 'fat'", |
| 47 | + "data": null, |
| 48 | + "status": "ONLINE", |
| 49 | + "username": "supabot", |
| 50 | + }, |
| 51 | + Object { |
| 52 | + "age_range": "[25,35)", |
| 53 | + "catchphrase": "'bat' 'cat'", |
| 54 | + "data": null, |
| 55 | + "status": "OFFLINE", |
| 56 | + "username": "kiwicopple", |
| 57 | + }, |
| 58 | + Object { |
| 59 | + "age_range": "[25,35)", |
| 60 | + "catchphrase": "'bat' 'rat'", |
| 61 | + "data": null, |
| 62 | + "status": "ONLINE", |
| 63 | + "username": "awailas", |
| 64 | + }, |
| 65 | + Object { |
| 66 | + "age_range": "[20,30)", |
| 67 | + "catchphrase": "'fat' 'rat'", |
| 68 | + "data": null, |
| 69 | + "status": "ONLINE", |
| 70 | + "username": "dragarcia", |
| 71 | + }, |
| 72 | + Object { |
| 73 | + "age_range": "[20,30)", |
| 74 | + "catchphrase": "'json' 'test'", |
| 75 | + "data": Object { |
| 76 | + "foo": Object { |
| 77 | + "bar": Object { |
| 78 | + "nested": "value", |
| 79 | + }, |
| 80 | + "baz": "string value", |
| 81 | + }, |
| 82 | + }, |
| 83 | + "status": "ONLINE", |
| 84 | + "username": "jsonuser", |
| 85 | + }, |
| 86 | + ], |
| 87 | + "error": null, |
| 88 | + "status": 200, |
| 89 | + "statusText": "OK", |
| 90 | + } |
| 91 | + `) |
| 92 | + // Test passes if we reach here without errors, as it means nodeFetch was used |
| 93 | + } finally { |
| 94 | + // Restore original fetch |
| 95 | + globalThis.fetch = originalFetch |
| 96 | + } |
| 97 | +}) |
| 98 | + |
| 99 | +test('handles array error with 404 status', async () => { |
| 100 | + // Mock the fetch response to return an array error with 404 |
| 101 | + const customFetch = jest.fn().mockImplementation(() => |
| 102 | + Promise.resolve({ |
| 103 | + ok: false, |
| 104 | + status: 404, |
| 105 | + statusText: 'Not Found', |
| 106 | + text: () => Promise.resolve('[]'), |
| 107 | + }) |
| 108 | + ) |
| 109 | + |
| 110 | + const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, { |
| 111 | + fetch: customFetch, |
| 112 | + }) |
| 113 | + |
| 114 | + const res = await postgrestWithCustomFetch.from('users').select() |
| 115 | + |
| 116 | + expect(res).toMatchInlineSnapshot(` |
| 117 | + Object { |
| 118 | + "count": null, |
| 119 | + "data": Array [], |
| 120 | + "error": null, |
| 121 | + "status": 200, |
| 122 | + "statusText": "OK", |
| 123 | + } |
| 124 | + `) |
| 125 | +}) |
| 126 | + |
| 127 | +test('handles empty body with 404 status', async () => { |
| 128 | + // Mock the fetch response to return an empty body with 404 |
| 129 | + const customFetch = jest.fn().mockImplementation(() => |
| 130 | + Promise.resolve({ |
| 131 | + ok: false, |
| 132 | + status: 404, |
| 133 | + statusText: 'Not Found', |
| 134 | + text: () => Promise.resolve(''), |
| 135 | + }) |
| 136 | + ) |
| 137 | + |
| 138 | + const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, { |
| 139 | + fetch: customFetch, |
| 140 | + }) |
| 141 | + |
| 142 | + const res = await postgrestWithCustomFetch.from('users').select() |
| 143 | + |
| 144 | + expect(res).toMatchInlineSnapshot(` |
| 145 | + Object { |
| 146 | + "count": null, |
| 147 | + "data": null, |
| 148 | + "error": null, |
| 149 | + "status": 204, |
| 150 | + "statusText": "No Content", |
| 151 | + } |
| 152 | + `) |
| 153 | +}) |
| 154 | + |
| 155 | +test('maybeSingle handles zero rows error', async () => { |
| 156 | + const customFetch = jest.fn().mockImplementation(() => |
| 157 | + Promise.resolve({ |
| 158 | + ok: false, |
| 159 | + status: 406, |
| 160 | + statusText: 'Not Acceptable', |
| 161 | + text: () => |
| 162 | + Promise.resolve( |
| 163 | + JSON.stringify({ |
| 164 | + code: 'PGRST116', |
| 165 | + details: '0 rows', |
| 166 | + hint: null, |
| 167 | + message: 'JSON object requested, multiple (or no) rows returned', |
| 168 | + }) |
| 169 | + ), |
| 170 | + }) |
| 171 | + ) |
| 172 | + |
| 173 | + const postgrestWithCustomFetch = new PostgrestClient<Database>(REST_URL, { |
| 174 | + fetch: customFetch, |
| 175 | + }) |
| 176 | + |
| 177 | + const res = await postgrestWithCustomFetch.from('users').select().maybeSingle() |
| 178 | + |
| 179 | + expect(res).toMatchInlineSnapshot(` |
| 180 | + Object { |
| 181 | + "count": null, |
| 182 | + "data": null, |
| 183 | + "error": null, |
| 184 | + "status": 200, |
| 185 | + "statusText": "OK", |
| 186 | + } |
| 187 | + `) |
| 188 | +}) |
| 189 | + |
| 190 | +test('connection error w/o throwing', async () => { |
| 191 | + const postgrest = new PostgrestClient<Database>('http://foo.invalid') |
| 192 | + let isErrorCaught = false |
| 193 | + await postgrest |
| 194 | + .from('users') |
| 195 | + .select() |
| 196 | + .then(undefined, () => { |
| 197 | + isErrorCaught = true |
| 198 | + }) |
| 199 | + expect(isErrorCaught).toBe(false) |
| 200 | +}) |
| 201 | + |
| 202 | +test('connection error w/ throwOnError', async () => { |
| 203 | + const postgrest = new PostgrestClient<Database>('http://foo.invalid') |
| 204 | + let isErrorCaught = false |
| 205 | + await postgrest |
| 206 | + .from('users') |
| 207 | + .select() |
| 208 | + .throwOnError() |
| 209 | + .then(undefined, () => { |
| 210 | + isErrorCaught = true |
| 211 | + }) |
| 212 | + expect(isErrorCaught).toBe(true) |
| 213 | +}) |
0 commit comments