diff --git a/app/mobile/src/context/useCardContext.hook.js b/app/mobile/src/context/useCardContext.hook.js index c9a21b85..2081e096 100644 --- a/app/mobile/src/context/useCardContext.hook.js +++ b/app/mobile/src/context/useCardContext.hook.js @@ -192,7 +192,6 @@ export function useCardContext() { cards.current.delete(card.id); } } - setRevision.current = revision; await store.actions.setCardRevision(guid, revision); updateState({ offsync: false, cards: cards.current }); @@ -217,8 +216,10 @@ export function useCardContext() { const cardToken = `${profile?.guid}.${detail?.token}`; if (entry.card.notifiedProfile !== cardRevision.profile) { - const message = await getContactProfile(cardServer, cardToken); - await setCardProfile(server, token, cardId, message); + if (entry.card.profileRevision !== cardRevision.profile) { + const message = await getContactProfile(cardServer, cardToken); + await setCardProfile(server, token, cardId, message); + } entry.card.notifiedProfile = cardRevision.profile; store.actions.setCardItemNotifiedProfile(guid, cardId, cardRevision.profile); } @@ -254,18 +255,18 @@ export function useCardContext() { 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, cardRevision.channel); - entry.card.notifiedView = cardRevision.view; - await store.actions.setCardItemNotifiedView(guid, cardId, cardRevision.view); entry.channels.set(channel.id, channelEntry); } else { - await store.actions.clearCardChannelTopicItems(guid, card.id, channel.id); - await store.actions.clearCardChannelItem(guid, card.id, channel.id); - entry.channel.delete(channel.id); + await store.actions.clearCardChannelTopicItems(guid, cardId, channel.id); + await store.actions.clearCardChannelItem(guid, cardId, channel.id); + entry.channels.delete(channel.id); } } + entry.card.notifiedChannel = cardRevision.channel; + await store.actions.setCardItemNotifiedChannel(guid, cardId, cardRevision.channel); + entry.card.notifiedView = cardRevision.view; + await store.actions.setCardItemNotifiedView(guid, cardId, cardRevision.view); } }; diff --git a/app/mobile/test/Card.test.js b/app/mobile/test/Card.test.js index 356e5a3c..479f07ed 100644 --- a/app/mobile/test/Card.test.js +++ b/app/mobile/test/Card.test.js @@ -14,12 +14,18 @@ function CardView() { useEffect(() => { setRenderCount(renderCount + 1); const rendered = []; - card.state.cards.forEach((value) => { - const chanels = []; - value.channels.forEach((value) => { - console.log(value); + card.state.cards.forEach((cardValue) => { + const content = [( + { cardValue.card.profile.handle } + )]; + cardValue.channels.forEach((channelValue) => { + content.push({ channelValue.detail.data }); }); - rendered.push({ value.card.profile.handle }); + rendered.push( + + { content } + + ); }); setCards(rendered); }, [card.state]); @@ -43,6 +49,7 @@ const realUseContext = React.useContext; const realFetchWithTimeout = fetchUtil.fetchWithTimeout; const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout; +let fetchMessage; let fetchCards; let fetchDetail; let fetchProfile; @@ -52,6 +59,7 @@ beforeEach(() => { fetchDetail = {}; fetchProfile = {}; fetchCardChannels = []; + fetchMessage = false; const mockUseContext = jest.fn().mockImplementation((ctx) => { return useTestStoreContext(); @@ -59,8 +67,6 @@ beforeEach(() => { React.useContext = mockUseContext; const mockFetch = jest.fn().mockImplementation((url, options) => { - console.log(url); - if (url.startsWith('https://test.org/contact/cards?agent')) { return Promise.resolve({ json: () => Promise.resolve(fetchCards) @@ -81,6 +87,12 @@ beforeEach(() => { json: () => Promise.resolve(fetchCardChannels) }); } + if (url.startsWith('https://test.org/profile/message?contact')) { + fetchMessage = true; + return Promise.resolve({ + json: () => Promise.resolve({}) + }); + } else { return Promise.resolve({ json: () => Promise.resolve([]) @@ -118,7 +130,7 @@ test('add, update, and remove card', async () => { data: { detailRevision: 2, profileRevision: 3, - notifiedProfile: 3, + notifiedProfile: 4, notifiedArticle: 5, notifiedChannel: 6, notifiedView: 7, @@ -135,7 +147,8 @@ test('add, update, and remove card', async () => { await waitFor(async () => { expect(screen.getByTestId('card').props.children).toHaveLength(1); - expect(screen.getByTestId('000a').props.children).toBe('test1'); + expect(screen.getByTestId('000a-handle').props.children).toBe('test1'); + expect(fetchMessage).toBe(true); }); fetchCards = [{ @@ -144,7 +157,7 @@ test('add, update, and remove card', async () => { data: { detailRevision: 3, profileRevision: 4, - notifiedProfile: 3, + notifiedProfile: 4, notifiedArticle: 5, notifiedChannel: 6, notifiedView: 7, @@ -162,12 +175,12 @@ test('add, update, and remove card', async () => { await act(async () => { const card = screen.getByTestId('card').props.card; - await card.actions.setRevision(3); + await card.actions.setRevision(4); }); await waitFor(async () => { expect(screen.getByTestId('card').props.children).toHaveLength(1); - expect(screen.getByTestId('000a').props.children).toBe('test2'); + expect(screen.getByTestId('000a-handle').props.children).toBe('test2'); }); fetchCards = [{ @@ -177,7 +190,7 @@ test('add, update, and remove card', async () => { await act(async () => { const card = screen.getByTestId('card').props.card; - await card.actions.setRevision(3); + await card.actions.setRevision(4); }); await waitFor(async () => { @@ -186,7 +199,7 @@ test('add, update, and remove card', async () => { await act(async () => { const card = screen.getByTestId('card').props.card; - await card.actions.setRevision(4); + await card.actions.setRevision(5); }); await waitFor(async () => { @@ -222,7 +235,7 @@ test('add, update, and remove channel', async () => { }); await waitFor(async () => { - expect(screen.getByTestId('card').props.children).toHaveLength(1); + expect(screen.getByTestId('000a').props.children).toHaveLength(1); }); fetchCardChannels = [{ @@ -242,14 +255,31 @@ test('add, update, and remove channel', async () => { }, }]; + fetchCards = [{ + id: '000a', + revision: 2, + data: { + detailRevision: 2, + profileRevision: 3, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 6, + notifiedView: 7, + }, + }]; + await act(async () => { const card = screen.getByTestId('card').props.card; await card.actions.setRevision(2); }); + await waitFor(async () => { + expect(screen.getByTestId('000a').props.children).toHaveLength(1); + }); + fetchCards = [{ id: '000a', - revision: 1, + revision: 2, data: { detailRevision: 2, profileRevision: 3, @@ -260,5 +290,82 @@ test('add, update, and remove channel', async () => { }, }]; + await act(async () => { + const card = screen.getByTestId('card').props.card; + await card.actions.setRevision(3); + }); + + await waitFor(async () => { + expect(screen.getByTestId('000a').props.children).toHaveLength(2); + expect(screen.getByTestId('01').props.children).toBe('testchannel'); + }); + + fetchCardChannels = [{ + id: '01', + revision: 2, + data: { + detailRevision: 2, + topicRevision: 2, + channelDetail: { + dataType: 'superbasic', + data: 'testchannel2', + }, + channelSummary: { + dataType: 'superbasictopic', + data: 'testtopic', + }, + }, + }]; + + fetchCards = [{ + id: '000a', + revision: 2, + data: { + detailRevision: 2, + profileRevision: 3, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 8, + notifiedView: 7, + }, + }]; + + await act(async () => { + const card = screen.getByTestId('card').props.card; + await card.actions.setRevision(4); + }); + + await waitFor(async () => { + expect(screen.getByTestId('000a').props.children).toHaveLength(2); + expect(screen.getByTestId('01').props.children).toBe('testchannel2'); + }); + + fetchCardChannels = [{ + id: '01', + revision: 3, + }]; + + fetchCards = [{ + id: '000a', + revision: 3, + data: { + detailRevision: 2, + profileRevision: 3, + notifiedProfile: 3, + notifiedArticle: 5, + notifiedChannel: 9, + notifiedView: 7, + }, + }]; + + await act(async () => { + const card = screen.getByTestId('card').props.card; + await card.actions.setRevision(5); + }); + + await waitFor(async () => { + expect(screen.getByTestId('000a').props.children).toHaveLength(1); + }); + });