mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
extending channel test
This commit is contained in:
parent
b1b26b3fe4
commit
e1743004c8
@ -8,6 +8,7 @@ export async function getChannels(token, revision) {
|
||||
let types = encodeURIComponent(JSON.stringify([ 'sealed', 'superbasic' ]));
|
||||
param += `&types=${types}`
|
||||
let channels = await fetchWithTimeout('/content/channels' + param, { method: 'GET' });
|
||||
|
||||
checkResponse(channels)
|
||||
let ret = await channels.json()
|
||||
return ret;
|
||||
|
@ -1,4 +1,5 @@
|
||||
import React, { useState, useEffect, useContext } from 'react';
|
||||
import { prettyDOM } from '@testing-library/dom'
|
||||
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
|
||||
import { ChannelContextProvider, ChannelContext } from 'context/ChannelContext';
|
||||
import * as fetchUtil from 'api/fetchUtil';
|
||||
@ -6,16 +7,27 @@ import * as fetchUtil from 'api/fetchUtil';
|
||||
let channelContext = null;
|
||||
function ChannelView() {
|
||||
const [renderCount, setRenderCount] = useState(0);
|
||||
const [channels, setChannels] = useState([]);
|
||||
const channel = useContext(ChannelContext);
|
||||
channelContext = channel;
|
||||
|
||||
useEffect(() => {
|
||||
const rendered = []
|
||||
const entries = Array.from(channel.state.channels.values());
|
||||
entries.forEach(entry => {
|
||||
rendered.push(
|
||||
<div key={entry.id} data-testid="channel">
|
||||
<span data-testid="detail">{ entry.data.channelDetail.data }</span>
|
||||
<span data-testid="summary">{ entry.data.channelSummary.data }</span>
|
||||
</div>);
|
||||
});
|
||||
setChannels(rendered);
|
||||
setRenderCount(renderCount + 1);
|
||||
}, [channel.state]);
|
||||
}, [channel.state])
|
||||
|
||||
return (
|
||||
<div>
|
||||
<span data-testid="count">{ renderCount }</span>
|
||||
<div data-testid="channels" count={renderCount}>
|
||||
{ channels }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -45,6 +57,36 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
test('testing channel sync', async () => {
|
||||
|
||||
fetching = (url, options) => {
|
||||
if (url.startsWith('/content/channels/123/detail')) {
|
||||
return Promise.resolve({
|
||||
status: 200,
|
||||
json: () => Promise.resolve({ dataType: 'superbasic', data: 'testdata' }),
|
||||
});
|
||||
}
|
||||
else if (url.startsWith('/content/channels/123/summary')) {
|
||||
return Promise.resolve({
|
||||
status: 200,
|
||||
json: () => Promise.resolve({ guid: '11', dataType: 'superbasictopic', data: 'testdata' }),
|
||||
});
|
||||
}
|
||||
else {
|
||||
return Promise.resolve({
|
||||
status: 200,
|
||||
json: () => Promise.resolve([
|
||||
{ id: '123', revision: 2, data: {
|
||||
detailRevision: 3,
|
||||
topicRevision: 5,
|
||||
channelSummary: { guid: '11', dataType: 'superbasictopic', data: 'testdata' },
|
||||
channelDetail: { dataType: 'superbasic', data: 'testdata' },
|
||||
}
|
||||
},
|
||||
])
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
render(<ChannelTestApp />);
|
||||
|
||||
await waitFor(async () => {
|
||||
@ -55,6 +97,16 @@ test('testing channel sync', async () => {
|
||||
channelContext.actions.setToken('abc123');
|
||||
await channelContext.actions.setRevision(1);
|
||||
});
|
||||
|
||||
//screen.getByTestId('count').attributes.count.value
|
||||
//console.log(prettyDOM(screen.getByTestId('channels')));
|
||||
|
||||
await waitFor(async () => {
|
||||
expect(screen.getByTestId('channels').children).toHaveLength(1);
|
||||
expect(screen.getByTestId('detail').textContent).toBe('testdata');
|
||||
expect(screen.getByTestId('summary').textContent).toBe('testdata');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user