From 5ab3e9ee7a78abe4a073a65b2795ff0c67ee3147 Mon Sep 17 00:00:00 2001 From: balzack Date: Sun, 8 Jan 2023 00:09:19 -0800 Subject: [PATCH] extending card context webapp test --- net/web/src/context/useCardContext.hook.js | 5 +- net/web/test/Card.test.js | 70 +++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/net/web/src/context/useCardContext.hook.js b/net/web/src/context/useCardContext.hook.js index fdc6be66..24eac3a5 100644 --- a/net/web/src/context/useCardContext.hook.js +++ b/net/web/src/context/useCardContext.hook.js @@ -63,7 +63,10 @@ export function useCardContext() { try { const token = access.current; const card = cards.current.get(card.id); - await syncCard(token, card); + if (card.data.cardDetail.status === 'connected') { + await syncCard(token, card); + } + card.offsync = false; cards.current.set(card.id, card); } catch(err) { diff --git a/net/web/test/Card.test.js b/net/web/test/Card.test.js index e560b0df..da69f080 100644 --- a/net/web/test/Card.test.js +++ b/net/web/test/Card.test.js @@ -46,15 +46,23 @@ const realFetchWithTimeout = fetchUtil.fetchWithTimeout; const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout; let statusCards; -let fetchCards; let statusMessage; +let statusDetail; +let statusProfile; +let fetchCards; let fetchMessage; +let fetchDetail; +let fetchProfile; beforeEach(() => { statusMessage = 200; fetchMessage = statusCards = 200; - fetchCards =[]; + fetchCards = []; + statusDetail = 200; + fetchDetail = {}; + statusProfile = 200; + fetchProfile = {}; const mockFetch = jest.fn().mockImplementation((url, options) => { const params = url.split('/'); @@ -65,6 +73,20 @@ beforeEach(() => { json: () => Promise.resolve(fetchMessage), }); } + else if (params[4]?.split('?')[0] === 'detail') { + return Promise.resolve({ + url: 'getDetail', + status: statusDetail, + json: () => Promise.resolve(fetchDetail), + }); + } + else if (params[4]?.split('?')[0] === 'profile') { + return Promise.resolve({ + url: 'getProfile', + status: statusProfile, + json: () => Promise.resolve(fetchProfile), + }); + } else if (params[2]?.split('?')[0] === 'cards') { return Promise.resolve({ url: 'getCards', @@ -130,6 +152,50 @@ test('add, update, remove card', async () => { expect(screen.getByTestId('token').textContent).toBe('01ab'); }); + fetchCards = [{ + id: '000a', + revision: 2, + data: { + detailRevision: 3, + profileRevision: 4, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 6, + notifiedView: 7, + }, + }]; + + fetchProfile = { + guid: '01ab23', + name: 'tested', + }; + + fetchDetail = { + status: 'confirmed', + }; + + await act( async () => { + cardContext.actions.setRevision(2); + }); + + await waitFor(async () => { + expect(screen.getByTestId('name').textContent).toBe('tested'); + expect(screen.getByTestId('status').textContent).toBe('confirmed'); + }); + + fetchCards = [{ + id: '000a', + revision: 3, + }]; + + await act( async () => { + cardContext.actions.setRevision(3); + }); + + await waitFor(async () => { + expect(screen.getByTestId('cards').children).toHaveLength(0); + }); + act(() => { cardContext.actions.clearToken(); });