databag/net/web/test/Listing.test.tsx

104 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-01-22 06:38:02 +00:00
import React, { useState, useEffect, useContext } from 'react';
import {render, act, screen, waitFor, fireEvent} from '@testing-library/react'
import { ProfileContextProvider } from 'context/ProfileContext';
2024-03-08 00:43:36 +00:00
import { SettingsContextProvider } from 'context/SettingsContext';
2023-01-22 06:38:02 +00:00
import { useListing } from 'session/listing/useListing.hook';
import * as fetchUtil from 'api/fetchUtil';
let listing = null;
function ListingView() {
const { state, actions } = useListing();
const [renderCount, setRenderCount] = useState(0);
const [contacts, setContacts] = useState([]);
listing = actions;
useEffect(() => {
const rendered = [];
state.contacts.forEach(item => {
rendered.push(
<div key={item.guid} data-testid="contact">
<span key={item.guid} data-testid={'contact-' + item.guid}>{ item.name }</span>
</div>
);
});
setContacts(rendered);
setRenderCount(renderCount + 1);
}, [state]);
return (
//@ts-ignore
2023-01-22 06:38:02 +00:00
<div data-testid="contacts" count={renderCount}>
{ contacts }
</div>
);
}
function ListingTestApp() {
return (
<ProfileContextProvider>
2024-03-08 00:43:36 +00:00
<SettingsContextProvider>
2023-01-22 06:38:02 +00:00
<ListingView />
2024-03-08 00:43:36 +00:00
</SettingsContextProvider>
2023-01-22 06:38:02 +00:00
</ProfileContextProvider>
);
}
let fetchListing;
const realFetchWithTimeout = fetchUtil.fetchWithTimeout;
const realFetchWithCustomTimeout = fetchUtil.fetchWithCustomTimeout;
beforeEach(() => {
fetchListing = [];
const mockFetch = jest.fn().mockImplementation((url, options) => {
return Promise.resolve({
json: () => Promise.resolve(fetchListing)
});
});
//@ts-ignore
2023-01-22 06:38:02 +00:00
fetchUtil.fetchWithTimeout = mockFetch;
//@ts-ignore
2023-01-22 06:38:02 +00:00
fetchUtil.fetchWithCustomTimeout = mockFetch;
});
afterEach(() => {
//@ts-ignore
2023-01-22 06:38:02 +00:00
fetchUtil.fetchWithTimeout = realFetchWithTimeout;
//@ts-ignore
2023-01-22 06:38:02 +00:00
fetchUtil.fetchWithCustomTimeout = realFetchWithCustomTimeout;
});
test('retrieve listing', async () => {
render(<ListingTestApp />);
await waitFor(() => {
expect(listing).not.toBe(null);
});
fetchListing = [
{
guid: 'abc123',
handle: 'tester',
name: 'mr. tester',
description: 'a tester',
location: 'here',
imageSet: false,
version: '0.0.1',
node: 'test.org',
},
];
await act(async () => {
await listing.getListing();
});
await waitFor(async () => {
expect(screen.getByTestId('contact-abc123').textContent).toBe('mr. tester');
});
});