From 1a6338181355d339a342769516ffa381d1795df7 Mon Sep 17 00:00:00 2001 From: balzack Date: Thu, 5 Jan 2023 14:33:06 -0800 Subject: [PATCH] more channel context testing --- net/web/src/api/getChannels.js | 1 - net/web/src/context/useChannelContext.hook.js | 2 +- net/web/test/Channel.test.js | 157 ++++++++++++++++-- 3 files changed, 147 insertions(+), 13 deletions(-) diff --git a/net/web/src/api/getChannels.js b/net/web/src/api/getChannels.js index b5dd7ce7..0dafc671 100644 --- a/net/web/src/api/getChannels.js +++ b/net/web/src/api/getChannels.js @@ -8,7 +8,6 @@ export async function getChannels(token, revision) { let types = encodeURIComponent(JSON.stringify([ 'sealed', 'superbasic' ])); param += `&types=${types}` let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' }); - checkResponse(channels) let ret = await channels.json() return ret; diff --git a/net/web/src/context/useChannelContext.hook.js b/net/web/src/context/useChannelContext.hook.js index eaa19c69..857203ae 100644 --- a/net/web/src/context/useChannelContext.hook.js +++ b/net/web/src/context/useChannelContext.hook.js @@ -134,7 +134,7 @@ export function useChannelContext() { const channel = channels.current.get(channelId); if (channel.revision === revision) { channel.data.unsealedSummary = unsealed; - channels.current.set(channelId, chanel); + channels.current.set(channelId, channel); updateState({ channels: channels.current }); } }, diff --git a/net/web/test/Channel.test.js b/net/web/test/Channel.test.js index 4ab37219..ed5aaf9f 100644 --- a/net/web/test/Channel.test.js +++ b/net/web/test/Channel.test.js @@ -15,8 +15,12 @@ function ChannelView() { const rendered = [] const entries = Array.from(channel.state.channels.values()); entries.forEach(entry => { +console.log(entry.data.unsealedSubject); + rendered.push(
+ { entry.data.unsealedSubject } + { entry.data.unsealedSummary } { entry.data.channelDetail.data } { entry.data.channelSummary.data }
); @@ -49,6 +53,11 @@ let statusChannels; let fetchDetail; let fetchSummary; let fetchChannels; +let addedChannel; +let removedChannel; +let updatedChannel; +let addedCard; +let removedCard; beforeEach(() => { statusDetail = 200; @@ -56,24 +65,64 @@ beforeEach(() => { statusChannels = 200; fetchDetail = {}; fetchSummary = {}; - fetchChannels = {}; - + fetchChannels =[]; + addedChannel = []; + updatedChannel = []; + removedChannel = []; + addedCard = []; + removedCard = []; const mockFetch = jest.fn().mockImplementation((url, options) => { - if (url.includes('detail')) { + if (options.method === 'POST') { + addedChannel.push(options.body); return Promise.resolve({ + status: 200, + json: () => Promise.resolve({ id: 'id1' }), + }); + } + else if (options.method === 'PUT') { + const params = url.split('/'); + if (params[4] === 'cards') { + addedCard.push(params[5].split('?')[0]); + } + else { + updatedChannel.push({ id: params[3], body: options.body }); + } + return Promise.resolve({ + status: 200, + json: () => Promise.resolve({}), + }); + } + else if (options.method === 'DELETE') { + const params = url.split('/'); + if (params[4] === 'cards') { + removedCard.push(params[5].split('?')[0]); + } + else { + removedChannel.push(params[3].split('?')[0]); + } + return Promise.resolve({ + status: 200, + json: () => Promise.resolve(), + }); + } + else if (url.includes('detail')) { + return Promise.resolve({ + url: 'getDetail', status: statusDetail, json: () => Promise.resolve(fetchDetail), }); } else if (url.includes('summary')) { return Promise.resolve({ + url: 'getSummary', status: statusSummary, json: () => Promise.resolve(fetchSummary), }); } else { return Promise.resolve({ - status: 200, + url: 'getChannels', + status: statusChannels, json: () => Promise.resolve(fetchChannels), }); } @@ -87,9 +136,7 @@ afterEach(() => { fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; }); -test('add, update and remove channel', async () => { - fetchDetail = { dataType: 'superbasic', data: 'testdata' }; - fetchSummary = { guid: '11', dataType: 'superbasictopic', data: 'testdata' }; +test('api invocation', async () => { render(); @@ -97,6 +144,43 @@ test('add, update and remove channel', async () => { expect(channelContext).not.toBe(null); }); + await act( async () => { + channelContext.actions.setToken('abc123'); + }); + + await act(async () => { + await channelContext.actions.addChannel('testtype', 'testsubject', ['testcard']); + await channelContext.actions.setChannelSubject('123', 'testtype', 'testsubject'); + await channelContext.actions.setChannelCard('123', 'newcard'); + await channelContext.actions.clearChannelCard('123', 'newcard'); + await channelContext.actions.removeChannel('123'); + }); + + await waitFor(async () => { + expect(addedChannel.length).toBe(1); + const added = JSON.parse(addedChannel[0]); + expect(added.dataType).toBe('testtype'); + expect(updatedChannel.length).toBe(1); + expect(updatedChannel[0].id).toBe('123'); + expect(removedChannel.length).toBe(1); + expect(removedChannel[0]).toBe('123'); + expect(addedCard.length).toBe(1); + expect(addedCard[0]).toBe('newcard'); + expect(addedCard[0]).toBe('newcard'); + }); + + await act( async () => { + channelContext.actions.clearToken(); + }); + +}); + +test('add, update and remove channel', async () => { + + render(); + + fetchDetail = { dataType: 'superbasic', data: 'testdata' }; + fetchSummary = { guid: '11', dataType: 'superbasictopic', data: 'testdata' }; fetchChannels = [ { id: '123', revision: 2, data: { detailRevision: 3, @@ -107,8 +191,15 @@ test('add, update and remove channel', async () => { }, ]; + await waitFor(async () => { + expect(channelContext).not.toBe(null); + }); + await act( async () => { channelContext.actions.setToken('abc123'); + }); + + await act( async () => { await channelContext.actions.setRevision(1); }); @@ -121,6 +212,8 @@ test('add, update and remove channel', async () => { expect(screen.getByTestId('summary').textContent).toBe('testdata'); }); + const count = parseInt(screen.getByTestId('channels').attributes.count.value) + 1; + fetchChannels = [ { id: '123', revision: 3, data: { detailRevision: 4, @@ -137,8 +230,19 @@ test('add, update and remove channel', async () => { await waitFor(async () => { expect(screen.getByTestId('channels').children).toHaveLength(1); expect(screen.getByTestId('detail').textContent).toBe('testdata2'); + expect(screen.getByTestId('channels').attributes.count.value).toBe(count.toString()); }); + await act( async () => { + await channelContext.actions.unsealChannelSubject('123', 'unsealedsubject', 3); + await channelContext.actions.unsealChannelSummary('123', 'unsealedsummary', 3); + }); + + await waitFor(async () => { + expect(screen.getByTestId('unsealed-subject').textContent).toBe('unsealedsubject'); + expect(screen.getByTestId('unsealed-summary').textContent).toBe('unsealedsummary'); + }); + fetchChannels = [ { id: '123', revision: 3 } ]; await act( async () => { @@ -160,10 +264,41 @@ test('add, update and remove channel', async () => { await waitFor(async () => { expect(screen.getByTestId('channels').attributes.offsync.value).toBe("false"); }); + + await act( async () => { + channelContext.actions.clearToken(); + }); + }); +test('resync', async () => { + + render(); + + await waitFor(async () => { + expect(channelContext).not.toBe(null); + }); + + await act( async () => { + channelContext.actions.setToken('abc123'); + }); + + await act( async () => { + statusChannels = 500; + await channelContext.actions.setRevision(1); + }); + + await waitFor(async () => { + expect(screen.getByTestId('channels').attributes.offsync.value).toBe("true"); + }); + + await act( async () => { + statusChannels = 200; + await channelContext.actions.resync(); + }); + + await waitFor(async () => { + expect(screen.getByTestId('channels').attributes.offsync.value).toBe("false"); + }); +}); -// set and clear of channel card -// add, update and remove topic -// unseal topic and channels -// offsync resync