Skip to content

Commit

Permalink
ADM-999 [frontend][backend]: fix the test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-yinyuan committed Sep 6, 2024
1 parent 398e550 commit b32834d
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IUseGetSourceControlConfigurationCrewInterface } from '@src/hooks/useGe
import { SourceControlConfiguration } from '@src/containers/MetricsStep/SouceControlConfiguration';
import { act, render, screen, waitFor, within } from '@testing-library/react';
import { LIST_OPEN, LOADING, REMOVE_BUTTON } from '@test/fixtures';
import { MetricsDataFailStatus } from '@src/constants/commons';
import { setupStore } from '@test/utils/setupStoreUtil';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
Expand All @@ -35,16 +36,37 @@ const mockInitRepoEffectResponse = {
isGetRepo: true,
isLoading: false,
getSourceControlRepoInfo: jest.fn(),
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};
const mockInitBranchEffectResponse = {
isLoading: false,
getSourceControlBranchInfo: jest.fn(),
isGetBranch: true,
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};
const mockInitCrewEffectResponse = {
isLoading: false,
getSourceControlCrewInfo: jest.fn(),
isGetAllCrews: true,
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};

let mockSourceControlSettings = mockInitSourceControlSettings;
Expand Down Expand Up @@ -221,4 +243,18 @@ describe('SourceControlConfiguration', () => {

expect(screen.getByText('Crew setting (optional)')).toBeInTheDocument();
});

it('should set error info when any request return error', () => {
mockOrganizationEffectResponse = {
...mockOrganizationEffectResponse,
info: {
code: 404,
errorTitle: 'error title',
errorMessage: 'error message',
},
};
setup();

expect(screen.getByLabelText('Error UI for pipeline settings')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IUseGetSourceControlConfigurationBranchInterface } from '@src/hooks/use
import { IUseGetSourceControlConfigurationRepoInterface } from '@src/hooks/useGetSourceControlConfigurationRepoEffect';
import { IUseGetSourceControlConfigurationCrewInterface } from '@src/hooks/useGetSourceControlConfigurationCrewEffect';
import { act, render, screen, within } from '@testing-library/react';
import { MetricsDataFailStatus } from '@src/constants/commons';
import { setupStore } from '@test/utils/setupStoreUtil';
import userEvent from '@testing-library/user-event';
import { LIST_OPEN, LOADING } from '@test/fixtures';
Expand All @@ -12,16 +13,37 @@ const mockInitRepoEffectResponse = {
isGetRepo: true,
isLoading: false,
getSourceControlRepoInfo: jest.fn(),
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};
const mockInitBranchEffectResponse = {
isLoading: false,
getSourceControlBranchInfo: jest.fn(),
isGetBranch: true,
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};
const mockInitCrewEffectResponse = {
isLoading: false,
getSourceControlCrewInfo: jest.fn(),
isGetAllCrews: true,
info: {
code: 200,
data: undefined,
errorTitle: '',
errorMessage: '',
},
stepFailedStatus: MetricsDataFailStatus.NotFailed,
};

let mockRepoEffectResponse: IUseGetSourceControlConfigurationRepoInterface = mockInitRepoEffectResponse;
Expand All @@ -34,6 +56,11 @@ let mockSelectSourceControlRepos = mockInitSelectSourceControlRepos;
const mockInitSelectSourceControlBranches = ['mockBranchName', 'mockBranchName1'];
let mockSelectSourceControlBranches = mockInitSelectSourceControlBranches;

const myDispatch = jest.fn();
jest.mock('@src/hooks', () => ({
...jest.requireActual('@src/hooks'),
useAppDispatch: () => myDispatch,
}));
jest.mock('@src/hooks/useGetSourceControlConfigurationRepoEffect', () => {
return {
useGetSourceControlConfigurationRepoEffect: () => mockRepoEffectResponse,
Expand All @@ -50,6 +77,10 @@ jest.mock('@src/hooks/useGetSourceControlConfigurationCrewEffect', () => {
};
});

jest.mock('@src/context/notification/NotificationSlice', () => ({
...jest.requireActual('@src/context/notification/NotificationSlice'),
addNotification: jest.fn(),
}));
jest.mock('@src/context/Metrics/metricsSlice', () => ({
...jest.requireActual('@src/context/Metrics/metricsSlice'),
selectSourceControlConfigurationSettings: jest.fn().mockImplementation(() => [
Expand Down Expand Up @@ -78,7 +109,11 @@ describe('SourceControlMetricSelection', () => {
mockSelectSourceControlBranches = mockInitSelectSourceControlBranches;
mockSelectSourceControlRepos = mockInitSelectSourceControlRepos;
});
afterEach(() => {
jest.clearAllMocks();
});
const onUpdateSourceControl = jest.fn();
const handleUpdateErrorInfo = jest.fn();
const setup = (isDuplicated: boolean = false) => {
const sourceControlSetting = {
id: 0,
Expand All @@ -96,6 +131,7 @@ describe('SourceControlMetricSelection', () => {
isDuplicated={isDuplicated}
setLoadingCompletedNumber={jest.fn()}
totalSourceControlNumber={1}
handleUpdateErrorInfo={handleUpdateErrorInfo}
/>
</Provider>,
);
Expand Down Expand Up @@ -188,4 +224,43 @@ describe('SourceControlMetricSelection', () => {
expect(getSourceControlBranchInfoFunction).toHaveBeenCalledTimes(1);
expect(getSourceControlCrewInfoFunction).toHaveBeenCalledTimes(2);
});

it('should add partial failed 4xx notification when any failed status is PartialFailed4xx', async () => {
mockCrewEffectResponse = {
...mockCrewEffectResponse,
stepFailedStatus: MetricsDataFailStatus.PartialFailed4xx,
};
setup();

expect(myDispatch).toHaveBeenCalledTimes(1);
});

it('should add partial failed 4xx notification when any failed status is PartialFailedNoCards', async () => {
mockCrewEffectResponse = {
...mockCrewEffectResponse,
stepFailedStatus: MetricsDataFailStatus.PartialFailedNoCards,
};
setup();

expect(myDispatch).toHaveBeenCalledTimes(1);
});

it('should set error info when any request return error', () => {
mockRepoEffectResponse = {
...mockRepoEffectResponse,
info: {
code: 404,
errorTitle: 'error title',
errorMessage: 'error message',
},
};
setup();

expect(handleUpdateErrorInfo).toHaveBeenCalledTimes(1);
expect(handleUpdateErrorInfo).toBeCalledWith({
code: 404,
errorTitle: 'error title',
errorMessage: 'error message',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import { useGetSourceControlConfigurationBranchEffect } from '@src/hooks/useGetS
import { sourceControlClient } from '@src/clients/sourceControl/SourceControlClient';
import { MOCK_GITHUB_GET_BRANCHES_RESPONSE } from '@test/fixtures';
import { act, renderHook, waitFor } from '@testing-library/react';
import { MetricsDataFailStatus } from '@src/constants/commons';
import { setupStore } from '@test/utils/setupStoreUtil';
import React, { ReactNode } from 'react';
import { Provider } from 'react-redux';
import { HttpStatusCode } from 'axios';

const mockDispatch = jest.fn();
const store = setupStore();
jest.mock('@src/hooks', () => ({
...jest.requireActual('@src/hooks'),
useAppDispatch: () => jest.fn(),
}));
jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: () => mockDispatch,
Expand All @@ -27,31 +32,40 @@ const Wrapper = ({ children }: { children: ReactNode }) => {
const clientSpy = jest.fn();
const mockRepo = jest.fn().mockImplementation(() => {
clientSpy();
return {
code: HttpStatusCode.Ok,
data: MOCK_GITHUB_GET_BRANCHES_RESPONSE,
errorTittle: '',
errorMessage: '',
};
});

beforeEach(() => {
sourceControlClient.getBranch = mockRepo;
clientSpy.mockClear();
return new Promise(() => {
return {
code: HttpStatusCode.Ok,
data: MOCK_GITHUB_GET_BRANCHES_RESPONSE,
errorTittle: '',
errorMessage: '',
};
});
});

describe('use get source control configuration branch info side effect', () => {
beforeEach(() => {
sourceControlClient.getBranch = mockRepo;
clientSpy.mockClear();
});

it('should init data state when render hook', async () => {
const { result } = renderHook(() => useGetSourceControlConfigurationBranchEffect(), { wrapper: Wrapper });
expect(result.current.isLoading).toBeFalsy();
expect(result.current.isGetBranch).toBeFalsy();
});

it('should return success data and loading state when client goes happy path', async () => {
it('should return success data and loading state when client return 200', async () => {
const { result } = renderHook(() => useGetSourceControlConfigurationBranchEffect(), { wrapper: Wrapper });
const mockOrganization = 'mockOrg';
const mockRepo = 'mockRepo';

sourceControlClient.getBranch = jest.fn().mockImplementation(() => {
return Promise.resolve({
code: 200,
data: {
name: ['test-branch1', 'test-branch2'],
},
});
});
await act(async () => {
result.current.getSourceControlBranchInfo(mockOrganization, mockRepo, 1);
});
Expand All @@ -60,6 +74,64 @@ describe('use get source control configuration branch info side effect', () => {
expect(result.current.isLoading).toBeFalsy();
expect(result.current.isGetBranch).toBeTruthy();
});
expect(clientSpy).toBeCalled();

expect(sourceControlClient.getBranch).toBeCalled();
});

it('should set error info when client dont return 200', async () => {
const { result } = renderHook(() => useGetSourceControlConfigurationBranchEffect(), { wrapper: Wrapper });
const mockOrganization = 'mockOrg';
const mockRepo = 'mockRepo';
sourceControlClient.getBranch = jest.fn().mockImplementation(() => {
return Promise.resolve({
code: 400,
});
});
await act(async () => {
result.current.getSourceControlBranchInfo(mockOrganization, mockRepo, 1);
});

expect(sourceControlClient.getBranch).toBeCalled();
expect(result.current.info).toEqual({
code: 400,
});
});

it('should set error step failed status to PartialFailed4xx when getting branch response is failed and client return 400', async () => {
const { result } = renderHook(() => useGetSourceControlConfigurationBranchEffect(), { wrapper: Wrapper });
const mockOrganization = 'mockOrg';
const mockRepo = 'mockRepo';
sourceControlClient.getBranch = jest.fn().mockImplementation(() => {
return Promise.reject({
reason: {
code: 400,
},
});
});
await act(async () => {
result.current.getSourceControlBranchInfo(mockOrganization, mockRepo, 1);
});

expect(sourceControlClient.getBranch).toBeCalled();
expect(result.current.stepFailedStatus).toEqual(MetricsDataFailStatus.PartialFailed4xx);
});

it('should set error step failed status to PartialFailedTimeout when getting branch response is failed and client dont return 400', async () => {
const { result } = renderHook(() => useGetSourceControlConfigurationBranchEffect(), { wrapper: Wrapper });
const mockOrganization = 'mockOrg';
const mockRepo = 'mockRepo';
sourceControlClient.getBranch = jest.fn().mockImplementation(() => {
return Promise.reject({
reason: {
code: 404,
},
});
});
await act(async () => {
result.current.getSourceControlBranchInfo(mockOrganization, mockRepo, 1);
});

expect(sourceControlClient.getBranch).toBeCalled();
expect(result.current.stepFailedStatus).toEqual(MetricsDataFailStatus.PartialFailedTimeout);
});
});
Loading

0 comments on commit b32834d

Please sign in to comment.