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

205 lines
4.9 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'
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 = []
const entries = Array.from(card.state.cards.values());
entries.forEach(entry => {
rendered.push(
<div key={entry.id} data-testid="card">
2023-01-07 23:21:07 +00:00
<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>
2023-01-07 07:04:38 +00:00
</div>);
});
setCards(rendered);
setRenderCount(renderCount + 1);
}, [card.state])
return (
<div data-testid="cards" count={renderCount} offsync={card.state.offsync.toString()}>
{ cards }
</div>
);
}
function CardTestApp() {
return (
<CardContextProvider>
<CardView />
</CardContextProvider>
)
}
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 =
2023-01-07 07:04:38 +00:00
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') {
return Promise.resolve({
url: 'getMessage',
status: statusMessage,
json: () => Promise.resolve(fetchMessage),
});
}
2023-01-08 08:09:19 +00:00
else if (params[4]?.split('?')[0] === 'detail') {
return Promise.resolve({
url: 'getDetail',
status: statusDetail,
json: () => Promise.resolve(fetchDetail),
});
}
else if (params[4]?.split('?')[0] === 'profile') {
return Promise.resolve({
url: 'getProfile',
status: statusProfile,
json: () => Promise.resolve(fetchProfile),
});
}
2023-01-07 23:21:07 +00:00
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([]),
});
}
2023-01-07 07:04:38 +00:00
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
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 () => {
cardContext.actions.setToken('abc123');
});
2023-01-07 23:21:07 +00:00
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 07:04:38 +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');
});
2023-01-08 08:09:19 +00:00
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 3,
profileRevision: 4,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
},
}];
fetchProfile = {
guid: '01ab23',
name: 'tested',
};
fetchDetail = {
status: 'confirmed',
};
await act( async () => {
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,
}];
await act( async () => {
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();
});
});