improving profile context test

This commit is contained in:
Roland Osborne 2022-12-30 23:58:17 -08:00
parent fe271d54c8
commit cdc5f988a5
3 changed files with 108 additions and 48 deletions

View File

@ -1,32 +1,34 @@
import React, { useState, useEffect, useContext } from 'react';
import { Text } from 'react-native';
import { View, Text } from 'react-native';
import { useTestStoreContext } from './useTestStoreContext.hook';
import {render, screen, waitFor, fireEvent} from '@testing-library/react-native'
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react-native'
import { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
import * as fetchUtil from 'api/fetchUtil';
function ProfileTest() {
const [ msg, setMsg ] = useState();
function ProfileView() {
const profile = useContext(ProfileContext);
const testSetup = async () => {
await profile.actions.clearSession();
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
await profile.actions.setRevision(1);
setMsg("DONE");
};
useEffect(() => {
testSetup();
}, []);
return (<Text testID="done">{ msg }</Text>);
return (
<View testID="profile" profile={profile}>
<Text testID="guid">{ profile.state.profile?.guid }</Text>
<Text testID="handle">{ profile.state.profile?.handle }</Text>
<Text testID="name">{ profile.state.profile?.name }</Text>
<Text testID="description">{ profile.state.profile?.description }</Text>
<Text testID="location">{ profile.state.profile?.location }</Text>
<Text testID="image">{ profile.state.profile?.image }</Text>
<Text testID="revision">{ profile.state.profile?.revision }</Text>
<Text testID="seal">{ profile.state.profile?.seal }</Text>
<Text testID="version">{ profile.state.profile?.version }</Text>
<Text testID="node">{ profile.state.profile?.node }</Text>
<Text testID="imageUrl">{ profile.state.imageUrl }</Text>
</View>
);
}
function ProfileTestApp() {
return (
<ProfileContextProvider>
<ProfileTest />
<ProfileView />
</ProfileContextProvider>
)
}
@ -34,6 +36,7 @@ function ProfileTestApp() {
const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let identity = { };
beforeEach(() => {
const mockUseContext = jest.fn().mockImplementation((ctx) => {
@ -41,9 +44,12 @@ beforeEach(() => {
});
React.useContext = mockUseContext;
const mockFetch = jest.fn().mockImplementation(() => {
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (options.method === 'PUT') {
identity = JSON.parse(options.body);
}
return Promise.resolve({
json: () => Promise.resolve({ name: 'jester' })
json: () => Promise.resolve(identity)
});
});
fetchUtil.fetchWithTimeout = mockFetch;
@ -61,9 +67,82 @@ test('testing', async () => {
render(<ProfileTestApp />)
await waitFor(async () => {
expect(screen.getByTestId('done').props.children).toBe("DONE");
expect(screen.getByTestId('name').props.children).toBe(undefined);
});
//await new Promise(r => setTimeout(r, 2000));
await act(async () => {
identity = { name: 'jester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
await profile.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("jester");
});
await act(async () => {
identity = { name: 'tester' };
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').props.children).toBe("tester");
});
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");
});
await act(async () => {
const profile = screen.getByTestId('profile').props.profile;
await profile.actions.clearSession();
});
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', appToken: '123' });
});
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");
});
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");
});
});

View File

@ -3,7 +3,6 @@ import SQLite from "react-native-sqlite-storage";
export function useTestStoreContext() {
const [state, setState] = useState({});
const db = useRef(null);
const updateState = (value) => {
setState((s) => ({ ...s, ...value }))
@ -23,11 +22,10 @@ export function useTestStoreContext() {
},
getProfile: async (guid) => {
console.log("GET PROFILE", guid);
return {};
return state.profile;
},
setProfile: async (guid, profile) => {
console.log("SET PROFILE", guid, profile);
updateState({ profile });
},
getFirstRun: async (guid) => {
},
@ -38,11 +36,10 @@ export function useTestStoreContext() {
setCardRequestStatus: async (guid, status) => {
},
getProfileRevision: async (guid) => {
console.log("GET PROFILE REVISION", guid);
return 0;
return state.profileRevision;
},
setProfileRevision: async (guid, revision) => {
console.log("SET PROFILE REVISION", guid, revision);
updateState({ profileRevision: revision });
},
getAccountStatus: async (guid) => {

View File

@ -7109,7 +7109,7 @@ react-freeze@^1.0.0:
resolved "https://registry.npmjs.org/react-freeze/-/react-freeze-1.0.3.tgz"
integrity sha512-ZnXwLQnGzrDpHBHiC56TXFXvmolPeMjTn1UOm610M4EXGzbEDR7oOIyS2ZiItgbs6eZc4oU/a0hpk8PrcKvv5g==
"react-is@^16.12.0 || ^17.0.0 || ^18.0.0", react-is@^18.0.0, react-is@^18.1.0, react-is@^18.2.0:
"react-is@^16.12.0 || ^17.0.0 || ^18.0.0", react-is@^18.0.0, react-is@^18.1.0:
version "18.2.0"
resolved "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz"
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==
@ -7344,15 +7344,6 @@ react-shallow-renderer@16.15.0, react-shallow-renderer@^16.15.0:
object-assign "^4.1.1"
react-is "^16.12.0 || ^17.0.0 || ^18.0.0"
react-test-renderer@^18.2.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-18.2.0.tgz#1dd912bd908ff26da5b9fca4fd1c489b9523d37e"
integrity sha512-JWD+aQ0lh2gvh4NM3bBM42Kx+XybOxCpgYK7F8ugAlpaTSnWsX+39Z4XkOykGZAHrjwwTZT3x3KxswVWxHPUqA==
dependencies:
react-is "^18.2.0"
react-shallow-renderer "^16.15.0"
scheduler "^0.23.0"
react-test-renderer@~18.1.0:
version "18.1.0"
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-18.1.0.tgz#35b75754834cf9ab517b6813db94aee0a6b545c3"
@ -7691,13 +7682,6 @@ scheduler@^0.22.0:
dependencies:
loose-envify "^1.1.0"
scheduler@^0.23.0:
version "0.23.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.23.0.tgz#ba8041afc3d30eb206a487b6b384002e4e61fdfe"
integrity sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==
dependencies:
loose-envify "^1.1.0"
semver@7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/semver/-/semver-7.0.0.tgz"