Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
at main 1.5 kB view raw
1// @vitest-environment jsdom 2 3import { h } from 'preact'; 4import { cleanup, render, act } from '@testing-library/preact'; 5import { map, interval, pipe } from 'wonka'; 6import { vi, expect, it, beforeEach, describe, afterEach } from 'vitest'; 7 8import { Provider } from '../context'; 9import { Subscription } from './Subscription'; 10 11const query = 'subscription Example { example }'; 12const client = { 13 executeSubscription: vi.fn(() => { 14 return pipe( 15 interval(200), 16 map((i: number) => ({ data: i, error: i + 1 })) 17 ); 18 }), 19}; 20 21describe('Subscription', () => { 22 beforeEach(() => { 23 vi.useFakeTimers(); 24 vi.spyOn(globalThis.console, 'error').mockImplementation(() => { 25 // do nothing 26 }); 27 }); 28 29 afterEach(() => { 30 cleanup(); 31 }); 32 33 it('Should execute the subscription', () => { 34 let props = {}; 35 const Test = () => h('p', {}, 'hi'); 36 const App = () => { 37 // @ts-ignore 38 return h(Provider, { 39 value: client, 40 children: [ 41 // @ts-ignore 42 h(Subscription, { query }, ({ data, fetching, error }) => { 43 props = { data, fetching, error }; 44 // @ts-ignore 45 return h(Test, {}); 46 }), 47 ], 48 }); 49 }; 50 51 render(h(App, {})); 52 53 expect(props).toStrictEqual({ 54 data: undefined, 55 fetching: true, 56 error: undefined, 57 }); 58 59 act(() => { 60 vi.advanceTimersByTime(200); 61 }); 62 63 expect(props).toStrictEqual({ data: 0, fetching: true, error: 1 }); 64 }); 65});