1// @vitest-environment jsdom
2
3import { h } from 'preact';
4import { cleanup, render } from '@testing-library/preact';
5import { map, interval, pipe } from 'wonka';
6import { vi, expect, it, beforeEach, describe, afterEach } from 'vitest';
7
8import { Query } from './Query';
9import { Provider } from '../context';
10
11const query = '{ example }';
12const variables = {
13 myVar: 1234,
14};
15
16const client = {
17 executeQuery: vi.fn(() =>
18 pipe(
19 interval(200),
20 map((i: number) => ({ data: i, error: i + 1 }))
21 )
22 ),
23};
24
25describe('Query', () => {
26 beforeEach(() => {
27 vi.spyOn(globalThis.console, 'error').mockImplementation(() => {
28 // do nothing
29 });
30 });
31
32 afterEach(() => {
33 cleanup();
34 });
35
36 it('Should execute the query', async () => {
37 let props = {};
38 const Test = () => h('p', {}, 'hi');
39 const App = () => {
40 // @ts-ignore
41 return h(Provider, {
42 value: client,
43 children: [
44 // @ts-ignore
45 h(Query, { query, variables }, ({ data, fetching, error }) => {
46 props = { data, fetching, error };
47 // @ts-ignore
48 return h(Test, {});
49 }),
50 ],
51 });
52 };
53 render(h(App, {}));
54 expect(props).toStrictEqual({
55 data: undefined,
56 fetching: true,
57 error: undefined,
58 });
59
60 await new Promise(res => {
61 setTimeout(() => {
62 expect(props).toStrictEqual({ data: 0, fetching: false, error: 1 });
63 res(null);
64 }, 250);
65 });
66 });
67});