2023-01-24 00:29:04 +00:00
|
|
|
import React, { useState, useEffect, useContext } from 'react';
|
2024-05-02 22:16:20 +00:00
|
|
|
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
|
2023-01-24 00:29:04 +00:00
|
|
|
import { AppContextProvider } from 'context/AppContext';
|
|
|
|
import { AccountContextProvider } from 'context/AccountContext';
|
|
|
|
import { ProfileContext, ProfileContextProvider } from 'context/ProfileContext';
|
|
|
|
import { StoreContextProvider } from 'context/StoreContext';
|
2024-03-08 00:43:36 +00:00
|
|
|
import { SettingsContextProvider } from 'context/SettingsContext';
|
2023-01-24 00:29:04 +00:00
|
|
|
import { useProfile } from 'session/account/profile/useProfile.hook';
|
|
|
|
import * as fetchUtil from 'api/fetchUtil';
|
|
|
|
|
|
|
|
let profileHook;
|
|
|
|
let profileContext;
|
|
|
|
function ProfileView() {
|
|
|
|
const { state, actions } = useProfile();
|
|
|
|
|
|
|
|
const [name, setName] = useState();
|
|
|
|
const [renderCount, setRenderCount] = useState(0);
|
|
|
|
const profile = useContext(ProfileContext);
|
|
|
|
profileContext = profile;
|
|
|
|
profileHook = actions;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const rendered = [];
|
|
|
|
setName(state.name);
|
|
|
|
setRenderCount(renderCount + 1);
|
|
|
|
}, [state]);
|
|
|
|
|
|
|
|
return (
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-05-02 22:16:20 +00:00
|
|
|
<div data-testid="name" count={renderCount}>{ name }</div>
|
2023-01-24 00:29:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ProfileTestApp() {
|
|
|
|
return (
|
|
|
|
<StoreContextProvider>
|
|
|
|
<ProfileContextProvider>
|
|
|
|
<AccountContextProvider>
|
2024-03-08 00:43:36 +00:00
|
|
|
<SettingsContextProvider>
|
2023-01-24 00:29:04 +00:00
|
|
|
<AppContextProvider>
|
|
|
|
<ProfileView />
|
|
|
|
</AppContextProvider>
|
2024-03-08 00:43:36 +00:00
|
|
|
</SettingsContextProvider>
|
2023-01-24 00:29:04 +00:00
|
|
|
</AccountContextProvider>
|
|
|
|
</ProfileContextProvider>
|
|
|
|
</StoreContextProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let updated;
|
|
|
|
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
|
|
|
|
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
|
|
|
|
beforeEach(() => {
|
2024-05-02 22:16:20 +00:00
|
|
|
let updated = false;
|
2023-01-24 00:29:04 +00:00
|
|
|
const mockFetch = jest.fn().mockImplementation((url, options) => {
|
|
|
|
if (options.method === 'PUT') {
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
return Promise.resolve({
|
2024-05-02 22:16:20 +00:00
|
|
|
json: () => Promise.resolve({ name: updated ? 'tested' : 'tester' })
|
2023-01-24 00:29:04 +00:00
|
|
|
});
|
|
|
|
});
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-24 00:29:04 +00:00
|
|
|
fetchUtil.fetchWithTimeout = mockFetch;
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-24 00:29:04 +00:00
|
|
|
fetchUtil.fetchWithCustomTimeout = mockFetch;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-24 00:29:04 +00:00
|
|
|
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-24 00:29:04 +00:00
|
|
|
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('update profile name', async () => {
|
2024-05-02 22:16:20 +00:00
|
|
|
|
2023-01-24 00:29:04 +00:00
|
|
|
render(<ProfileTestApp />);
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(profileContext).not.toBe(null);
|
|
|
|
expect(profileHook).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
profileContext.actions.setToken('abc123');
|
|
|
|
profileContext.actions.setRevision(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(screen.getByTestId('name').textContent).toBe('tester');
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
profileHook.setEditName('tested');
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await profileHook.setProfileDetails();
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
profileContext.actions.setRevision(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(screen.getByTestId('name').textContent).toBe('tested');
|
|
|
|
});
|
2024-05-02 22:16:20 +00:00
|
|
|
|
2023-01-24 00:29:04 +00:00
|
|
|
});
|
2024-05-02 22:16:20 +00:00
|
|
|
|