fixing unit test

This commit is contained in:
balzack 2023-03-05 17:37:37 -08:00
parent 3b2dd6216e
commit f66f9bfbbe
2 changed files with 63 additions and 12 deletions

View File

@ -20,6 +20,7 @@ function ConversationView() {
setRenderCount(renderCount + 1); setRenderCount(renderCount + 1);
const rendered = []; const rendered = [];
conversation.state.topics.forEach((value) => { conversation.state.topics.forEach((value) => {
rendered.push(<Text key={value.topicId} testID={value.topicId}>{ value.detail.data }</Text>); rendered.push(<Text key={value.topicId} testID={value.topicId}>{ value.detail.data }</Text>);
}); });
setTopics(rendered); setTopics(rendered);
@ -45,10 +46,12 @@ function ConversationTestApp() {
) )
} }
let revision;
let fetchCards; let fetchCards;
let fetchChannels; let fetchChannels;
let fetchCardChannels; let fetchCardChannels;
let fetchTopics; let fetchTopics;
let fetchTopic;
const realUseContext = React.useContext; const realUseContext = React.useContext;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout; const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
@ -58,6 +61,8 @@ beforeEach(() => {
fetchCards = []; fetchCards = [];
fetchChannels = []; fetchChannels = [];
fetchCardChannels = []; fetchCardChannels = [];
fetchTopic = {};
revision = 0;
const mockUseContext = jest.fn().mockImplementation((ctx) => { const mockUseContext = jest.fn().mockImplementation((ctx) => {
if (ctx === StoreContext) { if (ctx === StoreContext) {
@ -80,15 +85,27 @@ beforeEach(() => {
}); });
} }
else if (url.startsWith('https://test.org/content/channels?contact')) { else if (url.startsWith('https://test.org/content/channels?contact')) {
return Promise.resolve({ return Promise.resolve({
json: () => Promise.resolve(fetchCardChannels) json: () => Promise.resolve(fetchCardChannels)
}); });
} }
else if (url.startsWith('https://test.org/content/channels/123/topics/888/detail?agent')) {
return Promise.resolve({
json: () => Promise.resolve(fetchTopic)
});
}
else if (url.startsWith('https://test.org/content/channels/aabb/topics/888/detail?contact')) {
return Promise.resolve({
json: () => Promise.resolve(fetchTopic)
});
}
else if (url.startsWith('https://test.org/content/channels/aabb/topics?contact') || else if (url.startsWith('https://test.org/content/channels/aabb/topics?contact') ||
url.startsWith('https://test.org/content/channels/123/topics?agent')) { url.startsWith('https://test.org/content/channels/123/topics?agent')) {
const headers = new Map(); const headers = new Map();
headers.set('topic-marker', 48); headers.set('topic-marker', 1);
headers.set('topic-revision', 55); headers.set('topic-revision', revision);
return Promise.resolve({ return Promise.resolve({
url: 'getTopics', url: 'getTopics',
status: 200, status: 200,
@ -113,7 +130,7 @@ afterEach(() => {
}); });
test('add, update, remove card channel topic', async () => { test('add, update, remove card channel topic', async () => {
render(<ConversationTestApp />) render(<ConversationTestApp />)
await act(async () => { await act(async () => {
@ -167,6 +184,7 @@ test('add, update, remove card channel topic', async () => {
}, },
]; ];
revision = 5;
fetchTopics = [ fetchTopics = [
{ id: '888', revision: 5, data: { { id: '888', revision: 5, data: {
detailRevision: 3, detailRevision: 3,
@ -220,6 +238,7 @@ test('add, update, remove card channel topic', async () => {
}, },
]; ];
revision = 6;
fetchTopics = [ fetchTopics = [
{ id: '888', revision: 6, data: { { id: '888', revision: 6, data: {
detailRevision: 4, detailRevision: 4,
@ -268,10 +287,27 @@ test('add, update, remove card channel topic', async () => {
}, },
]; ];
revision = 6;
fetchTopics = [ fetchTopics = [
{ id: '888', revision: 6 } { id: '888', revision: 6 }
]; ];
fetchTopic = {
id: '888', revision: 6, data: {
detailRevision: 4,
tagRevision: 0,
topicDetail: {
guid: '0123',
dataType: 'topictype',
data: 'contacttopicdata2',
created: 1,
updated: 1,
status: 'confirmed',
transform: 'complete',
},
}
}
await act(async () => { await act(async () => {
const card = screen.getByTestId('conversation').props.card; const card = screen.getByTestId('conversation').props.card;
await card.actions.setRevision(4); await card.actions.setRevision(4);
@ -281,7 +317,6 @@ test('add, update, remove card channel topic', async () => {
expect(screen.getByTestId('conversation').props.children).toHaveLength(1); expect(screen.getByTestId('conversation').props.children).toHaveLength(1);
}); });
fetchCards = [{ fetchCards = [{
id: '000a', id: '000a',
revision: 4, revision: 4,
@ -295,6 +330,7 @@ test('add, update, remove card channel topic', async () => {
}, },
}]; }];
revision = 7;
fetchCardChannels = [ fetchCardChannels = [
{ id: 'aabb', revision: 5, data: { { id: 'aabb', revision: 5, data: {
detailRevision: 3, detailRevision: 3,
@ -312,15 +348,9 @@ test('add, update, remove card channel topic', async () => {
expect(screen.getByTestId('conversation').props.children).toHaveLength(0); expect(screen.getByTestId('conversation').props.children).toHaveLength(0);
}); });
}); });
test('add, update, remove channel topic', async () => { test('add, update, remove channel topic', async () => {
render(<ConversationTestApp />) render(<ConversationTestApp />)
@ -340,6 +370,7 @@ test('add, update, remove channel topic', async () => {
expect(screen.getByTestId('conversation').props.children).toHaveLength(0); expect(screen.getByTestId('conversation').props.children).toHaveLength(0);
}); });
revision = 5;
fetchChannels = [ fetchChannels = [
{ id: '123', revision: 2, data: { { id: '123', revision: 2, data: {
detailRevision: 3, detailRevision: 3,
@ -408,6 +439,7 @@ test('add, update, remove channel topic', async () => {
expect(screen.getByTestId('888').props.children).toBe('contacttopicdata'); expect(screen.getByTestId('888').props.children).toBe('contacttopicdata');
}); });
revision = 6;
fetchChannels = [ fetchChannels = [
{ id: '123', revision: 3, data: { { id: '123', revision: 3, data: {
detailRevision: 3, detailRevision: 3,
@ -455,6 +487,22 @@ test('add, update, remove channel topic', async () => {
{ id: '888', revision: 8 } { id: '888', revision: 8 }
]; ];
fetchTopic = {
id: '888', revision: 7, data: {
detailRevision: 5,
tagRevision: 0,
topicDetail: {
guid: '0123',
dataType: 'topictype',
data: 'contacttopicdata2',
created: 1,
updated: 1,
status: 'confirmed',
transform: 'complete',
},
}
};
await act(async () => { await act(async () => {
const channel = screen.getByTestId('conversation').props.channel; const channel = screen.getByTestId('conversation').props.channel;
await channel.actions.setRevision(4); await channel.actions.setRevision(4);
@ -464,6 +512,7 @@ test('add, update, remove channel topic', async () => {
expect(screen.getByTestId('conversation').props.children).toHaveLength(1); expect(screen.getByTestId('conversation').props.children).toHaveLength(1);
}); });
revision = 7;
fetchChannels = [ fetchChannels = [
{ id: '123', revision: 5, data: { { id: '123', revision: 5, data: {
detailRevision: 3, detailRevision: 3,
@ -481,7 +530,6 @@ test('add, update, remove channel topic', async () => {
expect(screen.getByTestId('conversation').props.children).toHaveLength(0); expect(screen.getByTestId('conversation').props.children).toHaveLength(0);
}); });
}); });

View File

@ -103,6 +103,9 @@ export function useConversationContext() {
} }
else if (ignoreRevision || topicRevision > curSyncRevision.current) { else if (ignoreRevision || topicRevision > curSyncRevision.current) {
const delta = await getTopicDelta(cardId, channelId, curSyncRevision.current, null, curTopicMarker.current, null); const delta = await getTopicDelta(cardId, channelId, curSyncRevision.current, null, curTopicMarker.current, null);
if (topicRevision > delta.revision) {
throw new Error("invalid topic revision");
}
await setTopicDelta(cardId, channelId, delta.topics); await setTopicDelta(cardId, channelId, delta.topics);
await setSyncRevision(cardId, channelId, delta.revision); await setSyncRevision(cardId, channelId, delta.revision);
curSyncRevision.current = delta.revision; curSyncRevision.current = delta.revision;