From c04cd4c2704108d12f2b6f9525be62e5865a16d6 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sat, 14 Jan 2023 21:47:10 -0800 Subject: [PATCH] testing card update indicator --- net/web/test/Session.test.js | 126 +++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 net/web/test/Session.test.js diff --git a/net/web/test/Session.test.js b/net/web/test/Session.test.js new file mode 100644 index 00000000..4a66ee77 --- /dev/null +++ b/net/web/test/Session.test.js @@ -0,0 +1,126 @@ +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 { 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'; +import { ViewportContextProvider } from 'context/ViewportContext'; +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' }, +})); + +let cardContext; +let storeContext; +function SessionView() { + const { state, actions } = useSession(); + const card = useContext(CardContext); + const store = useContext(StoreContext); + cardContext = card; + storeContext = store; + return (
{ state.cardUpdated.toString() }
); +} + +function SessionTestApp() { + return ( + + + + + + + + + + + + + + + + + + ); +} + +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) + }); + } + else { + return Promise.resolve({ + json: () => Promise.resolve([]) + }); + } + }); + fetchUtil.fetchWithTimeout = mockFetch; + fetchUtil.fetchWithCustomTimeout = mockFetch; +}); + +afterEach(() => { + fetchUtil.fetchWithTimeout = realFetchWithTimeout; + fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; +}); + +test('card update indicator', async () => { + render(); + + await waitFor(async () => { + expect(cardContext).not.toBe(null); + expect(storeContext).not.toBe(null); + }); + + fetchCards = [{ + 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' }, + }, + }]; + + await act(async () => { + cardContext.actions.setToken('123'); + storeContext.actions.setValue('cards:updated', 133); + cardContext.actions.setRevision(1); + }); + + await waitFor(async () => { + expect(screen.getByTestId('updated').textContent).toBe('true'); + }); + + await act(async () => { + storeContext.actions.setValue('cards:updated', 136); + }); + + await waitFor(async () => { + expect(screen.getByTestId('updated').textContent).toBe('false'); + }); +}); + + +