|
| 1 | +import React, { useRef } from 'react'; |
| 2 | +import test from 'ava'; |
| 3 | +import { Provider, encase } from 'react-jpex'; |
| 4 | +import { renderHook } from '@testing-library/react-hooks'; |
| 5 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 6 | +import jpex from 'jpex'; |
| 7 | + |
| 8 | +const baseJpex = jpex; |
| 9 | + |
| 10 | +test.afterEach(cleanup); |
| 11 | + |
| 12 | +test.serial('injects dependencies into a hook', (t) => { |
| 13 | + type Foo = string; |
| 14 | + const useFoo = encase((foo: Foo) => () => { |
| 15 | + // just for the sake of proving this is a react hook |
| 16 | + const ref = useRef(foo); |
| 17 | + return ref.current; |
| 18 | + }); |
| 19 | + |
| 20 | + const { result: { current } } = renderHook(useFoo, { |
| 21 | + wrapper: Provider, |
| 22 | + initialProps: { |
| 23 | + onMount(jpex) { |
| 24 | + jpex.constant<Foo>('foo'); |
| 25 | + }, |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + t.is(current, 'foo'); |
| 30 | +}); |
| 31 | + |
| 32 | +test.serial('injects dependencies into a component', async(t) => { |
| 33 | + type Foo = string; |
| 34 | + const Component = encase((foo: Foo) => (props: { bar: string }) => { |
| 35 | + const ref = useRef(props.bar); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div> |
| 39 | + <span>{ref.current}</span> |
| 40 | + <span>{foo}</span> |
| 41 | + </div> |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + render( |
| 46 | + <Provider onMount={(jpex) => jpex.constant<Foo>('foo')}> |
| 47 | + <Component bar="bar"/> |
| 48 | + </Provider> |
| 49 | + ); |
| 50 | + |
| 51 | + await screen.findByText('foo'); |
| 52 | + await screen.findByText('bar'); |
| 53 | + |
| 54 | + t.pass(); |
| 55 | +}); |
| 56 | + |
| 57 | +test.serial('when props change it still works', async(t) => { |
| 58 | + type Foo = string; |
| 59 | + const Component = encase((foo: Foo) => (props: { bar: string }) => { |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + <span>{props.bar}</span> |
| 63 | + <span>{foo}</span> |
| 64 | + </div> |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + const { rerender } = render( |
| 69 | + <Provider onMount={(jpex) => jpex.constant<Foo>('foo')}> |
| 70 | + <Component bar="bar"/> |
| 71 | + </Provider> |
| 72 | + ); |
| 73 | + |
| 74 | + await screen.findByText('foo'); |
| 75 | + await screen.findByText('bar'); |
| 76 | + |
| 77 | + rerender( |
| 78 | + <Provider onMount={(jpex) => jpex.constant<Foo>('foo')}> |
| 79 | + <Component bar="baz"/> |
| 80 | + </Provider> |
| 81 | + ); |
| 82 | + |
| 83 | + await screen.findByText('foo'); |
| 84 | + await screen.findByText('baz'); |
| 85 | + |
| 86 | + t.pass(); |
| 87 | +}); |
| 88 | + |
| 89 | +test.serial('nested encases', async(t) => { |
| 90 | + const jpex = baseJpex.extend(); |
| 91 | + |
| 92 | + type Foo = string; |
| 93 | + jpex.constant<Foo>('foo'); |
| 94 | + |
| 95 | + const useFoo = encase((foo: Foo) => () => { |
| 96 | + // just for the sake of proving this is a react hook |
| 97 | + const ref = useRef(foo); |
| 98 | + return ref.current; |
| 99 | + }); |
| 100 | + type UseFoo = typeof useFoo; |
| 101 | + jpex.constant<UseFoo>(useFoo); |
| 102 | + |
| 103 | + const Component = encase((useFoo: UseFoo) => () => { |
| 104 | + const foo = useFoo(); |
| 105 | + |
| 106 | + return ( |
| 107 | + <div> |
| 108 | + <span>{foo}</span> |
| 109 | + </div> |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + render( |
| 114 | + <Provider value={jpex}> |
| 115 | + <Component/> |
| 116 | + </Provider> |
| 117 | + ); |
| 118 | + |
| 119 | + await screen.findByText('foo'); |
| 120 | + |
| 121 | + t.pass(); |
| 122 | +}); |
0 commit comments