diff --git a/app/mobile/src/context/useCardContext.hook.js b/app/mobile/src/context/useCardContext.hook.js index 0b58c239..c9a21b85 100644 --- a/app/mobile/src/context/useCardContext.hook.js +++ b/app/mobile/src/context/useCardContext.hook.js @@ -78,8 +78,8 @@ export function useCardContext() { revision: cardChannel.revision, detailRevision: cardChannel.data.detailRevision, topicRevision: cardChannel.data.topicRevision, - detail: cardChannel.data.detail, - summary: cardChannel.data.summary, + detail: cardChannel.data.channelDetail, + summary: cardChannel.data.channelSummary, }; }; @@ -112,7 +112,7 @@ export function useCardContext() { try { const { server, token, guid } = access.current; const entry = cards.current.get(cardId); - if (entry?.card?.detail === 'connected') { + if (entry?.card?.detail.status === 'connected') { const card = await getCard(server, token, cardId); const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data || {}; const cardRevision = { view: notifiedView, profile: notifiedProfile, artcile: notifiedArticle, channel: notifiedChannel }; @@ -165,7 +165,7 @@ export function useCardContext() { entry.card.detailRevision = item.detailRevision; await store.actions.setCardItemDetail(guid, card.id, entry.card.detailRevision, entry.card.detail); } - if (entry.card.detail?.state === 'connected' && !entry.card.offsync) { + if (entry.card.detail?.status === 'connected' && !entry.card.offsync) { try { const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = item; const cardRevision = { view: notifiedView, profile: notifiedProfile, article: notifiedArticle, channel: notifiedChannel }; @@ -230,23 +230,35 @@ export function useCardContext() { for (let channel of delta) { if (channel.data) { const channelItem = setCardChannelItem(channel); - const channelEntry = entry.channels.get(channel.id) || {}; + const channelEntry = entry.channels.get(channel.id) || { channelId: channel.id }; const { detailRevision, topicRevision } = channelEntry; - if (item.detailRevision !== detailRevision) { - channelEntry.detail = await getContactChannelDetail(cardServer, cardToken, channel.id); - channelEntry.detailRevision = detailRevision; - await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, channelEntry.detailRevision, channelEntry.detail); + if (channelItem.detailRevision !== detailRevision) { + if (channelItem.detail) { + channelEntry.detail = channelItem.detail; + } + else { + channelEntry.detail = await getContactChannelDetail(cardServer, cardToken, channel.id); + } + channelEntry.unsealedDetail = null; + channelEntry.detailRevision = channelItem.detailRevision; + await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, channelItem.detailRevision, channelEntry.detail); } - if (item.topicRevision !== topicRevision) { - channelEntry.summary = await getContactChannelSummary(cardServer, cardToken, channel.id); - channelEntry.topicRevision = topicRevision; - await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, channelEntry.topicRevision, channelEntry.summary); + if (channelItem.topicRevision !== topicRevision) { + if (channelItem.summary) { + channelEntry.summary = channelItem.summary; + } + else { + channelEntry.summary = await getContactChannelSummary(cardServer, cardToken, channel.id); + } + channelEntry.unsealedSummary = null; + channelEntry.topicRevision = channelItem.topicRevision; + await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, channelItem.topicRevision, channelEntry.summary); } entry.card.notifiedChannel = cardRevision.channel; - await store.actions.setCardItemNotifiedChannel(guid, cardId, channelRevision.channel); + await store.actions.setCardItemNotifiedChannel(guid, cardId, cardRevision.channel); entry.card.notifiedView = cardRevision.view; - await store.actions.setCardItemNotifiedView(guid, cardId, channelRevision.view); - entry.channel.set(channel.id, channelEntry); + await store.actions.setCardItemNotifiedView(guid, cardId, cardRevision.view); + entry.channels.set(channel.id, channelEntry); } else { await store.actions.clearCardChannelTopicItems(guid, card.id, channel.id); diff --git a/app/mobile/test/Card.test.js b/app/mobile/test/Card.test.js index 29f05dcc..356e5a3c 100644 --- a/app/mobile/test/Card.test.js +++ b/app/mobile/test/Card.test.js @@ -15,6 +15,10 @@ function CardView() { setRenderCount(renderCount + 1); const rendered = []; card.state.cards.forEach((value) => { + const chanels = []; + value.channels.forEach((value) => { + console.log(value); + }); rendered.push({ value.card.profile.handle }); }); setCards(rendered); @@ -42,8 +46,12 @@ const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout; let fetchCards; let fetchDetail; let fetchProfile; +let fetchCardChannels; beforeEach(() => { fetchCards = []; + fetchDetail = {}; + fetchProfile = {}; + fetchCardChannels = []; const mockUseContext = jest.fn().mockImplementation((ctx) => { return useTestStoreContext(); @@ -68,6 +76,11 @@ beforeEach(() => { json: () => Promise.resolve(fetchDetail) }); } + if (url.startsWith('https://test.org/content/channels?contact')) { + return Promise.resolve({ + json: () => Promise.resolve(fetchCardChannels) + }); + } else { return Promise.resolve({ json: () => Promise.resolve([]) @@ -85,7 +98,7 @@ afterEach(() => { fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; }); -test('add, update, and remove', async () => { +test('add, update, and remove card', async () => { render() @@ -182,5 +195,70 @@ test('add, update, and remove', async () => { }); +test('add, update, and remove channel', async () => { + + render() + 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 () => { + const card = screen.getByTestId('card').props.card; + await card.actions.setSession({ guid: 'abc', server: 'test.org', token: '123' }); + await card.actions.setRevision(1); + }); + + await waitFor(async () => { + expect(screen.getByTestId('card').props.children).toHaveLength(1); + }); + + fetchCardChannels = [{ + id: '01', + revision: 1, + data: { + detailRevision: 1, + topicRevision: 1, + channelDetail: { + dataType: 'superbasic', + data: 'testchannel', + }, + channelSummary: { + dataType: 'superbasictopic', + data: 'testtopic', + }, + }, + }]; + + await act(async () => { + const card = screen.getByTestId('card').props.card; + await card.actions.setRevision(2); + }); + + fetchCards = [{ + id: '000a', + revision: 1, + data: { + detailRevision: 2, + profileRevision: 3, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 7, + notifiedView: 7, + }, + }]; + +});