From cdc5f988a5f7c8261c3d6392af5a85fab19001e6 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 30 Dec 2022 23:58:17 -0800 Subject: [PATCH] improving profile context test --- app/mobile/test/Profile.test.js | 127 ++++++++++++++++---- app/mobile/test/useTestStoreContext.hook.js | 11 +- app/mobile/yarn.lock | 18 +-- 3 files changed, 108 insertions(+), 48 deletions(-) diff --git a/app/mobile/test/Profile.test.js b/app/mobile/test/Profile.test.js index ac12924b..00ca2ff3 100644 --- a/app/mobile/test/Profile.test.js +++ b/app/mobile/test/Profile.test.js @@ -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 ({ msg }); + return ( + + { profile.state.profile?.guid } + { profile.state.profile?.handle } + { profile.state.profile?.name } + { profile.state.profile?.description } + { profile.state.profile?.location } + { profile.state.profile?.image } + { profile.state.profile?.revision } + { profile.state.profile?.seal } + { profile.state.profile?.version } + { profile.state.profile?.node } + { profile.state.imageUrl } + + ); } function ProfileTestApp() { return ( - + ) } @@ -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; @@ -58,12 +64,85 @@ afterEach(() => { }); test('testing', async () => { - render() + render() - await waitFor(async () => { - expect(screen.getByTestId('done').props.children).toBe("DONE"); - }); - //await new Promise(r => setTimeout(r, 2000)); + 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 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"); + }); + }); diff --git a/app/mobile/test/useTestStoreContext.hook.js b/app/mobile/test/useTestStoreContext.hook.js index 692c08da..56279038 100644 --- a/app/mobile/test/useTestStoreContext.hook.js +++ b/app/mobile/test/useTestStoreContext.hook.js @@ -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) => { diff --git a/app/mobile/yarn.lock b/app/mobile/yarn.lock index d14bfa8a..fd760c20 100644 --- a/app/mobile/yarn.lock +++ b/app/mobile/yarn.lock @@ -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"