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(
|
2023-01-09 05:13:58 +00:00
|
|
|
<div key={entry.id} data-testid="card" offsync={entry.offsync.toString()}>
|
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') {
|
2023-01-09 05:13:58 +00:00
|
|
|
|
2023-01-07 23:21:07 +00:00
|
|
|
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 {
|
|
|
|
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-08 23:00:24 +00:00
|
|
|
test('resync cards', async() => {
|
|
|
|
|
|
|
|
render(<CardTestApp />);
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(cardContext).not.toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setToken('abc123');
|
|
|
|
});
|
|
|
|
|
|
|
|
statusCards = 500;
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setRevision(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(screen.getByTestId('cards').attributes.offsync.value).toBe('true');
|
|
|
|
});
|
|
|
|
|
|
|
|
statusCards = 200;
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.resync();
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(screen.getByTestId('cards').attributes.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);
|
|
|
|
});
|
|
|
|
|
|
|
|
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.setToken('abc123');
|
|
|
|
cardContext.actions.setRevision(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
fetchCards = [{
|
|
|
|
id: '000a',
|
|
|
|
revision: 1,
|
|
|
|
data: {
|
|
|
|
detailRevision: 2,
|
|
|
|
profileRevision: 3,
|
|
|
|
notifiedProfile: 4,
|
|
|
|
notifiedArticle: 5,
|
|
|
|
notifiedChannel: 6,
|
|
|
|
notifiedView: 7,
|
|
|
|
},
|
|
|
|
}];
|
|
|
|
|
|
|
|
statusMessage = 500;
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.setRevision(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(async () => {
|
|
|
|
expect(screen.getByTestId('card').attributes.offsync.value).toBe('true');
|
|
|
|
});
|
|
|
|
|
|
|
|
statusMessage = 200;
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cardContext.actions.resyncCard('000a');
|
|
|
|
});
|
|
|
|
|
|
|
|
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-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();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|