databag/net/web/test/Thread.test.js

269 lines
7.1 KiB
JavaScript
Raw Normal View History

2023-01-27 05:00:10 +00:00
import React, { useState, useEffect, useContext } from 'react';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
2023-01-27 05:00:10 +00:00
import { AppContextProvider } from 'context/AppContext';
import { AccountContextProvider } from 'context/AccountContext';
import { ProfileContext, ProfileContextProvider } from 'context/ProfileContext';
import { StoreContextProvider } from 'context/StoreContext';
2024-03-08 00:43:36 +00:00
import { SettingsContextProvider } from 'context/SettingsContext';
2023-01-27 05:00:10 +00:00
import { ConversationContextProvider } from 'context/ConversationContext';
2023-01-27 19:28:28 +00:00
import { CardContext, CardContextProvider } from 'context/CardContext';
2023-01-27 05:00:10 +00:00
import { UploadContextProvider } from 'context/UploadContext';
import { useConversation } from 'session/conversation/useConversation.hook';
import * as fetchUtil from 'api/fetchUtil';
2023-01-27 19:28:28 +00:00
let conversation = null;
let card = null;
2023-01-27 05:00:10 +00:00
function ThreadView() {
const { state, actions } = useConversation('card01', 'channel01');
const [renderCount, setRenderCount] = useState(0);
2023-01-27 19:28:28 +00:00
const [topics, setTopics] = useState([]);
conversation = actions;
card = useContext(CardContext);
2023-01-27 05:00:10 +00:00
useEffect(() => {
const rendered = [];
2023-01-27 19:28:28 +00:00
const entries = Array.from(state.topics.values());
entries.forEach(entry => {
2023-01-27 19:28:28 +00:00
rendered.push(
<div key={entry.id} data-testid={entry.id}>{ entry.text }</div>
)
2023-01-27 19:28:28 +00:00
});
setTopics(rendered);
2023-01-27 05:00:10 +00:00
setRenderCount(renderCount + 1);
}, [state]);
return (
<div data-testid="thread" count={renderCount}>{ topics }</div>
2023-01-27 05:00:10 +00:00
);
}
function ThreadTestApp() {
return (
<StoreContextProvider>
<UploadContextProvider>
<CardContextProvider>
<ProfileContextProvider>
<AccountContextProvider>
2024-03-08 00:43:36 +00:00
<SettingsContextProvider>
2023-01-27 05:00:10 +00:00
<ConversationContextProvider>
<AppContextProvider>
<ThreadView />
</AppContextProvider>
</ConversationContextProvider>
2024-03-08 00:43:36 +00:00
</SettingsContextProvider>
2023-01-27 05:00:10 +00:00
</AccountContextProvider>
</ProfileContextProvider>
</CardContextProvider>
</UploadContextProvider>
</StoreContextProvider>
);
}
2023-01-27 19:28:28 +00:00
let fetchCards;
let fetchChannels;
let fetchTopics;
let fetchDetail;
2023-01-27 05:00:10 +00:00
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
beforeEach(() => {
2023-01-27 19:28:28 +00:00
fetchCards = [];
fetchChannels = [];
fetchTopics = [];
fetchDetail = {};
2023-01-27 05:00:10 +00:00
const mockFetch = jest.fn().mockImplementation((url, options) => {
2023-01-27 19:28:28 +00:00
if (url.startsWith('/contact/cards?agent')) {
return Promise.resolve({
json: () => Promise.resolve(fetchCards)
2023-01-27 19:28:28 +00:00
});
}
if (url.startsWith('https://test.org/content/channels?contact')) {
return Promise.resolve({
json: () => Promise.resolve(fetchChannels)
2023-01-27 19:28:28 +00:00
});
}
if (url.startsWith('https://test.org/content/channels/channel01/topics?contact')) {
const headers = new Map();
headers.set('topic-marker', 48);
headers.set('topic-revision', 55);
return Promise.resolve({
url: 'getTopics',
status: 200,
headers: headers,
json: () => Promise.resolve(fetchTopics),
});
}
if (url.startsWith('https://test.org/content/channels/channel01/topics/topic01/detail?contact')) {
return Promise.resolve({
json: () => Promise.resolve(fetchDetail)
2023-01-27 19:28:28 +00:00
});
}
else {
2023-01-27 19:28:28 +00:00
return Promise.resolve({
json: () => Promise.resolve([])
2023-01-27 19:28:28 +00:00
});
2023-01-27 05:00:10 +00:00
}
});
fetchUtil.fetchWithTimeout = mockFetch;
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('add, update, remove topic', async () => {
2023-01-27 19:28:28 +00:00
render(<ThreadTestApp />);
await waitFor(async () => {
expect(card).not.toBe(null);
expect(conversation).not.toBe(null);
});
fetchCards = [{
id: 'card01',
revision: 1,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 6,
notifiedView: 7,
cardDetail: { status: 'connected', statusUpdate: 136, token: '01ab', },
cardProfile: { guid: 'guid01', handle: 'test1', name: 'tester', imageSet: false,
seal: 'abc', version: '1.1.1', node: 'test.org' },
2023-01-27 19:28:28 +00:00
},
}];
2023-01-27 19:28:28 +00:00
fetchChannels = [
{ id: 'channel01', revision: 2, data: {
2023-01-27 19:28:28 +00:00
detailRevision: 3,
topicRevision: 5,
channelSummary: { guid: 'guid01', dataType: 'superbasictopic', data: 'testcardtopic' },
channelDetail: { dataType: 'superbasic', data: 'testcardchannel' },
}
2023-01-27 19:28:28 +00:00
},
];
fetchTopics = [
{ id: 'topic01', revision: 1, data: {
detailRevision: 1,
tagRevision: 1,
topicDetail: {
guid: 'guid01',
dataType: 'superbasictopic',
data: JSON.stringify({ text: 'message' }),
created: 1,
updated: 1,
status: 'confirmed',
transform: 'complete',
2023-01-27 19:28:28 +00:00
},
}},
2023-01-27 19:28:28 +00:00
];
await act(async () => {
card.actions.setToken('abc123');
card.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('thread').children).toHaveLength(1);
expect(screen.getByTestId('topic01').textContent).toBe('message');
});
2023-01-27 19:28:28 +00:00
fetchCards = [{
id: 'card01',
revision: 2,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 7,
notifiedView: 7,
2023-01-27 19:28:28 +00:00
},
}];
2023-01-27 19:28:28 +00:00
fetchChannels = [
{ id: 'channel01', revision: 3, data: {
2023-01-27 19:28:28 +00:00
detailRevision: 3,
topicRevision: 6,
channelSummary: { guid: 'guid01', dataType: 'superbasictopic', data: 'testcardtopic' },
channelDetail: { dataType: 'superbasic', data: 'testcardchannel' },
}
2023-01-27 19:28:28 +00:00
},
];
fetchTopics = [
{ id: 'topic01', revision: 2, data: {
2024-04-25 20:29:25 +00:00
detailRevision: 2,
tagRevision: 1,
}},
];
fetchDetail = { id: 'topic01', revision: 2, data: {
detailRevision: 2,
tagRevision: 1,
topicDetail: {
guid: 'guid01',
dataType: 'superbasictopic',
data: JSON.stringify({ text: 'message2' }),
created: 1,
updated: 1,
status: 'confirmed',
transform: 'complete',
}
}}
2023-01-27 19:28:28 +00:00
await act(async () => {
card.actions.setRevision(2);
});
await waitFor(async () => {
expect(screen.getByTestId('topic01').textContent).toBe('message2');
});
2023-01-27 19:28:28 +00:00
fetchCards = [{
id: 'card01',
revision: 3,
data: {
detailRevision: 2,
profileRevision: 3,
notifiedProfile: 3,
notifiedArticle: 5,
notifiedChannel: 8,
notifiedView: 7,
2023-01-27 19:28:28 +00:00
},
}];
2023-01-27 19:28:28 +00:00
fetchChannels = [
{ id: 'channel01', revision: 4, data: {
2023-01-27 19:28:28 +00:00
detailRevision: 3,
topicRevision: 7,
channelSummary: { guid: 'guid01', dataType: 'superbasictopic', data: 'testcardtopic' },
channelDetail: { dataType: 'superbasic', data: 'testcardchannel' },
}
2023-01-27 19:28:28 +00:00
},
];
fetchTopics = [
{ id: 'topic01', revision: 2 },
];
2023-01-27 19:28:28 +00:00
await act(async () => {
card.actions.setRevision(3);
});
await waitFor(async () => {
expect(screen.getByTestId('thread').children).toHaveLength(0);
});
2023-01-27 05:00:10 +00:00
});