Skip to content

Commit

Permalink
Add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
kgabryje committed Apr 2, 2024
1 parent c323f24 commit 362f808
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@ const mockLoadQueryData = jest.fn<Promise<unknown>, unknown[]>(
createArrayPromise,
);

const actual = jest.requireActual('../../../src/chart/clients/ChartClient');
// ChartClient is now a mock
jest.mock('../../../src/chart/clients/ChartClient', () =>
jest.fn().mockImplementation(() => ({
loadDatasource: mockLoadDatasource,
loadFormData: mockLoadFormData,
loadQueryData: mockLoadQueryData,
})),
);
jest.spyOn(actual, 'default').mockImplementation(() => ({
loadDatasource: mockLoadDatasource,
loadFormData: mockLoadFormData,
loadQueryData: mockLoadQueryData,
}));

const ChartClientMock = ChartClient as jest.Mock<ChartClient>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ import {
SharedLabelColor,
SharedLabelColorSource,
} from '@superset-ui/core';
import { getAnalogousColors } from '../../src/color/utils';

jest.mock('../../src/color/utils', () => ({
getAnalogousColors: jest
.fn()
.mockImplementation(() => ['red', 'green', 'blue']),
}));
const actual = jest.requireActual('../../src/color/utils');
const getAnalogousColorsSpy = jest
.spyOn(actual, 'getAnalogousColors')
.mockImplementation(() => ['red', 'green', 'blue']);

describe('SharedLabelColor', () => {
beforeAll(() => {
Expand Down Expand Up @@ -161,7 +159,7 @@ describe('SharedLabelColor', () => {
sharedLabelColor.updateColorMap('', 'testColors');
const colorMap = sharedLabelColor.getColorMap();
expect(Object.fromEntries(colorMap)).not.toEqual({});
expect(getAnalogousColors).not.toBeCalled();
expect(getAnalogousColorsSpy).not.toBeCalled();
});

it('should use analagous colors', () => {
Expand All @@ -176,7 +174,7 @@ describe('SharedLabelColor', () => {
sharedLabelColor.updateColorMap('', 'testColors');
const colorMap = sharedLabelColor.getColorMap();
expect(Object.fromEntries(colorMap)).not.toEqual({});
expect(getAnalogousColors).toBeCalled();
expect(getAnalogousColorsSpy).toBeCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"compilerOptions": {
"composite": false,
"emitDeclarationOnly": false,
"noEmit": true,
"rootDir": "."
// "composite": false,
// "emitDeclarationOnly": false,
// "noEmit": true,
// "rootDir": "."
},
"extends": "../../../tsconfig.json",
"include": ["**/*", "../types/**/*", "../../../types/**/*"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,67 @@
* under the License.
*/

import fetchMock from 'fetch-mock';
import {
formatTimeRange,
buildTimeRangeString,
fetchTimeRange,
} from '../../src/utils/fetchTimeRange';
import * as uiCore from '@superset-ui/core';

describe('buildTimeRangeString', () => {
it('generates proper time range string', () => {
expect(
buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'),
).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00');
expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe(
' : 2020-07-30T00:00:00',
);
expect(buildTimeRangeString('', '')).toBe(' : ');
afterEach(fetchMock.restore);

it('generates proper time range string', () => {
expect(
buildTimeRangeString('2010-07-30T00:00:00', '2020-07-30T00:00:00'),
).toBe('2010-07-30T00:00:00 : 2020-07-30T00:00:00');
expect(buildTimeRangeString('', '2020-07-30T00:00:00')).toBe(
' : 2020-07-30T00:00:00',
);
expect(buildTimeRangeString('', '')).toBe(' : ');
});

it('generates a readable time range', () => {
expect(formatTimeRange('Last 7 days')).toBe('Last 7 days');
expect(formatTimeRange('No filter')).toBe('No filter');
expect(formatTimeRange('Yesterday : Tomorrow')).toBe(
'Yesterday ≤ col < Tomorrow',
);
expect(formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00')).toBe(
'2010-07-30 ≤ col < 2020-07-30',
);
expect(formatTimeRange('2010-07-30T01:00:00 : ')).toBe(
'2010-07-30T01:00:00 ≤ col < ∞',
);
expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe(
'-∞ ≤ col < 2020-07-30',
);
});

it('returns a formatted time range from response', async () => {
fetchMock.get("glob:*/api/v1/time_range/?q='Last+day'", {
result: [
{
since: '2021-04-13T00:00:00',
until: '2021-04-14T00:00:00',
timeRange: 'Last day',
},
],
});

const timeRange = await fetchTimeRange('Last day', 'temporal_col');
expect(timeRange).toEqual({
value: '2021-04-13 ≤ temporal_col < 2021-04-14',
});
});

describe('formatTimeRange', () => {
it('generates a readable time range', () => {
expect(formatTimeRange('Last 7 days')).toBe('Last 7 days');
expect(formatTimeRange('No filter')).toBe('No filter');
expect(formatTimeRange('Yesterday : Tomorrow')).toBe(
'Yesterday ≤ col < Tomorrow',
);
expect(formatTimeRange('2010-07-30T00:00:00 : 2020-07-30T00:00:00')).toBe(
'2010-07-30 ≤ col < 2020-07-30',
);
expect(formatTimeRange('2010-07-30T01:00:00 : ')).toBe(
'2010-07-30T01:00:00 ≤ col < ∞',
);
expect(formatTimeRange(' : 2020-07-30T00:00:00')).toBe(
'-∞ ≤ col < 2020-07-30',
);
it('returns a formatted error message from response', async () => {
fetchMock.get("glob:*/api/v1/time_range/?q='Last+day'", {
status: 500,
});

const timeRange = await fetchTimeRange('Last day', 'temporal_col');
expect(timeRange).toEqual({
error: 'Internal Server Error',
});
});

0 comments on commit 362f808

Please sign in to comment.