2023-01-07 07:04:38 +00:00
|
|
|
import React, { useState, useEffect, useContext } from 'react';
|
2024-04-25 20:29:25 +00:00
|
|
|
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(() => {
|
2024-04-25 20:29:25 +00:00
|
|
|
const rendered = [];
|
2023-01-07 07:04:38 +00:00
|
|
|
const entries = Array.from(card.state.cards.values());
|
2024-04-25 20:29:25 +00:00
|
|
|
entries.forEach((entry) => {
|
2023-01-07 07:04:38 +00:00
|
|
|
rendered.push(
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:29:25 +00:00
|
|
|
<div
|
|
|
|
key={entry.id}
|
|
|
|
data-testid="card"
|
2024-04-25 20:59:35 +00:00
|
|
|
data-offsync={entry.offsync.toString()}
|
2024-04-25 20:29:25 +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>
|
|
|
|
</div>,
|
|
|
|
);
|
2023-01-07 07:04:38 +00:00
|
|
|
});
|
|
|
|
setCards(rendered);
|
|
|
|
setRenderCount(renderCount + 1);
|
2024-04-25 20:29:25 +00:00
|
|
|
}, [card.state]);
|
2023-01-07 07:04:38 +00:00
|
|
|
|
|
|
|
return (
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:29:25 +00:00
|
|
|
<div
|
|
|
|
data-testid="cards"
|
2024-04-25 20:59:35 +00:00
|
|
|
data-count={renderCount}
|
|
|
|
data-offsync={card.state.offsync.toString()}
|
2024-04-25 20:29:25 +00:00
|
|
|
>
|
|
|
|
{cards}
|
2023-01-07 07:04:38 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function CardTestApp() {
|
|
|
|
return (
|
|
|
|
<CardContextProvider>
|
|
|
|
<CardView />
|
|
|
|
</CardContextProvider>
|
2024-04-25 20:29:25 +00:00
|
|
|
);
|
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;
|
2024-04-25 20:29:25 +00:00
|
|
|
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') {
|
|
|
|
return Promise.resolve({
|
|
|
|
url: 'getMessage',
|
|
|
|
status: statusMessage,
|
|
|
|
json: () => Promise.resolve(fetchMessage),
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
} 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),
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
} 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),
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
} 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),
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
} 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
|
|
|
});
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-07 07:04:38 +00:00
|
|
|
fetchUtil.fetchWithTimeout = mockFetch;
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-07 07:04:38 +00:00
|
|
|
fetchUtil.fetchWithCustomTimeout = mockFetch;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-07 07:04:38 +00:00
|
|
|
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2023-01-07 07:04:38 +00:00
|
|
|
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
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;
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-08 23:00:24 +00:00
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setRevision(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:59:35 +00:00
|
|
|
expect(screen.getByTestId('cards').attributes['data-offsync'].value).toBe('true');
|
2023-01-08 23:00:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
statusCards = 200;
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-08 23:00:24 +00:00
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.resync();
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-08 23:00:24 +00:00
|
|
|
await waitFor(async () => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:59:35 +00:00
|
|
|
expect(screen.getByTestId('cards').attributes['data-offsync'].value).toBe('false');
|
2023-01-09 05:13:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
cardContext.actions.clearToken();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('resync contact', async () => {
|
|
|
|
render(<CardTestApp />);
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(cardContext).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +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-09 05:13:58 +00:00
|
|
|
},
|
2024-04-25 20:29:25 +00:00
|
|
|
];
|
2023-01-09 05:13:58 +00:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setToken('abc123');
|
|
|
|
cardContext.actions.setRevision(1);
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
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
|
|
|
},
|
2024-04-25 20:29:25 +00:00
|
|
|
];
|
2023-01-09 05:13:58 +00:00
|
|
|
|
|
|
|
statusMessage = 500;
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setRevision(2);
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-09 05:13:58 +00:00
|
|
|
await waitFor(async () => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:59:35 +00:00
|
|
|
expect(screen.getByTestId('card').attributes['data-offsync'].value).toBe('true');
|
2023-01-09 05:13:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
statusMessage = 200;
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-09 05:13:58 +00:00
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.resyncCard('000a');
|
|
|
|
});
|
2024-04-25 20:29:25 +00:00
|
|
|
|
2023-01-09 05:13:58 +00:00
|
|
|
await waitFor(async () => {
|
2024-04-24 20:26:55 +00:00
|
|
|
//@ts-ignore
|
2024-04-25 20:59:35 +00:00
|
|
|
expect(screen.getByTestId('card').attributes['data-offsync'].value).toBe('false');
|
2023-01-08 23:00:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
cardContext.actions.clearToken();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
await act(async () => {
|
2023-01-07 07:04:38 +00:00
|
|
|
cardContext.actions.setToken('abc123');
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +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 23:21:07 +00:00
|
|
|
},
|
2024-04-25 20:29:25 +00:00
|
|
|
];
|
2023-01-07 23:21:07 +00:00
|
|
|
|
2024-04-25 20:29:25 +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');
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
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
|
|
|
},
|
2024-04-25 20:29:25 +00:00
|
|
|
];
|
2023-01-08 08:09:19 +00:00
|
|
|
|
|
|
|
fetchProfile = {
|
|
|
|
guid: '01ab23',
|
|
|
|
name: 'tested',
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchDetail = {
|
|
|
|
status: 'confirmed',
|
|
|
|
};
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2024-04-25 20:29:25 +00:00
|
|
|
fetchCards = [
|
|
|
|
{
|
|
|
|
id: '000a',
|
|
|
|
revision: 3,
|
|
|
|
},
|
|
|
|
];
|
2023-01-08 08:09:19 +00:00
|
|
|
|
2024-04-25 20:29:25 +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();
|
|
|
|
});
|
|
|
|
});
|