From 6c4fc82202b0d06bf1c8cde9ed115f26bab7fbd9 Mon Sep 17 00:00:00 2001 From: balzack Date: Sat, 7 Jan 2023 15:21:07 -0800 Subject: [PATCH] exended card context webapp test --- net/web/src/context/useCardContext.hook.js | 23 ++++---- net/web/test/Card.test.js | 66 ++++++++++++++++++++-- 2 files changed, 73 insertions(+), 16 deletions(-) diff --git a/net/web/src/context/useCardContext.hook.js b/net/web/src/context/useCardContext.hook.js index 04cc8f89..fdc6be66 100644 --- a/net/web/src/context/useCardContext.hook.js +++ b/net/web/src/context/useCardContext.hook.js @@ -61,8 +61,9 @@ export function useCardContext() { forceCard.current = null; try { + const token = access.current; const card = cards.current.get(card.id); - await syncCard(card); + await syncCard(token, card); cards.current.set(card.id, card); } catch(err) { @@ -83,6 +84,7 @@ export function useCardContext() { const revision = curRevision.current; const delta = await getCards(token, setRevision.current); for (let card of delta) { + if (card.data) { let cur = cards.current.get(card.id); if (cur == null) { @@ -113,19 +115,21 @@ export function useCardContext() { cur.data.curNotifiedArticle = card.data.notifiedArticle; cur.data.curNotifiedChannel = card.data.notifiedChannel; try { - await syncCard(cur); + await syncCard(token, cur); } catch (err) { console.log(err); cur.offsync = true; } } - cards.current.set(cur.id, cur); + cards.current.set(card.id, cur); } else { - cards.current.delete(cur.id); + cards.current.delete(card.id); } } + + setRevision.current = revision; updateState({ offsync: false, cards: cards.current }); } catch (err) { @@ -141,7 +145,7 @@ export function useCardContext() { }; const syncCard = async (token, card) => { - const { cardProfile, cardDetail } = card; + const { cardProfile, cardDetail } = card.data; // sync profile if (card.data.setNotifiedProfile !== card.data.curNotifiedProfile) { @@ -166,12 +170,11 @@ export function useCardContext() { card.offsync = false; } - const syncCardArticles = async (card) => { - console.log("update contact articles"); - } + const syncCardArticles = async (card) => {} const syncCardChannels = async (card) => { - const { cardProfile, cardDetail } = card; + const { cardProfile, cardDetail } = card.data; + const { node } = cardProfile.node; const token = `${cardProfile.guid}.${cardDetail.token}`; let delta; if (card.data.setNotifiedView !== card.data.curNotifiedView) { @@ -186,7 +189,7 @@ export function useCardContext() { if (channel.data) { let cur = card.channels.get(channel.id); if (cur == null) { - cur = { guid, cardId, id: channel.id, data: {} }; + cur = { id: channel.id, data: {} }; } if (cur.data.detailRevision !== channel.data.detailRevision) { if (channel.data.channelDetail != null) { diff --git a/net/web/test/Card.test.js b/net/web/test/Card.test.js index 5019747c..e560b0df 100644 --- a/net/web/test/Card.test.js +++ b/net/web/test/Card.test.js @@ -18,6 +18,9 @@ function CardView() { rendered.push(
+ { entry.data.cardProfile.name } + { entry.data.cardDetail.status } + { entry.data.cardDetail.token }
); }); setCards(rendered); @@ -44,16 +47,40 @@ const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout; let statusCards; let fetchCards; +let statusMessage; +let fetchMessage; beforeEach(() => { + statusMessage = 200; + fetchMessage = statusCards = 200; fetchCards =[]; const mockFetch = jest.fn().mockImplementation((url, options) => { - return Promise.resolve({ - url: 'getChannels', - status: statusChannels, - json: () => Promise.resolve(fetchChannels), - }); + + const params = url.split('/'); + if (params[4]?.split('?')[0] === 'message') { + return Promise.resolve({ + url: 'getMessage', + status: statusMessage, + json: () => Promise.resolve(fetchMessage), + }); + } + else if (params[2]?.split('?')[0] === 'cards') { + return Promise.resolve({ + url: 'getCards', + status: statusCards, + json: () => Promise.resolve(fetchCards), + }); + } + else { + console.log(params, options); + + return Promise.resolve({ + url: 'endpoint', + status: 200, + json: () => Promise.resolve([]), + }); + } }); fetchUtil.fetchWithTimeout = mockFetch; fetchUtil.fetchWithCustomTimeout = mockFetch; @@ -64,7 +91,7 @@ afterEach(() => { fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; }); -test('boilerplate', async () => { +test('add, update, remove card', async () => { render(); @@ -76,7 +103,34 @@ test('boilerplate', async () => { cardContext.actions.setToken('abc123'); }); + fetchCards = [{ + id: '000a', + revision: 1, + data: { + detailRevision: 2, + profileRevision: 3, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 6, + notifiedView: 7, + cardDetail: { status: 'connected', statusUpdate: 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.setRevision(1); + }); + + await waitFor(async () => { + expect(screen.getByTestId('cards').children).toHaveLength(1); + expect(screen.getByTestId('name').textContent).toBe('tester'); + expect(screen.getByTestId('status').textContent).toBe('connected'); + expect(screen.getByTestId('token').textContent).toBe('01ab'); + }); + + act(() => { cardContext.actions.clearToken(); });