more channel context testing

This commit is contained in:
balzack 2023-01-05 14:33:06 -08:00
parent 1aa19dc654
commit 1a63381813
3 changed files with 147 additions and 13 deletions

View File

@ -8,7 +8,6 @@ export async function getChannels(token, revision) {
let types = encodeURIComponent(JSON.stringify([ 'sealed', 'superbasic' ])); let types = encodeURIComponent(JSON.stringify([ 'sealed', 'superbasic' ]));
param += `&types=${types}` param += `&types=${types}`
let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' }); let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' });
checkResponse(channels) checkResponse(channels)
let ret = await channels.json() let ret = await channels.json()
return ret; return ret;

View File

@ -134,7 +134,7 @@ export function useChannelContext() {
const channel = channels.current.get(channelId); const channel = channels.current.get(channelId);
if (channel.revision === revision) { if (channel.revision === revision) {
channel.data.unsealedSummary = unsealed; channel.data.unsealedSummary = unsealed;
channels.current.set(channelId, chanel); channels.current.set(channelId, channel);
updateState({ channels: channels.current }); updateState({ channels: channels.current });
} }
}, },

View File

@ -15,8 +15,12 @@ function ChannelView() {
const rendered = [] const rendered = []
const entries = Array.from(channel.state.channels.values()); const entries = Array.from(channel.state.channels.values());
entries.forEach(entry => { entries.forEach(entry => {
console.log(entry.data.unsealedSubject);
rendered.push( rendered.push(
<div key={entry.id} data-testid="channel"> <div key={entry.id} data-testid="channel">
<span data-testid="unsealed-subject">{ entry.data.unsealedSubject }</span>
<span data-testid="unsealed-summary">{ entry.data.unsealedSummary }</span>
<span data-testid="detail">{ entry.data.channelDetail.data }</span> <span data-testid="detail">{ entry.data.channelDetail.data }</span>
<span data-testid="summary">{ entry.data.channelSummary.data }</span> <span data-testid="summary">{ entry.data.channelSummary.data }</span>
</div>); </div>);
@ -49,6 +53,11 @@ let statusChannels;
let fetchDetail; let fetchDetail;
let fetchSummary; let fetchSummary;
let fetchChannels; let fetchChannels;
let addedChannel;
let removedChannel;
let updatedChannel;
let addedCard;
let removedCard;
beforeEach(() => { beforeEach(() => {
statusDetail = 200; statusDetail = 200;
@ -56,24 +65,64 @@ beforeEach(() => {
statusChannels = 200; statusChannels = 200;
fetchDetail = {}; fetchDetail = {};
fetchSummary = {}; fetchSummary = {};
fetchChannels = {}; fetchChannels =[];
addedChannel = [];
updatedChannel = [];
removedChannel = [];
addedCard = [];
removedCard = [];
const mockFetch = jest.fn().mockImplementation((url, options) => { const mockFetch = jest.fn().mockImplementation((url, options) => {
if (url.includes('detail')) { if (options.method === 'POST') {
addedChannel.push(options.body);
return Promise.resolve({ return Promise.resolve({
status: 200,
json: () => Promise.resolve({ id: 'id1' }),
});
}
else if (options.method === 'PUT') {
const params = url.split('/');
if (params[4] === 'cards') {
addedCard.push(params[5].split('?')[0]);
}
else {
updatedChannel.push({ id: params[3], body: options.body });
}
return Promise.resolve({
status: 200,
json: () => Promise.resolve({}),
});
}
else if (options.method === 'DELETE') {
const params = url.split('/');
if (params[4] === 'cards') {
removedCard.push(params[5].split('?')[0]);
}
else {
removedChannel.push(params[3].split('?')[0]);
}
return Promise.resolve({
status: 200,
json: () => Promise.resolve(),
});
}
else if (url.includes('detail')) {
return Promise.resolve({
url: 'getDetail',
status: statusDetail, status: statusDetail,
json: () => Promise.resolve(fetchDetail), json: () => Promise.resolve(fetchDetail),
}); });
} }
else if (url.includes('summary')) { else if (url.includes('summary')) {
return Promise.resolve({ return Promise.resolve({
url: 'getSummary',
status: statusSummary, status: statusSummary,
json: () => Promise.resolve(fetchSummary), json: () => Promise.resolve(fetchSummary),
}); });
} }
else { else {
return Promise.resolve({ return Promise.resolve({
status: 200, url: 'getChannels',
status: statusChannels,
json: () => Promise.resolve(fetchChannels), json: () => Promise.resolve(fetchChannels),
}); });
} }
@ -87,9 +136,7 @@ afterEach(() => {
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout; fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
}); });
test('add, update and remove channel', async () => { test('api invocation', async () => {
fetchDetail = { dataType: 'superbasic', data: 'testdata' };
fetchSummary = { guid: '11', dataType: 'superbasictopic', data: 'testdata' };
render(<ChannelTestApp />); render(<ChannelTestApp />);
@ -97,6 +144,43 @@ test('add, update and remove channel', async () => {
expect(channelContext).not.toBe(null); expect(channelContext).not.toBe(null);
}); });
await act( async () => {
channelContext.actions.setToken('abc123');
});
await act(async () => {
await channelContext.actions.addChannel('testtype', 'testsubject', ['testcard']);
await channelContext.actions.setChannelSubject('123', 'testtype', 'testsubject');
await channelContext.actions.setChannelCard('123', 'newcard');
await channelContext.actions.clearChannelCard('123', 'newcard');
await channelContext.actions.removeChannel('123');
});
await waitFor(async () => {
expect(addedChannel.length).toBe(1);
const added = JSON.parse(addedChannel[0]);
expect(added.dataType).toBe('testtype');
expect(updatedChannel.length).toBe(1);
expect(updatedChannel[0].id).toBe('123');
expect(removedChannel.length).toBe(1);
expect(removedChannel[0]).toBe('123');
expect(addedCard.length).toBe(1);
expect(addedCard[0]).toBe('newcard');
expect(addedCard[0]).toBe('newcard');
});
await act( async () => {
channelContext.actions.clearToken();
});
});
test('add, update and remove channel', async () => {
render(<ChannelTestApp />);
fetchDetail = { dataType: 'superbasic', data: 'testdata' };
fetchSummary = { guid: '11', dataType: 'superbasictopic', data: 'testdata' };
fetchChannels = [ fetchChannels = [
{ id: '123', revision: 2, data: { { id: '123', revision: 2, data: {
detailRevision: 3, detailRevision: 3,
@ -107,8 +191,15 @@ test('add, update and remove channel', async () => {
}, },
]; ];
await waitFor(async () => {
expect(channelContext).not.toBe(null);
});
await act( async () => { await act( async () => {
channelContext.actions.setToken('abc123'); channelContext.actions.setToken('abc123');
});
await act( async () => {
await channelContext.actions.setRevision(1); await channelContext.actions.setRevision(1);
}); });
@ -121,6 +212,8 @@ test('add, update and remove channel', async () => {
expect(screen.getByTestId('summary').textContent).toBe('testdata'); expect(screen.getByTestId('summary').textContent).toBe('testdata');
}); });
const count = parseInt(screen.getByTestId('channels').attributes.count.value) + 1;
fetchChannels = [ fetchChannels = [
{ id: '123', revision: 3, data: { { id: '123', revision: 3, data: {
detailRevision: 4, detailRevision: 4,
@ -137,6 +230,17 @@ test('add, update and remove channel', async () => {
await waitFor(async () => { await waitFor(async () => {
expect(screen.getByTestId('channels').children).toHaveLength(1); expect(screen.getByTestId('channels').children).toHaveLength(1);
expect(screen.getByTestId('detail').textContent).toBe('testdata2'); expect(screen.getByTestId('detail').textContent).toBe('testdata2');
expect(screen.getByTestId('channels').attributes.count.value).toBe(count.toString());
});
await act( async () => {
await channelContext.actions.unsealChannelSubject('123', 'unsealedsubject', 3);
await channelContext.actions.unsealChannelSummary('123', 'unsealedsummary', 3);
});
await waitFor(async () => {
expect(screen.getByTestId('unsealed-subject').textContent).toBe('unsealedsubject');
expect(screen.getByTestId('unsealed-summary').textContent).toBe('unsealedsummary');
}); });
fetchChannels = [ { id: '123', revision: 3 } ]; fetchChannels = [ { id: '123', revision: 3 } ];
@ -160,10 +264,41 @@ test('add, update and remove channel', async () => {
await waitFor(async () => { await waitFor(async () => {
expect(screen.getByTestId('channels').attributes.offsync.value).toBe("false"); expect(screen.getByTestId('channels').attributes.offsync.value).toBe("false");
}); });
await act( async () => {
channelContext.actions.clearToken();
}); });
});
test('resync', async () => {
render(<ChannelTestApp />);
await waitFor(async () => {
expect(channelContext).not.toBe(null);
});
await act( async () => {
channelContext.actions.setToken('abc123');
});
await act( async () => {
statusChannels = 500;
await channelContext.actions.setRevision(1);
});
await waitFor(async () => {
expect(screen.getByTestId('channels').attributes.offsync.value).toBe("true");
});
await act( async () => {
statusChannels = 200;
await channelContext.actions.resync();
});
await waitFor(async () => {
expect(screen.getByTestId('channels').attributes.offsync.value).toBe("false");
});
});
// set and clear of channel card
// add, update and remove topic
// unseal topic and channels
// offsync resync