databag/net/web/test/Profile.test.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useContext } from 'react';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
2022-12-31 19:40:25 +00:00
let profileContext = null;
function ProfileView() {
2023-01-03 01:49:43 +00:00
const [renderCount, setRenderCount] = useState(0);
const profile = useContext(ProfileContext);
2022-12-31 19:40:25 +00:00
profileContext = profile;
2023-01-03 01:49:43 +00:00
useEffect(() => {
setRenderCount(renderCount + 1);
}, [profile.state]);
return (
2022-12-31 19:40:25 +00:00
<div>
2023-01-03 01:49:43 +00:00
<span data-testid="count">{ renderCount }</span>
2023-01-02 06:35:00 +00:00
<span data-testid="guid">{ profile.state.identity?.guid }</span>
<span data-testid="handle">{ profile.state.identity?.handle }</span>
<span data-testid="name">{ profile.state.identity?.name }</span>
<span data-testid="description">{ profile.state.identity?.description }</span>
<span data-testid="location">{ profile.state.identity?.location }</span>
<span data-testid="image">{ profile.state.identity?.image }</span>
<span data-testid="revision">{ profile.state.identity?.revision }</span>
<span data-testid="seal">{ profile.state.identity?.seal }</span>
<span data-testid="version">{ profile.state.identity?.version }</span>
<span data-testid="node">{ profile.state.identity?.node }</span>
</div>
);
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
<ProfileView />
</ProfileContextProvider>
)
}
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let identity = { };
beforeEach(() => {
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (options.method === 'PUT') {
identity = JSON.parse(options.body);
}
return Promise.resolve({
json: () => Promise.resolve(identity)
});
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
2023-01-04 23:27:29 +00:00
test('testing profile sync', async () => {
render(<ProfileTestApp />);
2022-12-31 19:40:25 +00:00
await waitFor(async () => {
expect(profileContext).not.toBe(null);
});
await waitFor(async () => {
expect(screen.getByTestId('name').textContent).toBe("");
});
2022-12-31 19:40:25 +00:00
await act(async () => {
identity = { name: 'jester' };
await profileContext.actions.setToken({ guid: 'abc', server: 'test.org', appToken: '123' });
await profileContext.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('name').textContent).toBe("jester");
});
2022-12-31 21:40:51 +00:00
await act(async () => {
identity = { name: 'tester' };
await profileContext.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').textContent).toBe("tester");
});
2023-01-03 01:49:43 +00:00
const count = screen.getByTestId('count').textContent;
await act(async () => {
identity = { name: 'renderer' };
await profileContext.actions.setRevision(3);
});
await waitFor(async () => {
const renderCount = parseInt(screen.getByTestId('count').textContent);
const nextCount = parseInt(count) + 1;
expect(nextCount).toBe(renderCount);
});
2022-12-31 21:40:51 +00:00
await act(async () => {
await profileContext.actions.clearToken();
});
});