Skip to content

Commit 3adc9ea

Browse files
committed
[test]: Update unit tests
1 parent 4042b8a commit 3adc9ea

File tree

15 files changed

+541
-153
lines changed

15 files changed

+541
-153
lines changed

packages/base/src/App.ce.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ describe('test App ce', () => {
5959
(useSelector as jest.Mock).mockImplementation((selector) => {
6060
return selector({
6161
user: {
62-
token: 'AAh32ffdswt'
62+
token: 'AAh32ffdswt',
63+
isLoggingIn: false
6364
},
6465
nav: {
6566
modalStatus: {

packages/base/src/App.test.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ describe('App', () => {
9393
(useSelector as jest.Mock).mockImplementation((selector) => {
9494
return selector({
9595
user: {
96-
token: 'AAh32ffdswt'
96+
token: 'AAh32ffdswt',
97+
isLoggingIn: false
9798
},
9899
nav: {
99100
modalStatus: {
@@ -137,7 +138,8 @@ describe('App', () => {
137138
(useSelector as jest.Mock).mockImplementation((selector) => {
138139
return selector({
139140
user: {
140-
token: ''
141+
token: '',
142+
isLoggingIn: false
141143
}
142144
});
143145
});
@@ -210,12 +212,16 @@ describe('App', () => {
210212
(useSelector as jest.Mock).mockImplementation((selector) => {
211213
return selector({
212214
user: {
213-
token: ''
215+
token: '',
216+
isLoggingIn: false
214217
},
215218
nav: {
216219
modalStatus: {
217220
[ModalName.Company_Notice]: false
218221
}
222+
},
223+
availabilityZone: {
224+
availabilityZoneTips: []
219225
}
220226
});
221227
});
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { act, cleanup } from '@testing-library/react';
2+
import { superRenderHook } from '@actiontech/shared/lib/testUtil/superRender';
3+
import { useDispatch, useSelector } from 'react-redux';
4+
import {
5+
baseMockApi,
6+
createSpySuccessResponse
7+
} from '@actiontech/shared/lib/testUtil/mockApi';
8+
import useNavigateToWorkbench from '.';
9+
import { mockUseRecentlySelectedZone } from '../../testUtils/mockHooks/mockUseRecentlySelectedZone';
10+
import { mockGatewayTipsData } from '@actiontech/shared/lib/testUtil/mockApi/base/gateway/data';
11+
12+
jest.mock('react-redux', () => ({
13+
...jest.requireActual('react-redux'),
14+
useDispatch: jest.fn(),
15+
useSelector: jest.fn()
16+
}));
17+
18+
describe('useNavigateToWorkbench', () => {
19+
const dispatchSpy = jest.fn();
20+
let getGatewayTipsSpy: jest.SpyInstance;
21+
let getSQLQueryConfigurationSpy: jest.SpyInstance;
22+
23+
beforeEach(() => {
24+
(useDispatch as jest.Mock).mockImplementation(() => dispatchSpy);
25+
(useSelector as jest.Mock).mockImplementation((selector) => {
26+
return selector({
27+
availabilityZone: {
28+
availabilityZoneTips: mockGatewayTipsData
29+
}
30+
});
31+
});
32+
mockUseRecentlySelectedZone();
33+
getGatewayTipsSpy = baseMockApi.gateway.getGatewayTips();
34+
getSQLQueryConfigurationSpy = baseMockApi.cloudBeaver.getSqlQueryUrl();
35+
jest.useFakeTimers();
36+
});
37+
38+
afterEach(() => {
39+
jest.useRealTimers();
40+
jest.clearAllMocks();
41+
cleanup();
42+
});
43+
44+
it('should initialize with correct default values', () => {
45+
const { result } = superRenderHook(() => useNavigateToWorkbench());
46+
47+
expect(result.current.navigateToWorkbenchLoading).toBeFalsy();
48+
expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy();
49+
expect(typeof result.current.navigateToWorkbenchAsync).toBe('function');
50+
expect(typeof result.current.getAvailabilityZoneTipsAsync).toBe('function');
51+
});
52+
53+
it('should fetch availability zone tips successfully', async () => {
54+
const { result } = superRenderHook(() => useNavigateToWorkbench());
55+
expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy();
56+
57+
act(() => {
58+
result.current.getAvailabilityZoneTipsAsync();
59+
});
60+
expect(result.current.getAvailabilityZoneTipsLoading).toBeTruthy();
61+
await act(async () => jest.advanceTimersByTime(3000));
62+
expect(result.current.getAvailabilityZoneTipsLoading).toBeFalsy();
63+
64+
expect(getGatewayTipsSpy).toHaveBeenCalledTimes(1);
65+
expect(dispatchSpy).toHaveBeenCalledWith({
66+
type: 'availabilityZone/updateAvailabilityZoneTips',
67+
payload: { availabilityZoneTips: mockGatewayTipsData }
68+
});
69+
});
70+
71+
it('should fetch SQL query configuration successfully', async () => {
72+
const mockUpdateRecentlySelectedZone = jest.fn();
73+
getSQLQueryConfigurationSpy.mockImplementation(() =>
74+
createSpySuccessResponse({
75+
data: {
76+
enable_sql_query: true,
77+
sql_query_root_uri: '/cloudbeaver'
78+
}
79+
})
80+
);
81+
mockUseRecentlySelectedZone({
82+
availabilityZone: undefined,
83+
updateRecentlySelectedZone: mockUpdateRecentlySelectedZone
84+
});
85+
const { result } = superRenderHook(() => useNavigateToWorkbench());
86+
expect(result.current.navigateToWorkbenchLoading).toBeFalsy();
87+
88+
act(() => {
89+
result.current.navigateToWorkbenchAsync();
90+
});
91+
expect(result.current.navigateToWorkbenchLoading).toBeTruthy();
92+
await act(async () => jest.advanceTimersByTime(3000));
93+
expect(result.current.navigateToWorkbenchLoading).toBeFalsy();
94+
95+
expect(getSQLQueryConfigurationSpy).toHaveBeenCalledTimes(1);
96+
expect(mockUpdateRecentlySelectedZone).toHaveBeenCalledWith(
97+
mockGatewayTipsData[0]
98+
);
99+
});
100+
});

packages/base/src/hooks/useSessionUser/index.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,24 @@ describe('useSessionUser', () => {
5555
});
5656
expect(result.current.getSessionUserLoading).toBeFalsy();
5757
});
58+
59+
it('should get user system data', async () => {
60+
const { result } = superRenderHook(() => useSessionUser(), undefined, {
61+
initStore: {
62+
user: { uid: 'test' }
63+
}
64+
});
65+
expect(result.current.getSessionUserSystemLoading).toBeFalsy();
66+
expect(result.current.shouldNavigateToWorkbench).toEqual(undefined);
67+
68+
await act(async () => {
69+
result.current.getSessionUserInfoAsync();
70+
await jest.advanceTimersByTime(3000);
71+
});
72+
expect(getUserBySessionSpy).toHaveBeenCalledTimes(1);
73+
expect(getCurrentUserSpy).toHaveBeenCalledTimes(1);
74+
await act(async () => jest.advanceTimersByTime(3000));
75+
expect(result.current.getSessionUserSystemLoading).toBeFalsy();
76+
expect(result.current.shouldNavigateToWorkbench).toBeFalsy();
77+
});
5878
});

packages/base/src/page/BindUser/index.ce.test.tsx

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { getBySelector } from '@actiontech/shared/lib/testUtil/customQuery';
1111
import { eventEmitter } from '@actiontech/dms-kit/es/utils/EventEmitter';
1212
import EmitterKey from '@actiontech/dms-kit/es/data/EmitterKey';
1313
import { ROUTE_PATHS } from '@actiontech/dms-kit';
14+
import { mockUseSessionUser } from '../../testUtils/mockHooks/mockUseSessionUser';
15+
import { mockUseNavigateToWorkbench } from '../../testUtils/mockHooks/mockUseNavigateToWorkbench';
1416

1517
jest.mock('react-router-dom', () => {
1618
return {
@@ -27,6 +29,10 @@ jest.mock('react-redux', () => ({
2729
describe('page/BindUser-ce', () => {
2830
const navigateSpy = jest.fn();
2931
const dispatchSpy = jest.fn();
32+
const getSessionUserInfoAsyncSpy = jest.fn(() => Promise.resolve(false));
33+
const navigateToWorkbenchAsyncSpy = jest.fn(() => Promise.resolve(undefined));
34+
const getAvailabilityZoneTipsAsyncSpy = jest.fn(() => Promise.resolve([]));
35+
3036
const customRender = (path = '/user/bind') => {
3137
return baseSuperRender(<BindUser />, undefined, {
3238
routerProps: { initialEntries: [path] }
@@ -38,6 +44,24 @@ describe('page/BindUser-ce', () => {
3844
(useDispatch as jest.Mock).mockImplementation(() => dispatchSpy);
3945
jest.useFakeTimers();
4046
dms.mockAllApi();
47+
48+
getSessionUserInfoAsyncSpy
49+
.mockClear()
50+
.mockImplementation(() => Promise.resolve(false));
51+
getAvailabilityZoneTipsAsyncSpy
52+
.mockClear()
53+
.mockImplementation(() => Promise.resolve([]));
54+
navigateToWorkbenchAsyncSpy
55+
.mockClear()
56+
.mockImplementation(() => Promise.resolve(undefined));
57+
58+
mockUseSessionUser({
59+
getSessionUserInfoAsync: getSessionUserInfoAsyncSpy
60+
});
61+
mockUseNavigateToWorkbench({
62+
navigateToWorkbenchAsync: navigateToWorkbenchAsyncSpy,
63+
getAvailabilityZoneTipsAsync: getAvailabilityZoneTipsAsyncSpy
64+
});
4165
});
4266

4367
afterEach(() => {
@@ -137,13 +161,22 @@ describe('page/BindUser-ce', () => {
137161
user_name: 'oauth2_admin',
138162
pwd: 'oauth2_admin'
139163
});
140-
expect(dispatchSpy).toHaveBeenCalledTimes(1);
141-
expect(dispatchSpy).toHaveBeenCalledWith({
164+
expect(dispatchSpy).toHaveBeenCalledTimes(3);
165+
expect(dispatchSpy).toHaveBeenNthCalledWith(1, {
142166
type: 'user/updateToken',
143167
payload: {
144168
token: 'Bearer token'
145169
}
146170
});
171+
expect(dispatchSpy).toHaveBeenNthCalledWith(2, {
172+
type: 'user/updateIsLoggingIn',
173+
payload: true
174+
});
175+
expect(dispatchSpy).toHaveBeenNthCalledWith(3, {
176+
type: 'user/updateIsLoggingIn',
177+
payload: false
178+
});
179+
expect(getSessionUserInfoAsyncSpy).toHaveBeenCalledTimes(1);
147180
expect(navigateSpy).toHaveBeenCalled();
148181
expect(navigateSpy).toHaveBeenCalledWith('/');
149182
});
@@ -204,13 +237,22 @@ describe('page/BindUser-ce', () => {
204237
const search = `user_exist=true&dms_token=111111`;
205238
customRender(`/user/bind?${search}`);
206239
await act(async () => jest.advanceTimersByTime(300));
207-
expect(dispatchSpy).toHaveBeenCalledTimes(1);
208-
expect(dispatchSpy).toHaveBeenCalledWith({
240+
expect(dispatchSpy).toHaveBeenCalledTimes(3);
241+
expect(dispatchSpy).toHaveBeenNthCalledWith(1, {
209242
type: 'user/updateToken',
210243
payload: {
211244
token: 'Bearer 111111'
212245
}
213246
});
247+
expect(dispatchSpy).toHaveBeenNthCalledWith(2, {
248+
type: 'user/updateIsLoggingIn',
249+
payload: true
250+
});
251+
expect(dispatchSpy).toHaveBeenNthCalledWith(3, {
252+
type: 'user/updateIsLoggingIn',
253+
payload: false
254+
});
255+
expect(getSessionUserInfoAsyncSpy).toHaveBeenCalledTimes(1);
214256
expect(navigateSpy).toHaveBeenCalled();
215257
expect(navigateSpy).toHaveBeenCalledWith('/');
216258
});

0 commit comments

Comments
 (0)