databag/net/web/test/Card.test.js

310 lines
7.0 KiB
JavaScript
Raw Normal View History

2023-01-07 07:04:38 +00:00
import React, { useState, useEffect, useContext } from 'react';
import { prettyDOM } from '@testing-library/dom'
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
2023-01-07 07:04:38 +00:00
import { CardContextProvider, CardContext } from 'context/CardContext';
import * as fetchUtil from 'api/fetchUtil';
let cardContext = null;
function CardView() {
const [renderCount, setRenderCount] = useState(0);
const [cards, setCards] = useState([]);
const card = useContext(CardContext);
cardContext = card;
useEffect(() => {
const rendered = []
2023-01-07 07:04:38 +00:00
const entries = Array.from(card.state.cards.values());
entries.forEach(entry => {
2023-01-07 07:04:38 +00:00
rendered.push(
<div key={entry.id} data-testid="card" offsync={entry.offsync.toString()}>
<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>);
2023-01-07 07:04:38 +00:00
});
setCards(rendered);
setRenderCount(renderCount + 1);
}, [card.state])
2023-01-07 07:04:38 +00:00
return (
<div data-testid="cards" count={renderCount} offsync={card.state.offsync.toString()}>
{ cards }
2023-01-07 07:04:38 +00:00
</div>
);
}
function CardTestApp() {
return (
<CardContextProvider>
<CardView />
</CardContextProvider>
)
2023-01-07 07:04:38 +00:00
}
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
let statusCards;
2023-01-07 23:21:07 +00:00
let statusMessage;
2023-01-08 08:09:19 +00:00
let statusDetail;
let statusProfile;
let fetchCards;
2023-01-07 23:21:07 +00:00
let fetchMessage;
2023-01-08 08:09:19 +00:00
let fetchDetail;
let fetchProfile;
2023-01-07 07:04:38 +00:00
beforeEach(() => {
2023-01-07 23:21:07 +00:00
statusMessage = 200;
fetchMessage =
statusCards = 200;
2023-01-08 08:09:19 +00:00
fetchCards = [];
statusDetail = 200;
fetchDetail = {};
statusProfile = 200;
fetchProfile = {};
2023-01-07 07:04:38 +00:00
const mockFetch = jest.fn().mockImplementation((url, options) => {
2023-01-07 23:21:07 +00:00
const params = url.split('/');
if (params[4]?.split('?')[0] === 'message') {
2023-01-07 23:21:07 +00:00
return Promise.resolve({
url: 'getMessage',
status: statusMessage,
json: () => Promise.resolve(fetchMessage),
});
}
else if (params[4]?.split('?')[0] === 'detail') {
2023-01-08 08:09:19 +00:00
return Promise.resolve({
url: 'getDetail',
status: statusDetail,
json: () => Promise.resolve(fetchDetail),
});
}
else if (params[4]?.split('?')[0] === 'profile') {
2023-01-08 08:09:19 +00:00
return Promise.resolve({
url: 'getProfile',
status: statusProfile,
json: () => Promise.resolve(fetchProfile),
});
}
else if (params[2]?.split('?')[0] === 'cards') {
2023-01-07 23:21:07 +00:00
return Promise.resolve({
url: 'getCards',
status: statusCards,
json: () => Promise.resolve(fetchCards),
});
}
else {
2023-01-07 23:21:07 +00:00
return Promise.resolve({
url: 'endpoint',
status: 200,
json: () => Promise.resolve([]),
});
}
2023-01-07 07:04:38 +00:00
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('resync cards', async() => {
2023-01-08 23:00:24 +00:00
render(<CardTestApp />);
await waitFor(async () => {
expect(cardContext).not.toBe(null);
});
await act(async () => {
cardContext.actions.setToken('abc123');
});
statusCards = 500;
2023-01-08 23:00:24 +00:00
await act(async () => {
cardContext.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('cards').attributes.offsync.value).toBe('true');
2023-01-08 23:00:24 +00:00
});
statusCards = 200;
2023-01-08 23:00:24 +00:00
await act(async () => {
cardContext.actions.resync();
});
2023-01-08 23:00:24 +00:00
await waitFor(async () => {
expect(screen.getByTestId('cards').attributes.offsync.value).toBe('false');
2023-01-09 05:13:58 +00:00
});
act(() => {
cardContext.actions.clearToken();
});
2023-01-09 05:13:58 +00:00
});
test('resync contact', async () => {
2023-01-09 05:13:58 +00:00
render(<CardTestApp />);
await waitFor(async () => {
expect(cardContext).not.toBe(null);
});
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' },
2023-01-09 05:13:58 +00:00
},
}];
2023-01-09 05:13:58 +00:00
await act(async () => {
cardContext.actions.setToken('abc123');
cardContext.actions.setRevision(1);
});
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 4,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
2023-01-09 05:13:58 +00:00
},
}];
2023-01-09 05:13:58 +00:00
statusMessage = 500;
await act(async () => {
cardContext.actions.setRevision(2);
});
2023-01-09 05:13:58 +00:00
await waitFor(async () => {
expect(screen.getByTestId('card').attributes.offsync.value).toBe('true');
2023-01-09 05:13:58 +00:00
});
statusMessage = 200;
2023-01-09 05:13:58 +00:00
await act(async () => {
cardContext.actions.resyncCard('000a');
});
2023-01-09 05:13:58 +00:00
await waitFor(async () => {
expect(screen.getByTestId('card').attributes.offsync.value).toBe('false');
2023-01-08 23:00:24 +00:00
});
act(() => {
cardContext.actions.clearToken();
});
2023-01-08 23:00:24 +00:00
});
2023-01-07 23:21:07 +00:00
test('add, update, remove card', async () => {
2023-01-07 07:04:38 +00:00
render(<CardTestApp />);
await waitFor(async () => {
expect(cardContext).not.toBe(null);
});
await act( async () => {
2023-01-07 07:04:38 +00:00
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' },
2023-01-07 23:21:07 +00:00
},
}];
2023-01-07 23:21:07 +00:00
await act( async () => {
2023-01-07 23:21:07 +00:00
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');
});
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 3,
profileRevision: 4,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
2023-01-08 08:09:19 +00:00
},
}];
2023-01-08 08:09:19 +00:00
fetchProfile = {
guid: '01ab23',
name: 'tested',
};
fetchDetail = {
status: 'confirmed',
};
await act( async () => {
2023-01-08 08:09:19 +00:00
cardContext.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('name').textContent).toBe('tested');
expect(screen.getByTestId('status').textContent).toBe('confirmed');
});
fetchCards = [{
id: '000a',
revision: 3,
}];
2023-01-08 08:09:19 +00:00
await act( async () => {
2023-01-08 08:09:19 +00:00
cardContext.actions.setRevision(3);
});
await waitFor(async () => {
expect(screen.getByTestId('cards').children).toHaveLength(0);
});
2023-01-07 23:21:07 +00:00
act(() => {
2023-01-07 07:04:38 +00:00
cardContext.actions.clearToken();
});
2023-01-07 07:04:38 +00:00
});