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

View File

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