diff --git a/net/web/src/context/useAppContext.hook.js b/net/web/src/context/useAppContext.hook.js index f446dbfe..17991f25 100644 --- a/net/web/src/context/useAppContext.hook.js +++ b/net/web/src/context/useAppContext.hook.js @@ -37,10 +37,6 @@ export function useAppContext(websocket) { const channelContext = useContext(ChannelContext); const cardContext = useContext(CardContext); - useEffect(() => { - console.log(state.status); - }, [state.status]); - const setSession = (token) => { try { accountContext.actions.setToken(token); diff --git a/net/web/src/session/account/profile/Profile.jsx b/net/web/src/session/account/profile/Profile.jsx index 5a857852..aada0d96 100644 --- a/net/web/src/session/account/profile/Profile.jsx +++ b/net/web/src/session/account/profile/Profile.jsx @@ -1,5 +1,5 @@ import { useRef, useCallback } from 'react'; -import { Space, Modal, Input, Button } from 'antd'; +import { Modal, Input, Button } from 'antd'; import { ProfileWrapper, ProfileDetailsWrapper, ProfileImageWrapper, EditFooter } from './Profile.styled'; import { useProfile } from './useProfile.hook'; import { Logo } from 'logo/Logo'; diff --git a/net/web/src/session/account/profile/accountAccess/useAccountAccess.hook.js b/net/web/src/session/account/profile/accountAccess/useAccountAccess.hook.js index 1f8b706e..4b4813aa 100644 --- a/net/web/src/session/account/profile/accountAccess/useAccountAccess.hook.js +++ b/net/web/src/session/account/profile/accountAccess/useAccountAccess.hook.js @@ -3,9 +3,6 @@ import { AccountContext } from 'context/AccountContext'; import { ProfileContext } from 'context/ProfileContext'; import { generateSeal, unlockSeal, updateSeal } from 'context/sealUtil'; import { getUsername } from 'api/getUsername'; -import CryptoJS from 'crypto-js'; -import { JSEncrypt } from 'jsencrypt' - export function useAccountAccess() { const [state, setState] = useState({ @@ -70,7 +67,7 @@ export function useAccountAccess() { const sealUpdate = async () => { const updated = updateSeal(state.seal, state.sealKey, state.sealPassword); - await account.actions.updateSeal(state.seal); + await account.actions.updateSeal(updated.seal); }; const isEnabled = () => { diff --git a/net/web/test/User.test.js b/net/web/test/User.test.js new file mode 100644 index 00000000..4b482856 --- /dev/null +++ b/net/web/test/User.test.js @@ -0,0 +1,106 @@ +import React, { useState, useEffect, useContext } from 'react'; +import {render, act, screen, waitFor, fireEvent} from '@testing-library/react' +import { AppContextProvider } from 'context/AppContext'; +import { AccountContextProvider } from 'context/AccountContext'; +import { ProfileContext, ProfileContextProvider } from 'context/ProfileContext'; +import { StoreContextProvider } from 'context/StoreContext'; +import { ViewportContextProvider } from 'context/ViewportContext'; +import { useProfile } from 'session/account/profile/useProfile.hook'; +import * as fetchUtil from 'api/fetchUtil'; + +let profileHook; +let profileContext; +function ProfileView() { + const { state, actions } = useProfile(); + + const [name, setName] = useState(); + const [renderCount, setRenderCount] = useState(0); + const profile = useContext(ProfileContext); + profileContext = profile; + profileHook = actions; + + useEffect(() => { + const rendered = []; + setName(state.name); + setRenderCount(renderCount + 1); + }, [state]); + + return ( +
{ name }
+ ); +} + +function ProfileTestApp() { + return ( + + + + + + + + + + + + ); +} + +let updated; +const realFetchWithTimeout = fetchUtil.fetchWithTimeout; +const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout; +beforeEach(() => { + let updated = false; + const mockFetch = jest.fn().mockImplementation((url, options) => { + if (options.method === 'PUT') { + updated = true; + } + return Promise.resolve({ + json: () => Promise.resolve({ name: updated ? 'tested' : 'tester' }) + }); + }); + fetchUtil.fetchWithTimeout = mockFetch; + fetchUtil.fetchWithCustomTimeout = mockFetch; +}); + +afterEach(() => { + fetchUtil.fetchWithTimeout = realFetchWithTimeout; + fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; +}); + +test('update profile name', async () => { + + render(); + + await waitFor(async () => { + expect(profileContext).not.toBe(null); + expect(profileHook).not.toBe(null); + }); + + await act(async () => { + profileContext.actions.setToken('abc123'); + profileContext.actions.setRevision(1); + }); + + await waitFor(async () => { + expect(screen.getByTestId('name').textContent).toBe('tester'); + }); + + await act(async () => { + profileHook.setEditName('tested'); + }); + + await act(async () => { + await profileHook.setProfileDetails(); + }); + + await act(async () => { + profileContext.actions.setRevision(2); + }); + + await waitFor(async () => { + expect(screen.getByTestId('name').textContent).toBe('tested'); + }); + +}); +