databag/net/web/test/Session.test.js

130 lines
3.9 KiB
JavaScript
Raw Normal View History

2023-01-15 05:47:10 +00:00
import React, { useState, useEffect, useContext } from 'react';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
2023-01-15 05:47:10 +00:00
import { AppContextProvider } from 'context/AppContext';
import { AccountContextProvider } from 'context/AccountContext';
import { ProfileContextProvider } from 'context/ProfileContext';
import { CardContext, CardContextProvider } from 'context/CardContext';
import { ChannelContextProvider } from 'context/ChannelContext';
import { StoreContext, StoreContextProvider } from 'context/StoreContext';
import { UploadContextProvider } from 'context/UploadContext';
2024-03-08 00:43:36 +00:00
import { SettingsContextProvider } from 'context/SettingsContext';
2023-04-14 22:49:24 +00:00
import { RingContextProvider } from 'context/RingContext';
2023-01-15 05:47:10 +00:00
import { useSession } from 'session/useSession.hook';
import * as fetchUtil from 'api/fetchUtil';
let navPath;
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => { return (path) => { navPath = path } },
useLocation: () => { return 'path' },
2023-01-15 05:47:10 +00:00
}));
let cardContext;
let storeContext;
function SessionView() {
const { state, actions } = useSession();
const card = useContext(CardContext);
const store = useContext(StoreContext);
cardContext = card;
storeContext = store;
return (<div data-testid="updated">{ state.cardUpdated.toString() }</div>);
2023-01-15 05:47:10 +00:00
}
function SessionTestApp() {
return (
<UploadContextProvider>
<ChannelContextProvider>
<CardContextProvider>
<ProfileContextProvider>
<StoreContextProvider>
<AccountContextProvider>
2023-04-14 22:49:24 +00:00
<RingContextProvider>
2024-03-08 00:43:36 +00:00
<SettingsContextProvider>
2023-04-14 22:49:24 +00:00
<AppContextProvider>
<SessionView />
</AppContextProvider>
2024-03-08 00:43:36 +00:00
</SettingsContextProvider>
2023-04-14 22:49:24 +00:00
</RingContextProvider>
2023-01-15 05:47:10 +00:00
</AccountContextProvider>
</StoreContextProvider>
</ProfileContextProvider>
</CardContextProvider>
</ChannelContextProvider>
</UploadContextProvider>
);
}
let fetchCards;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
beforeEach(() => {
fetchCards = [];
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (url === '/contact/cards?agent=123') {
return Promise.resolve({
json: () => Promise.resolve(fetchCards)
2023-01-15 05:47:10 +00:00
});
}
else {
2023-01-15 05:47:10 +00:00
return Promise.resolve({
json: () => Promise.resolve([])
2023-01-15 05:47:10 +00:00
});
}
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('card update indicator', async () => {
render(<SessionTestApp />);
2023-01-15 05:47:10 +00:00
await waitFor(async () => {
expect(cardContext).not.toBe(null);
expect(storeContext).not.toBe(null);
});
2023-01-15 05:47:10 +00:00
fetchCards = [{
2023-01-15 05:47:10 +00:00
id: '000a',
revision: 1,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
cardDetail: { status: 'connected', statusUpdated: 136, token: '01ab', },
cardProfile: { guid: '01ab23', handle: 'test1', name: 'tester', imageSet: false,
seal: 'abc', version: '1.1.1', node: 'test.org' },
2023-01-15 05:47:10 +00:00
},
}];
2023-01-15 05:47:10 +00:00
await act(async () => {
cardContext.actions.setToken('123');
storeContext.actions.setValue('cards:updated', 133);
cardContext.actions.setRevision(1);
});
2023-01-15 05:47:10 +00:00
await waitFor(async () => {
expect(screen.getByTestId('updated').textContent).toBe('true');
});
2023-01-15 05:47:10 +00:00
await act(async () => {
storeContext.actions.setValue('cards:updated', 136);
});
2023-01-15 05:47:10 +00:00
await waitFor(async () => {
expect(screen.getByTestId('updated').textContent).toBe('false');
});
2023-01-15 05:47:10 +00:00
});