Skip to content

[fix](shared/useUserInfo): freeze project issue #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 78 additions & 13 deletions packages/shared/lib/global/useUserInfo/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useDispatch, useSelector } from 'react-redux';
import { useDispatch } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import useUserInfo from '.';
import { act, renderHook } from '@testing-library/react';
import { act } from '@testing-library/react';
import global from '../../../../base/src/testUtils/mockApi/global';
import { GetUserPayload } from '../../../../base/src/testUtils/mockApi/global/data';
import {
createSpyErrorResponse,
createSpyFailResponse
createSpyFailResponse,
createSpySuccessResponse
} from '../../testUtil/mockApi';
import { renderHooksWithRedux } from '../../testUtil/customRender';
import { IStore } from '../../types/common.type';

jest.mock('react-redux', () => {
return {
...jest.requireActual('react-redux'),
useSelector: jest.fn(),
useDispatch: jest.fn()
};
});
Expand All @@ -32,11 +34,6 @@ describe('useUserInfo', () => {
beforeEach(() => {
jest.useFakeTimers();
(useDispatch as jest.Mock).mockImplementation(() => mockDispatch);
(useSelector as jest.Mock).mockImplementation((selector) => {
return selector({
user: { uid: '111' }
});
});
(useNavigate as jest.Mock).mockImplementation(() => navigateSpy);
getCurrentUserSpy = global.getCurrentUser();
});
Expand All @@ -46,8 +43,76 @@ describe('useUserInfo', () => {
jest.clearAllMocks();
});

const customRender = (
store: IStore = {
user: { uid: '111', bindProjects: [] }
}
) => {
return renderHooksWithRedux(() => useUserInfo(), store);
};

it('should update userInfo when request success', async () => {
const { result } = renderHook(() => useUserInfo());
const { result } = customRender();
expect(result.current.userInfo).not.toBeDefined();
expect(result.current.getUserInfoLoading).toBeFalsy();
await act(() => result.current.getUserInfo());
await act(async () => jest.advanceTimersByTime(3000));
expect(mockDispatch).toHaveBeenCalled();
expect(mockDispatch).toHaveBeenCalledWith({
payload: {
role: 'admin',
username: 'test'
},
type: 'user/updateUser'
});
expect(mockDispatch).toHaveBeenCalledWith({
payload: {
bindProjects: GetUserPayload.user_bind_projects?.map((i) => ({
...i,
archived: false
}))
},
type: 'user/updateBindProjects'
});
expect(mockDispatch).toHaveBeenCalledWith({
payload: {
managementPermissions: []
},
type: 'user/updateManagementPermissions'
});
expect(mockDispatch).toHaveBeenCalledWith({
payload: true,
type: 'user/updateUserInfoFetchStatus'
});
});

it('should update userInfo when bindProjects is not an empty array', async () => {
const projectMock = {
is_manager: true,
project_id: '700200',
project_name: 'test'
};
getCurrentUserSpy.mockClear();
getCurrentUserSpy.mockImplementation(() =>
createSpySuccessResponse({
data: {
...GetUserPayload,
user_bind_projects: [
...(GetUserPayload?.user_bind_projects ?? []),
projectMock
]
}
})
);
const bindProjectsMock =
GetUserPayload.user_bind_projects?.map((i) => ({
...i,
archived: true
})) ?? [];

const { result } = customRender({
user: { uid: '111', bindProjects: bindProjectsMock }
});
expect(result.current.userInfo).not.toBeDefined();
expect(result.current.getUserInfoLoading).toBeFalsy();
await act(() => result.current.getUserInfo());
Expand All @@ -62,7 +127,7 @@ describe('useUserInfo', () => {
});
expect(mockDispatch).toHaveBeenCalledWith({
payload: {
bindProjects: GetUserPayload.user_bind_projects
bindProjects: [...bindProjectsMock, { ...projectMock, archived: false }]
},
type: 'user/updateBindProjects'
});
Expand All @@ -83,7 +148,7 @@ describe('useUserInfo', () => {
getCurrentUserSpy = getCurrentUserSpy.mockImplementation(() =>
createSpyFailResponse({ name: '', user_uid: '' })
);
const { result } = renderHook(() => useUserInfo());
const { result } = customRender();
await act(() => result.current.getUserInfo());
await act(async () => jest.advanceTimersByTime(3000));
expect(mockDispatch).toHaveBeenCalledTimes(12);
Expand Down Expand Up @@ -120,7 +185,7 @@ describe('useUserInfo', () => {
getCurrentUserSpy = getCurrentUserSpy.mockImplementation(() =>
createSpyErrorResponse({ name: '', user_uid: '' })
);
const { result } = renderHook(() => useUserInfo());
const { result } = customRender();
await act(() => result.current.getUserInfo());
await act(async () => jest.advanceTimersByTime(3000));
expect(mockDispatch).toHaveBeenCalledTimes(6);
Expand Down
14 changes: 12 additions & 2 deletions packages/shared/lib/global/useUserInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const useUserInfo = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const location = useLocation();
const userId = useSelector((state: IReduxState) => state.user.uid);
const { userId, bindProjects } = useSelector((state: IReduxState) => ({
userId: state.user.uid,
bindProjects: state.user.bindProjects
}));

const clearUserInfo = useCallback(() => {
dispatch(
Expand Down Expand Up @@ -73,7 +76,14 @@ const useUserInfo = () => {

dispatch(
updateBindProjects({
bindProjects: data?.user_bind_projects ?? []
bindProjects:
data?.user_bind_projects?.map((project) => ({
...project,
archived:
bindProjects?.find(
(i) => i.project_id === project.project_id
)?.archived ?? false
})) ?? []
})
);

Expand Down