From 712751539deccbd5ed0bd8f7e4b6ec3daaae7bcf Mon Sep 17 00:00:00 2001 From: asalem Date: Wed, 10 Jun 2020 10:56:14 -0700 Subject: [PATCH] refactor(test): reverted tests in favor of integration tests for submitQueryButton component --- ui/src/shared/apis/query.test.ts | 89 -------------------------------- ui/src/shared/apis/query.ts | 23 +++------ 2 files changed, 8 insertions(+), 104 deletions(-) delete mode 100644 ui/src/shared/apis/query.test.ts diff --git a/ui/src/shared/apis/query.test.ts b/ui/src/shared/apis/query.test.ts deleted file mode 100644 index deede4fda8b..00000000000 --- a/ui/src/shared/apis/query.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import fetchMock from 'jest-fetch-mock' -fetchMock.enableMocks() - -import {getQueryResult} from 'src/shared/apis/query' - -// Types -import {VariableAssignment} from 'src/types' - -// Utils -import {buildVarsOption} from 'src/variables/utils/buildVarsOption' - -const variableAssignments: VariableAssignment[] = [ - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'bucket'}, - init: {type: 'StringLiteral', value: 'Futile Devices'}, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'base_query'}, - init: {type: 'StringLiteral', value: ''}, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'values'}, - init: {type: 'StringLiteral', value: 'system'}, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'broker_host'}, - init: {type: 'StringLiteral', value: ''}, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'timeRangeStart'}, - init: { - type: 'UnaryExpression', - operator: '-', - argument: { - type: 'DurationLiteral', - values: [{magnitude: 1, unit: 'h'}], - }, - }, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'timeRangeStop'}, - init: { - type: 'CallExpression', - callee: {type: 'Identifier', name: 'now'}, - }, - }, - { - type: 'VariableAssignment', - id: {type: 'Identifier', name: 'windowPeriod'}, - init: { - type: 'DurationLiteral', - values: [{magnitude: 10000, unit: 'ms'}], - }, - }, -] - -const extern = buildVarsOption(variableAssignments) -const orgID = '674b23253171ee69' -const query = `from(bucket: "Default Bucket") -|> range(start: v.timeRangeStart, stop: v.timeRangeStop) -|> filter(fn: (r) => r["_measurement"] == "cpu") -|> filter(fn: (r) => r["_field"] == "usage_user")` - -describe('Shared.APIs.Query.GetQueryResult', () => { - it('Should allow queries to be cancellable with abortController', () => { - const abortController = new AbortController() - abortController.abort() - expect(() => getQueryResult(orgID, query, abortController, extern)).toThrow( - 'The operation was aborted.' - ) - }) - it("Should not allow queries to be cancelled if they've already resolved", async () => { - const abortController = new AbortController() - const promise = getQueryResult(orgID, query, abortController, extern) - - expect(promise).toBeInstanceOf(Promise) - - abortController.abort() - const resolved = await promise - expect(resolved).not.toBeInstanceOf(Promise) - expect(resolved.status).toBe(200) - }) -}) diff --git a/ui/src/shared/apis/query.ts b/ui/src/shared/apis/query.ts index a4ace92a482..4d069319dda 100644 --- a/ui/src/shared/apis/query.ts +++ b/ui/src/shared/apis/query.ts @@ -33,12 +33,12 @@ export interface RunQueryErrorResult { code?: string } -export const getQueryResult = ( +export const runQuery = ( orgID: string, query: string, - controller: AbortController, - extern?: File -): Promise => { + extern?: File, + abortController?: AbortController +): CancelBox => { const url = `${API_BASE_PATH}api/v2/query?${new URLSearchParams({orgID})}` const headers = { @@ -52,23 +52,16 @@ export const getQueryResult = ( dialect: {annotations: ['group', 'datatype', 'default']}, } - return fetch(url, { + const controller = abortController || new AbortController() + + const response = fetch(url, { method: 'POST', headers, body: JSON.stringify(body), signal: controller.signal, }) -} - -export const runQuery = ( - orgID: string, - query: string, - extern?: File, - abortController?: AbortController -): CancelBox => { - const controller = abortController || new AbortController() - const promise = getQueryResult(orgID, query, controller, extern) + const promise = response .then(processResponse) .catch(e => e.name === 'AbortError'