import { AxiosError } from 'axios'; import { searchDistinct } from '../../src/index'; import { TestGrid } from '../__grids__/TestGrid'; import { QueryDistinctObject } from '../../src/types'; import { bootstrapIntegrationTests, cleanupIntegrationTests, } from './integration.utils'; jest.disableAutomock(); jest.unmock('axios'); jest.setTimeout(10000); let TEST_GRID_ID: string; const queryDistinctObject: QueryDistinctObject = { query: {}, distinct: { columnNames: ['String Column'], }, }; const beforeEachRun = async () => { jest.resetModules(); const { testGridId } = await bootstrapIntegrationTests(); TEST_GRID_ID = testGridId; }; const afterEachRun = async () => { await cleanupIntegrationTests(TEST_GRID_ID); }; describe('Search Distinct', () => { beforeEach(() => beforeEachRun()); afterEach(() => afterEachRun()); describe('Positive Test Cases', () => { it('Should Return Distinct Matching Values Successfully', async () => { // Given const response = { matchingValues: ['Example String'], }; // When const { data, error } = await searchDistinct( queryDistinctObject, TEST_GRID_ID, ); // Then expect(error).toEqual(undefined); expect(data).toEqual(response); }); }); describe('Negative Test Cases', () => { it('Should Reject Invalid Grid Id', async () => { // Given const errorObject = { errorMessage: 'Grid not found', otherDetails: {}, errorType: 'DATAERROR', recoverable: true, }; // When const { data, error } = await searchDistinct( queryDistinctObject, 'INVALID_GRID_ID', ); // Then expect(data).toEqual(undefined); expect((error as AxiosError).response.data).toEqual(errorObject); }); it('Should Reject Invalid Share Id', async () => { // Given const errorObject = { errorMessage: 'System error. Please contact admin.', otherDetails: {}, errorType: 'SYSTEMERROR', recoverable: false, }; // When const { data, error } = await searchDistinct( queryDistinctObject, TEST_GRID_ID, { shareId: 'INVALID_SHARE_ID', }, ); // Then expect(data).toEqual(undefined); expect((error as AxiosError).response.data).toEqual(errorObject); }); it('Should Reject Invalid Auth Id in Production', async () => { // Given const errorObject = { errorMessage: 'System error. Please contact admin.', otherDetails: {}, errorType: 'SYSTEMERROR', recoverable: false, }; // When const { data, error } = await searchDistinct( queryDistinctObject, TEST_GRID_ID, { authId: 'INVALID_AUTHID', }, ); // Then expect(data).toEqual(undefined); expect((error as AxiosError).response.data).toEqual(errorObject); }); it('Should Reject Invalid Auth Id in QA', async () => { // Given const errorObject = { errorMessage: 'Grid not found', otherDetails: {}, errorType: 'DATAERROR', recoverable: true, }; // When const { data, error } = await searchDistinct( queryDistinctObject, TEST_GRID_ID, { authId: 'INVALID_AUTHID', qa: true, }, ); // Then expect(data).toEqual(undefined); expect((error as AxiosError).response.data).toEqual(errorObject); }); }); });