exended card context webapp test

This commit is contained in:
balzack 2023-01-07 15:21:07 -08:00
parent 6f972788a6
commit 6c4fc82202
2 changed files with 73 additions and 16 deletions

View File

@ -61,8 +61,9 @@ export function useCardContext() {
forceCard.current = null; forceCard.current = null;
try { try {
const token = access.current;
const card = cards.current.get(card.id); const card = cards.current.get(card.id);
await syncCard(card); await syncCard(token, card);
cards.current.set(card.id, card); cards.current.set(card.id, card);
} }
catch(err) { catch(err) {
@ -83,6 +84,7 @@ export function useCardContext() {
const revision = curRevision.current; const revision = curRevision.current;
const delta = await getCards(token, setRevision.current); const delta = await getCards(token, setRevision.current);
for (let card of delta) { for (let card of delta) {
if (card.data) { if (card.data) {
let cur = cards.current.get(card.id); let cur = cards.current.get(card.id);
if (cur == null) { if (cur == null) {
@ -113,19 +115,21 @@ export function useCardContext() {
cur.data.curNotifiedArticle = card.data.notifiedArticle; cur.data.curNotifiedArticle = card.data.notifiedArticle;
cur.data.curNotifiedChannel = card.data.notifiedChannel; cur.data.curNotifiedChannel = card.data.notifiedChannel;
try { try {
await syncCard(cur); await syncCard(token, cur);
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
cur.offsync = true; cur.offsync = true;
} }
} }
cards.current.set(cur.id, cur); cards.current.set(card.id, cur);
} }
else { else {
cards.current.delete(cur.id); cards.current.delete(card.id);
} }
} }
setRevision.current = revision;
updateState({ offsync: false, cards: cards.current }); updateState({ offsync: false, cards: cards.current });
} }
catch (err) { catch (err) {
@ -141,7 +145,7 @@ export function useCardContext() {
}; };
const syncCard = async (token, card) => { const syncCard = async (token, card) => {
const { cardProfile, cardDetail } = card; const { cardProfile, cardDetail } = card.data;
// sync profile // sync profile
if (card.data.setNotifiedProfile !== card.data.curNotifiedProfile) { if (card.data.setNotifiedProfile !== card.data.curNotifiedProfile) {
@ -166,12 +170,11 @@ export function useCardContext() {
card.offsync = false; card.offsync = false;
} }
const syncCardArticles = async (card) => { const syncCardArticles = async (card) => {}
console.log("update contact articles");
}
const syncCardChannels = 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}`; const token = `${cardProfile.guid}.${cardDetail.token}`;
let delta; let delta;
if (card.data.setNotifiedView !== card.data.curNotifiedView) { if (card.data.setNotifiedView !== card.data.curNotifiedView) {
@ -186,7 +189,7 @@ export function useCardContext() {
if (channel.data) { if (channel.data) {
let cur = card.channels.get(channel.id); let cur = card.channels.get(channel.id);
if (cur == null) { if (cur == null) {
cur = { guid, cardId, id: channel.id, data: {} }; cur = { id: channel.id, data: {} };
} }
if (cur.data.detailRevision !== channel.data.detailRevision) { if (cur.data.detailRevision !== channel.data.detailRevision) {
if (channel.data.channelDetail != null) { if (channel.data.channelDetail != null) {

View File

@ -18,6 +18,9 @@ function CardView() {
rendered.push( rendered.push(
<div key={entry.id} data-testid="card"> <div key={entry.id} data-testid="card">
<span data-testid="name">{ entry.data.cardProfile.name }</span>
<span data-testid="status">{ entry.data.cardDetail.status }</span>
<span data-testid="token">{ entry.data.cardDetail.token }</span>
</div>); </div>);
}); });
setCards(rendered); setCards(rendered);
@ -44,16 +47,40 @@ const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let statusCards; let statusCards;
let fetchCards; let fetchCards;
let statusMessage;
let fetchMessage;
beforeEach(() => { beforeEach(() => {
statusMessage = 200;
fetchMessage =
statusCards = 200; statusCards = 200;
fetchCards =[]; fetchCards =[];
const mockFetch = jest.fn().mockImplementation((url, options) => { const mockFetch = jest.fn().mockImplementation((url, options) => {
const params = url.split('/');
if (params[4]?.split('?')[0] === 'message') {
return Promise.resolve({ return Promise.resolve({
url: 'getChannels', url: 'getMessage',
status: statusChannels, status: statusMessage,
json: () => Promise.resolve(fetchChannels), 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.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch; fetchUtil.fetchWithCustomTimeout = mockFetch;
@ -64,7 +91,7 @@ afterEach(() => {
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
}); });
test('boilerplate', async () => { test('add, update, remove card', async () => {
render(<CardTestApp />); render(<CardTestApp />);
@ -76,7 +103,34 @@ test('boilerplate', async () => {
cardContext.actions.setToken('abc123'); 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 () => { 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(); cardContext.actions.clearToken();
}); });