From dc718da01bfc63662c564dae2fd88ccfef79ece9 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Tue, 29 Oct 2024 17:54:07 -0400 Subject: [PATCH] migrate query tests --- test/api/query.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/api/query.test.ts diff --git a/test/api/query.test.ts b/test/api/query.test.ts new file mode 100644 index 0000000..f9880ef --- /dev/null +++ b/test/api/query.test.ts @@ -0,0 +1,37 @@ +import {test, vi, expect, beforeEach, afterEach} from 'vitest'; +import {query} from '@carto/api-client'; + +const QUERY_RESPONSE = [{id: 1, value: 'string'}]; + +beforeEach(() => { + const mockFetch = vi + .fn() + .mockReturnValue( + Promise.resolve({ok: true, json: () => Promise.resolve(QUERY_RESPONSE)}) + ); + + vi.stubGlobal('fetch', mockFetch); +}); + +afterEach(() => void vi.restoreAllMocks()); + +test('query', async (t) => { + const mockFetch = vi.mocked(fetch); + + const response = await query({ + connectionName: 'carto_dw', + clientId: 'CUSTOM_CLIENT', + accessToken: '', + sqlQuery: 'SELECT * FROM a.b.h3_table', + }); + + expect(mockFetch).toHaveBeenCalledTimes(1); + + const [queryCall] = mockFetch.mock.calls; + + expect(queryCall[0]).toMatch(/v3\/sql\/carto_dw\/query/); + expect(queryCall[0]).toMatch(/q=SELECT\+\*\+FROM\+a\.b\.h3_table/); + expect(queryCall[0]).toMatch(/client\=CUSTOM_CLIENT/); + + expect(response).toEqual(QUERY_RESPONSE); +});