databag/app/mobile/__tests__/Profile.test.js

174 lines
5.6 KiB
JavaScript
Raw Normal View History

2022-12-31 06:16:15 +00:00
import React, { useState, useEffect, useContext } from 'react';
2022-12-31 07:58:17 +00:00
import { View, Text } from 'react-native';
2023-02-15 19:45:36 +00:00
import { useTestStoreContext } from 'context/useTestStoreContext.hook';
2022-12-31 07:58:17 +00:00
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react-native'
2022-12-31 06:16:15 +00:00
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
2022-12-31 07:58:17 +00:00
function ProfileView() {
2023-01-03 01:49:43 +00:00
const [renderCount, setRenderCount] = useState(0);
2022-12-31 06:16:15 +00:00
const profile = useContext(ProfileContext);
2023-01-03 01:49:43 +00:00
useEffect(() => {
setRenderCount(renderCount + 1);
}, [profile.state]);
2022-12-31 07:58:17 +00:00
return (
2023-01-03 01:49:43 +00:00
<View testID="profile" profile={profile} renderCount={renderCount}>
2023-01-02 23:46:08 +00:00
<Text testID="guid">{ profile.state.identity?.guid }</Text>
<Text testID="handle">{ profile.state.identity?.handle }</Text>
<Text testID="name">{ profile.state.identity?.name }</Text>
<Text testID="description">{ profile.state.identity?.description }</Text>
<Text testID="location">{ profile.state.identity?.location }</Text>
<Text testID="image">{ profile.state.identity?.image }</Text>
<Text testID="revision">{ profile.state.identity?.revision }</Text>
<Text testID="seal">{ profile.state.identity?.seal }</Text>
<Text testID="version">{ profile.state.identity?.version }</Text>
<Text testID="node">{ profile.state.identity?.node }</Text>
2022-12-31 07:58:17 +00:00
<Text testID="imageUrl">{ profile.state.imageUrl }</Text>
</View>
);
2022-12-31 06:16:15 +00:00
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
2022-12-31 07:58:17 +00:00
<ProfileView />
2022-12-31 06:16:15 +00:00
</ProfileContextProvider>
)
}
const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
2022-12-31 07:58:17 +00:00
let identity = { };
2022-12-31 06:16:15 +00:00
beforeEach(() => {
const mockUseContext = jest.fn().mockImplementation((ctx) => {
return useTestStoreContext();
});
React.useContext = mockUseContext;
2022-12-31 07:58:17 +00:00
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (options.method === 'PUT') {
identity = JSON.parse(options.body);
}
2022-12-31 06:16:15 +00:00
return Promise.resolve({
2022-12-31 07:58:17 +00:00
json: () => Promise.resolve(identity)
2022-12-31 06:16:15 +00:00
});
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
React.useContext = realUseContext;
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('testing', async () => {
2022-12-31 07:58:17 +00:00
render(<ProfileTestApp />)
2022-12-31 06:16:15 +00:00
2022-12-31 07:58:17 +00:00
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe(undefined);
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setSession({ guid: 'abc', server: 'test.org', token: '123' });
2022-12-31 07:58:17 +00:00
await profile.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("jester");
});
await act(async () => {
2023-01-02 23:46:08 +00:00
identity = { name: 'tester', image: 'abc123' };
2022-12-31 07:58:17 +00:00
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
2023-01-02 23:46:08 +00:00
expect(screen.getByTestId('imageUrl').props.children).toBe("https://test.org/profile/image?agent=123&revision=2");
2022-12-31 07:58:17 +00:00
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
2023-01-02 23:46:08 +00:00
expect(screen.getByTestId('imageUrl').props.children).toBe("https://test.org/profile/image?agent=123&revision=2");
2022-12-31 07:58:17 +00:00
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.clearSession();
});
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setSession({ guid: 'abc', server: 'test.org', token: '123' });
2022-12-31 07:58:17 +00:00
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
expect(screen.getByTestId('imageUrl').props.children).toBe("https://test.org/profile/image?agent=123&revision=2");
});
2023-01-02 23:46:08 +00:00
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(3);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("jester");
expect(screen.getByTestId('imageUrl').props.children).toBe(null);
});
2022-12-31 07:58:17 +00:00
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
for (let i = 0; i < 1024; i++) {
identity = { revision: i };
await profile.actions.setRevision(i);
}
});
await waitFor(async () => {
expect(screen.getByTestId('revision').props.children).toBe(1023);
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setProfileData("vesper", "sf", "dweb");
await profile.actions.setRevision(1024);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("vesper");
});
2023-01-03 01:49:43 +00:00
const renderCount = screen.getByTestId('profile').props.renderCount;
await act(async () => {
identity = { name: 'renderer' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2048);
});
await act(async () => {
expect(screen.getByTestId('profile').props.renderCount).toBe(renderCount + 1);
});
2022-12-31 06:16:15 +00:00
});