mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
improving profile context test
This commit is contained in:
parent
fe271d54c8
commit
cdc5f988a5
@ -1,32 +1,34 @@
|
|||||||
import React, { useState, useEffect, useContext } from 'react';
|
import React, { useState, useEffect, useContext } from 'react';
|
||||||
import { Text } from 'react-native';
|
import { View, Text } from 'react-native';
|
||||||
import { useTestStoreContext } from './useTestStoreContext.hook';
|
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 { ProfileContextProvider, ProfileContext } from 'context/ProfileContext';
|
||||||
import * as fetchUtil from 'api/fetchUtil';
|
import * as fetchUtil from 'api/fetchUtil';
|
||||||
|
|
||||||
function ProfileTest() {
|
function ProfileView() {
|
||||||
const [ msg, setMsg ] = useState();
|
|
||||||
const profile = useContext(ProfileContext);
|
const profile = useContext(ProfileContext);
|
||||||
|
|
||||||
const testSetup = async () => {
|
return (
|
||||||
await profile.actions.clearSession();
|
<View testID="profile" profile={profile}>
|
||||||
await profile.actions.setSession({ guid: 'abc', server: 'test.org', appToken: '123' });
|
<Text testID="guid">{ profile.state.profile?.guid }</Text>
|
||||||
await profile.actions.setRevision(1);
|
<Text testID="handle">{ profile.state.profile?.handle }</Text>
|
||||||
setMsg("DONE");
|
<Text testID="name">{ profile.state.profile?.name }</Text>
|
||||||
};
|
<Text testID="description">{ profile.state.profile?.description }</Text>
|
||||||
|
<Text testID="location">{ profile.state.profile?.location }</Text>
|
||||||
useEffect(() => {
|
<Text testID="image">{ profile.state.profile?.image }</Text>
|
||||||
testSetup();
|
<Text testID="revision">{ profile.state.profile?.revision }</Text>
|
||||||
}, []);
|
<Text testID="seal">{ profile.state.profile?.seal }</Text>
|
||||||
|
<Text testID="version">{ profile.state.profile?.version }</Text>
|
||||||
return (<Text testID="done">{ msg }</Text>);
|
<Text testID="node">{ profile.state.profile?.node }</Text>
|
||||||
|
<Text testID="imageUrl">{ profile.state.imageUrl }</Text>
|
||||||
|
</View>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function ProfileTestApp() {
|
function ProfileTestApp() {
|
||||||
return (
|
return (
|
||||||
<ProfileContextProvider>
|
<ProfileContextProvider>
|
||||||
<ProfileTest />
|
<ProfileView />
|
||||||
</ProfileContextProvider>
|
</ProfileContextProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -34,6 +36,7 @@ function ProfileTestApp() {
|
|||||||
const realUseContext = React.useContext;
|
const realUseContext = React.useContext;
|
||||||
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
|
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
|
||||||
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
|
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
|
||||||
|
let identity = { };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const mockUseContext = jest.fn().mockImplementation((ctx) => {
|
const mockUseContext = jest.fn().mockImplementation((ctx) => {
|
||||||
@ -41,9 +44,12 @@ beforeEach(() => {
|
|||||||
});
|
});
|
||||||
React.useContext = mockUseContext;
|
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({
|
return Promise.resolve({
|
||||||
json: () => Promise.resolve({ name: 'jester' })
|
json: () => Promise.resolve(identity)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
fetchUtil.fetchWithTimeout = mockFetch;
|
fetchUtil.fetchWithTimeout = mockFetch;
|
||||||
@ -61,9 +67,82 @@ test('testing', async () => {
|
|||||||
render(<ProfileTestApp />)
|
render(<ProfileTestApp />)
|
||||||
|
|
||||||
await waitFor(async () => {
|
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");
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ import SQLite from "react-native-sqlite-storage";
|
|||||||
|
|
||||||
export function useTestStoreContext() {
|
export function useTestStoreContext() {
|
||||||
const [state, setState] = useState({});
|
const [state, setState] = useState({});
|
||||||
const db = useRef(null);
|
|
||||||
|
|
||||||
const updateState = (value) => {
|
const updateState = (value) => {
|
||||||
setState((s) => ({ ...s, ...value }))
|
setState((s) => ({ ...s, ...value }))
|
||||||
@ -23,11 +22,10 @@ export function useTestStoreContext() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getProfile: async (guid) => {
|
getProfile: async (guid) => {
|
||||||
console.log("GET PROFILE", guid);
|
return state.profile;
|
||||||
return {};
|
|
||||||
},
|
},
|
||||||
setProfile: async (guid, profile) => {
|
setProfile: async (guid, profile) => {
|
||||||
console.log("SET PROFILE", guid, profile);
|
updateState({ profile });
|
||||||
},
|
},
|
||||||
getFirstRun: async (guid) => {
|
getFirstRun: async (guid) => {
|
||||||
},
|
},
|
||||||
@ -38,11 +36,10 @@ export function useTestStoreContext() {
|
|||||||
setCardRequestStatus: async (guid, status) => {
|
setCardRequestStatus: async (guid, status) => {
|
||||||
},
|
},
|
||||||
getProfileRevision: async (guid) => {
|
getProfileRevision: async (guid) => {
|
||||||
console.log("GET PROFILE REVISION", guid);
|
return state.profileRevision;
|
||||||
return 0;
|
|
||||||
},
|
},
|
||||||
setProfileRevision: async (guid, revision) => {
|
setProfileRevision: async (guid, revision) => {
|
||||||
console.log("SET PROFILE REVISION", guid, revision);
|
updateState({ profileRevision: revision });
|
||||||
},
|
},
|
||||||
|
|
||||||
getAccountStatus: async (guid) => {
|
getAccountStatus: async (guid) => {
|
||||||
|
@ -7109,7 +7109,7 @@ react-freeze@^1.0.0:
|
|||||||
resolved "https://registry.npmjs.org/react-freeze/-/react-freeze-1.0.3.tgz"
|
resolved "https://registry.npmjs.org/react-freeze/-/react-freeze-1.0.3.tgz"
|
||||||
integrity sha512-ZnXwLQnGzrDpHBHiC56TXFXvmolPeMjTn1UOm610M4EXGzbEDR7oOIyS2ZiItgbs6eZc4oU/a0hpk8PrcKvv5g==
|
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"
|
version "18.2.0"
|
||||||
resolved "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz"
|
resolved "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz"
|
||||||
integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==
|
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"
|
object-assign "^4.1.1"
|
||||||
react-is "^16.12.0 || ^17.0.0 || ^18.0.0"
|
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:
|
react-test-renderer@~18.1.0:
|
||||||
version "18.1.0"
|
version "18.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-18.1.0.tgz#35b75754834cf9ab517b6813db94aee0a6b545c3"
|
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:
|
dependencies:
|
||||||
loose-envify "^1.1.0"
|
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:
|
semver@7.0.0:
|
||||||
version "7.0.0"
|
version "7.0.0"
|
||||||
resolved "https://registry.npmjs.org/semver/-/semver-7.0.0.tgz"
|
resolved "https://registry.npmjs.org/semver/-/semver-7.0.0.tgz"
|
||||||
|
Loading…
Reference in New Issue
Block a user