superset/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-core/test/connection/SupersetClient.test.ts

126 lines
5.1 KiB
TypeScript
Raw Normal View History

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import fetchMock from 'fetch-mock';
import { SupersetClient, SupersetClientClass } from '@superset-ui/core/src/connection';
import { LOGIN_GLOB } from './fixtures/constants';
describe('SupersetClient', () => {
beforeAll(() => {
fetchMock.get(LOGIN_GLOB, { result: '' });
});
afterAll(fetchMock.restore);
afterEach(SupersetClient.reset);
it('exposes reset, configure, init, get, post, isAuthenticated, and reAuthenticate methods', () => {
expect(typeof SupersetClient.configure).toBe('function');
expect(typeof SupersetClient.init).toBe('function');
expect(typeof SupersetClient.get).toBe('function');
expect(typeof SupersetClient.post).toBe('function');
expect(typeof SupersetClient.isAuthenticated).toBe('function');
expect(typeof SupersetClient.reAuthenticate).toBe('function');
expect(typeof SupersetClient.request).toBe('function');
expect(typeof SupersetClient.reset).toBe('function');
});
it('throws if you call init, get, post, isAuthenticated, or reAuthenticate before configure', () => {
expect(SupersetClient.init).toThrow();
expect(SupersetClient.get).toThrow();
expect(SupersetClient.post).toThrow();
expect(SupersetClient.isAuthenticated).toThrow();
expect(SupersetClient.reAuthenticate).toThrow();
expect(SupersetClient.request).toThrow();
expect(SupersetClient.configure).not.toThrow();
});
// this also tests that the ^above doesn't throw if configure is called appropriately
it('calls appropriate SupersetClient methods when configured', async () => {
expect.assertions(15);
const mockGetUrl = '/mock/get/url';
const mockPostUrl = '/mock/post/url';
const mockRequestUrl = '/mock/request/url';
const mockPutUrl = '/mock/put/url';
const mockDeleteUrl = '/mock/delete/url';
const mockGetPayload = { get: 'payload' };
const mockPostPayload = { post: 'payload' };
const mockDeletePayload = { delete: 'ok' };
const mockPutPayload = { put: 'ok' };
fetchMock.get(mockGetUrl, mockGetPayload);
fetchMock.post(mockPostUrl, mockPostPayload);
fetchMock.delete(mockDeleteUrl, mockDeletePayload);
fetchMock.put(mockPutUrl, mockPutPayload);
fetchMock.get(mockRequestUrl, mockGetPayload);
const initSpy = jest.spyOn(SupersetClientClass.prototype, 'init');
const getSpy = jest.spyOn(SupersetClientClass.prototype, 'get');
const postSpy = jest.spyOn(SupersetClientClass.prototype, 'post');
const putSpy = jest.spyOn(SupersetClientClass.prototype, 'put');
const deleteSpy = jest.spyOn(SupersetClientClass.prototype, 'delete');
const authenticatedSpy = jest.spyOn(SupersetClientClass.prototype, 'isAuthenticated');
const csrfSpy = jest.spyOn(SupersetClientClass.prototype, 'getCSRFToken');
const requestSpy = jest.spyOn(SupersetClientClass.prototype, 'request');
SupersetClient.configure({});
await SupersetClient.init();
expect(initSpy).toHaveBeenCalledTimes(1);
expect(authenticatedSpy).toHaveBeenCalledTimes(2);
expect(csrfSpy).toHaveBeenCalledTimes(1);
await SupersetClient.get({ url: mockGetUrl });
await SupersetClient.post({ url: mockPostUrl });
await SupersetClient.delete({ url: mockDeleteUrl });
await SupersetClient.put({ url: mockPutUrl });
await SupersetClient.request({ url: mockRequestUrl });
// Make sure network calls have Accept: 'application/json' in headers
const networkCalls = [mockGetUrl, mockPostUrl, mockRequestUrl, mockPutUrl, mockDeleteUrl];
networkCalls.map((url: string) =>
expect(fetchMock.calls(url)[0][1]?.headers).toStrictEqual({
Accept: 'application/json',
'X-CSRFToken': '',
}),
);
SupersetClient.isAuthenticated();
await SupersetClient.reAuthenticate();
expect(initSpy).toHaveBeenCalledTimes(2);
expect(deleteSpy).toHaveBeenCalledTimes(1);
expect(putSpy).toHaveBeenCalledTimes(1);
feat(chart): Add `<ChartDataProvider />` (#120) * docs: [demo][connection] add ConfigureCORS story for testing CORS * docs: [demo][ConfigureCORS] better instructions * feat: [chart] add mvp DataProvider component * docs: better CORS story, update webpack for @babel/polyfill * docs: [chart] add DataProvider story with WordCloudPlugin * docs: [chart] add DataProvider deets to Readme * test(chart): move SuperChart.test.jsx => .tsx and instead use @ts-ignore * fix(connection): point interface.request to client.request * feat(chart): re-write DataProvider as ChartDataProvider * docs(demo): re-write LegacyWordCloudStories => ChartDataProviderStories * refactor(chart): use IDENTITY as ChartPlugin buildQuery default * feat(chart): support legacy + v1 loadQueryData endpoints in ChartClient * docs(demo): add sankey + sunburst plugins to ChartDataProvider story * style(chart): run prettier on SuperChart * feat(chart): export QueryData type from models/ChartProps * feat(chart): export Metrics and BaseFormData from types/ChartFormData * feat(chart): add request option overrides in ChartDataProvider * fix(chart): use Partial<> for ChartClient request option overrides * test(chart): add ChartDataProvider tests * build: include demo pkg in type script * build: move storybook/mocks to test/fixtures * build: move json-bigint TS declaration to root * test(chart): clean up ChartDataProvider test TS * chore(chart): lint fix SuperChart * fix(chart): set ChartPlugin.buildQuery default back to undefined * test(connection): fix expected Client.get call count * test(chart): fix ChartClient tests and add test for legacy API * fix(chart): uninitialized typo, change fetching => loading * docs(chart): update README to final ChartDataProvider API * docs(chart): fix typo * test(chart): get ChartDataProvider to one hundo * feat(chart): add and export more meaningful Datasource type * feat(chart): use Datasource type in ChartClient
2019-03-19 17:58:20 -04:00
expect(getSpy).toHaveBeenCalledTimes(1);
expect(postSpy).toHaveBeenCalledTimes(1);
expect(requestSpy).toHaveBeenCalledTimes(5); // request rewires to get
expect(csrfSpy).toHaveBeenCalledTimes(2); // from init() + reAuthenticate()
initSpy.mockRestore();
getSpy.mockRestore();
putSpy.mockRestore();
deleteSpy.mockRestore();
requestSpy.mockRestore();
postSpy.mockRestore();
authenticatedSpy.mockRestore();
csrfSpy.mockRestore();
fetchMock.reset();
});
});