databag/app/mobile/__tests__/Card.test.js

372 lines
9.1 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useContext } from 'react';
import { View, Text } from 'react-native';
2023-02-15 19:45:36 +00:00
import { useTestStoreContext } from 'context/useTestStoreContext.hook';
import { prettyDOM } from '@testing-library/dom';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react-native';
import { CardContextProvider, CardContext } from 'context/CardContext';
import * as fetchUtil from 'api/fetchUtil';
function CardView() {
const [renderCount, setRenderCount] = useState(0);
const card = useContext(CardContext);
const [cards, setCards] = useState([]);
useEffect(() => {
setRenderCount(renderCount + 1);
const rendered = [];
2023-02-08 19:24:56 +00:00
card.state.cards.forEach((cardValue) => {
const content = [(
<Text key="handle" testID={cardValue.card.cardId + "-handle"}>{ cardValue.card.profile.handle }</Text>
)];
cardValue.channels.forEach((channelValue) => {
content.push(<Text key={channelValue.channelId} testID={channelValue.channelId}>{ channelValue.detail.data }</Text>);
2023-02-08 07:52:38 +00:00
});
2023-02-08 19:24:56 +00:00
rendered.push(
<Text key={cardValue.card.cardId} testID={cardValue.card.cardId}>
{ content }
</Text>
);
});
setCards(rendered);
}, [card.state]);
return (
<View key="cards" testID="card" card={card} renderCount={renderCount}>
{ cards }
</View>
);
}
function CardTestApp() {
return (
<CardContextProvider>
<CardView />
</CardContextProvider>
)
}
const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
2023-02-08 19:24:56 +00:00
let fetchMessage;
let fetchCards;
let fetchDetail;
let fetchProfile;
2023-02-08 07:52:38 +00:00
let fetchCardChannels;
beforeEach(() => {
fetchCards = [];
2023-02-08 07:52:38 +00:00
fetchDetail = {};
fetchProfile = {};
fetchCardChannels = [];
2023-02-08 19:24:56 +00:00
fetchMessage = false;
const mockUseContext = jest.fn().mockImplementation((ctx) => {
return useTestStoreContext();
});
React.useContext = mockUseContext;
const mockFetch = jest.fn().mockImplementation((url, options) => {
if (url.startsWith('https://test.org/contact/cards?agent')) {
return Promise.resolve({
json: () => Promise.resolve(fetchCards)
});
}
if (url.startsWith('https://test.org/contact/cards/000a/profile?agent')) {
return Promise.resolve({
json: () => Promise.resolve(fetchProfile)
});
}
if (url.startsWith('https://test.org/contact/cards/000a/detail?agent')) {
return Promise.resolve({
json: () => Promise.resolve(fetchDetail)
});
}
2023-02-08 07:52:38 +00:00
if (url.startsWith('https://test.org/content/channels?contact')) {
return Promise.resolve({
json: () => Promise.resolve(fetchCardChannels)
});
}
2023-02-08 19:24:56 +00:00
if (url.startsWith('https://test.org/profile/message?contact')) {
fetchMessage = true;
return Promise.resolve({
json: () => Promise.resolve({})
});
}
else {
return Promise.resolve({
json: () => Promise.resolve([])
});
}
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
React.useContext = realUseContext;
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
2023-02-08 07:52:38 +00:00
test('add, update, and remove card', async () => {
render(<CardTestApp />)
await act(async () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setSession({ guid: 'abc', server: 'test.org', token: '123' });
await card.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('card').props.children).toHaveLength(0);
});
fetchCards = [{
id: '000a',
revision: 1,
data: {
detailRevision: 2,
profileRevision: 3,
2023-02-08 19:24:56 +00:00
notifiedProfile: 4,
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 () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('card').props.children).toHaveLength(1);
2023-02-08 19:24:56 +00:00
expect(screen.getByTestId('000a-handle').props.children).toBe('test1');
expect(fetchMessage).toBe(true);
});
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 3,
profileRevision: 4,
2023-02-08 19:24:56 +00:00
notifiedProfile: 4,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
},
}];
fetchProfile = {
guid: '01ab23',
handle: 'test2',
};
fetchDetail = {
status: 'confirmed',
}
await act(async () => {
const card = screen.getByTestId('card').props.card;
2023-02-08 19:24:56 +00:00
await card.actions.setRevision(4);
});
await waitFor(async () => {
expect(screen.getByTestId('card').props.children).toHaveLength(1);
2023-02-08 19:24:56 +00:00
expect(screen.getByTestId('000a-handle').props.children).toBe('test2');
});
fetchCards = [{
id: '000a',
revision: 3,
}];
await act(async () => {
const card = screen.getByTestId('card').props.card;
2023-02-08 19:24:56 +00:00
await card.actions.setRevision(4);
});
await waitFor(async () => {
expect(screen.getByTestId('card').props.children).toHaveLength(1);
});
await act(async () => {
const card = screen.getByTestId('card').props.card;
2023-02-08 19:24:56 +00:00
await card.actions.setRevision(5);
});
await waitFor(async () => {
expect(screen.getByTestId('card').props.children).toHaveLength(0);
});
});
2023-02-08 07:52:38 +00:00
test('add, update, and remove channel', async () => {
render(<CardTestApp />)
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 () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setSession({ guid: 'abc', server: 'test.org', token: '123' });
await card.actions.setRevision(1);
});
2023-02-08 07:52:38 +00:00
await waitFor(async () => {
2023-02-08 19:24:56 +00:00
expect(screen.getByTestId('000a').props.children).toHaveLength(1);
2023-02-08 07:52:38 +00:00
});
fetchCardChannels = [{
id: '01',
revision: 1,
data: {
detailRevision: 1,
topicRevision: 1,
channelDetail: {
dataType: 'superbasic',
data: 'testchannel',
},
channelSummary: {
dataType: 'superbasictopic',
data: 'testtopic',
},
},
}];
2023-02-08 19:24:56 +00:00
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
},
}];
2023-02-08 07:52:38 +00:00
await act(async () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setRevision(2);
});
2023-02-08 19:24:56 +00:00
await waitFor(async () => {
expect(screen.getByTestId('000a').props.children).toHaveLength(1);
});
2023-02-08 07:52:38 +00:00
fetchCards = [{
id: '000a',
2023-02-08 19:24:56 +00:00
revision: 2,
2023-02-08 07:52:38 +00:00
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 7,
notifiedView: 7,
},
}];
2023-02-08 19:24:56 +00:00
await act(async () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setRevision(3);
});
await waitFor(async () => {
expect(screen.getByTestId('000a').props.children).toHaveLength(2);
expect(screen.getByTestId('01').props.children).toBe('testchannel');
});
fetchCardChannels = [{
id: '01',
revision: 2,
data: {
detailRevision: 2,
topicRevision: 2,
channelDetail: {
dataType: 'superbasic',
data: 'testchannel2',
},
channelSummary: {
dataType: 'superbasictopic',
data: 'testtopic',
},
},
}];
fetchCards = [{
id: '000a',
revision: 2,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 8,
notifiedView: 7,
},
}];
await act(async () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setRevision(4);
});
await waitFor(async () => {
expect(screen.getByTestId('000a').props.children).toHaveLength(2);
expect(screen.getByTestId('01').props.children).toBe('testchannel2');
});
fetchCardChannels = [{
id: '01',
revision: 3,
}];
fetchCards = [{
id: '000a',
revision: 3,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 9,
notifiedView: 7,
},
}];
await act(async () => {
const card = screen.getByTestId('card').props.card;
await card.actions.setRevision(5);
});
await waitFor(async () => {
expect(screen.getByTestId('000a').props.children).toHaveLength(1);
});
2023-02-08 07:52:38 +00:00
});