mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
adding account profile test for webapp
This commit is contained in:
parent
4cba051a5d
commit
e7e8817747
@ -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);
|
||||
|
@ -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';
|
||||
|
@ -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 = () => {
|
||||
|
106
net/web/test/User.test.js
Normal file
106
net/web/test/User.test.js
Normal file
@ -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 (
|
||||
<div data-testid="name" count={renderCount}>{ name }</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ProfileTestApp() {
|
||||
return (
|
||||
<StoreContextProvider>
|
||||
<ProfileContextProvider>
|
||||
<AccountContextProvider>
|
||||
<ViewportContextProvider>
|
||||
<AppContextProvider>
|
||||
<ProfileView />
|
||||
</AppContextProvider>
|
||||
</ViewportContextProvider>
|
||||
</AccountContextProvider>
|
||||
</ProfileContextProvider>
|
||||
</StoreContextProvider>
|
||||
);
|
||||
}
|
||||
|
||||
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(<ProfileTestApp />);
|
||||
|
||||
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');
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user