From ff75fbf99e9ce748f8f7cf82278cf47f16296060 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 29 Jan 2024 22:57:20 +0100 Subject: [PATCH] Convert query.sec.spec.ts to ts --- .../{query.seq.spec.js => query.seq.spec.ts} | 68 ++++++++++++------- .../src/tests/utilities/TestConfiguration.ts | 4 +- .../server/src/tests/utilities/structures.ts | 5 +- 3 files changed, 49 insertions(+), 28 deletions(-) rename packages/server/src/api/routes/tests/{query.seq.spec.js => query.seq.spec.ts} (91%) diff --git a/packages/server/src/api/routes/tests/query.seq.spec.js b/packages/server/src/api/routes/tests/query.seq.spec.ts similarity index 91% rename from packages/server/src/api/routes/tests/query.seq.spec.js rename to packages/server/src/api/routes/tests/query.seq.spec.ts index b15a5f4d6d..5a3219d08f 100644 --- a/packages/server/src/api/routes/tests/query.seq.spec.js +++ b/packages/server/src/api/routes/tests/query.seq.spec.ts @@ -1,4 +1,4 @@ -const tk = require("timekeeper") +import tk from "timekeeper" tk.freeze(Date.now()) // Mock out postgres for this @@ -17,16 +17,21 @@ jest.mock("@budibase/backend-core", () => { }, } }) -const setup = require("./utilities") -const { checkBuilderEndpoint } = require("./utilities/TestFunctions") -const { checkCacheForDynamicVariable } = require("../../../threads/utils") +import * as setup from "./utilities" +import { checkBuilderEndpoint } from "./utilities/TestFunctions" +import { checkCacheForDynamicVariable } from "../../../threads/utils" const { basicQuery, basicDatasource } = setup.structures -const { events, db: dbCore } = require("@budibase/backend-core") +import { events, db as dbCore } from "@budibase/backend-core" +import { Datasource, Query, SourceName } from "@budibase/types" + +const mockIsProdAppID = dbCore.isProdAppID as jest.MockedFunction< + typeof dbCore.isProdAppID +> describe("/queries", () => { let request = setup.getRequest() let config = setup.getConfig() - let datasource, query + let datasource: Datasource & Required>, query: Query afterAll(setup.afterAll) @@ -44,14 +49,14 @@ describe("/queries", () => { const datasource = await config.createDatasource({ datasource: { ...basicDatasource().datasource, - source: "INVALID_INTEGRATION", + source: "INVALID_INTEGRATION" as SourceName, }, }) const query = await config.createQuery() return { datasource, query } } - const createQuery = async query => { + const createQuery = async (query: Query) => { return request .post(`/api/queries`) .send(query) @@ -67,7 +72,7 @@ describe("/queries", () => { jest.clearAllMocks() const res = await createQuery(query) - expect(res.res.statusMessage).toEqual( + expect((res as any).res.statusMessage).toEqual( `Query ${query.name} saved successfully.` ) expect(res.body).toEqual({ @@ -92,7 +97,7 @@ describe("/queries", () => { query._rev = res.body._rev await createQuery(query) - expect(res.res.statusMessage).toEqual( + expect((res as any).res.statusMessage).toEqual( `Query ${query.name} saved successfully.` ) expect(res.body).toEqual({ @@ -168,8 +173,8 @@ describe("/queries", () => { it("should remove sensitive info for prod apps", async () => { // Mock isProdAppID to pretend we are using a prod app - dbCore.isProdAppID.mockClear() - dbCore.isProdAppID.mockImplementation(() => true) + mockIsProdAppID.mockClear() + mockIsProdAppID.mockImplementation(() => true) const query = await config.createQuery() const res = await request @@ -184,7 +189,7 @@ describe("/queries", () => { // Reset isProdAppID mock expect(dbCore.isProdAppID).toHaveBeenCalledTimes(1) - dbCore.isProdAppID.mockImplementation(() => false) + mockIsProdAppID.mockImplementation(() => false) }) }) @@ -211,10 +216,11 @@ describe("/queries", () => { }) it("should apply authorization to endpoint", async () => { + const query = await config.createQuery() await checkBuilderEndpoint({ config, method: "DELETE", - url: `/api/queries/${config._id}/${config._rev}`, + url: `/api/queries/${query._id}/${query._rev}`, }) }) }) @@ -284,8 +290,8 @@ describe("/queries", () => { }) describe("variables", () => { - async function preview(datasource, fields) { - return config.previewQuery(request, config, datasource, fields) + async function preview(datasource: Datasource, fields: any) { + return config.previewQuery(request, config, datasource, fields, undefined) } it("should work with static variables", async () => { @@ -370,11 +376,19 @@ describe("/queries", () => { }) describe("Current User Request Mapping", () => { - async function previewGet(datasource, fields, params) { + async function previewGet( + datasource: Datasource, + fields: any, + params: any + ) { return config.previewQuery(request, config, datasource, fields, params) } - async function previewPost(datasource, fields, params) { + async function previewPost( + datasource: Datasource, + fields: any, + params: any + ) { return config.previewQuery( request, config, @@ -394,14 +408,18 @@ describe("/queries", () => { emailHdr: "{{[user].[email]}}", }, }) - const res = await previewGet(datasource, { - path: "www.google.com", - queryString: "email={{[user].[email]}}", - headers: { - queryHdr: "{{[user].[firstName]}}", - secondHdr: "1234", + const res = await previewGet( + datasource, + { + path: "www.google.com", + queryString: "email={{[user].[email]}}", + headers: { + queryHdr: "{{[user].[firstName]}}", + secondHdr: "1234", + }, }, - }) + undefined + ) const parsedRequest = JSON.parse(res.body.extra.raw) expect(parsedRequest.opts.headers).toEqual({ diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 61ab750c4a..58e1ae9e0e 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -794,11 +794,11 @@ class TestConfiguration { async createDatasource(config?: { datasource: Datasource - }): Promise { + }): Promise>> { config = config || basicDatasource() const response = await this.api.datasource.create(config.datasource) this.datasource = response - return this.datasource! + return { ...this.datasource, _id: this.datasource!._id! } } async updateDatasource(datasource: Datasource): Promise { diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 80aad3c1e2..cd2389e2eb 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -23,6 +23,7 @@ import { TableSourceType, AutomationIOType, } from "@budibase/types" +import { Query } from "@budibase/types" const { BUILTIN_ROLE_IDS } = roles @@ -360,7 +361,7 @@ export function basicDatasource(): { datasource: Datasource } { } } -export function basicQuery(datasourceId: string) { +export function basicQuery(datasourceId: string): Query { return { datasourceId: datasourceId, name: "New Query", @@ -368,6 +369,8 @@ export function basicQuery(datasourceId: string) { fields: {}, schema: {}, queryVerb: "read", + transformer: null, + readable: true, } }