|
| 1 | +import { fireEvent, render } from '@testing-library/react'; |
| 2 | +import React from 'react'; |
| 3 | +import { createContext, useContext } from '../src'; |
| 4 | +import { RenderTimer, Value } from './common'; |
| 5 | + |
| 6 | +describe('Basic', () => { |
| 7 | + interface User { |
| 8 | + name: string; |
| 9 | + age: number; |
| 10 | + } |
| 11 | + |
| 12 | + const UserContext = createContext<User>(); |
| 13 | + |
| 14 | + const Root = ({ children }: { children?: React.ReactNode }) => { |
| 15 | + const [name, setName] = React.useState('bamboo'); |
| 16 | + const [age, setAge] = React.useState(30); |
| 17 | + |
| 18 | + return ( |
| 19 | + <UserContext.Provider value={{ name, age }}> |
| 20 | + <input id="name" value={name} onChange={e => setName(e.target.value)} /> |
| 21 | + <input id="age" value={age} onChange={e => setAge(Number(e.target.value))} /> |
| 22 | + |
| 23 | + {children} |
| 24 | + </UserContext.Provider> |
| 25 | + ); |
| 26 | + }; |
| 27 | + |
| 28 | + function changeValue(container: HTMLElement, id: string, value: string) { |
| 29 | + fireEvent.change(container.querySelector(`#${id}`), { |
| 30 | + target: { |
| 31 | + value, |
| 32 | + }, |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + it('raw', () => { |
| 37 | + const Raw = () => { |
| 38 | + const user = useContext(UserContext); |
| 39 | + |
| 40 | + return ( |
| 41 | + <> |
| 42 | + <Value id="value" value={user} /> |
| 43 | + <RenderTimer id="raw" /> |
| 44 | + </> |
| 45 | + ); |
| 46 | + }; |
| 47 | + |
| 48 | + const { container } = render( |
| 49 | + <Root> |
| 50 | + <Raw /> |
| 51 | + </Root>, |
| 52 | + ); |
| 53 | + |
| 54 | + // Mount |
| 55 | + expect(container.querySelector('#raw')!.textContent).toEqual('1'); |
| 56 | + |
| 57 | + // Update `name`: Full Update |
| 58 | + changeValue(container, 'name', 'light'); |
| 59 | + expect(container.querySelector('#raw')!.textContent).toEqual('2'); |
| 60 | + expect(container.querySelector('#value')!.textContent).toEqual( |
| 61 | + JSON.stringify({ |
| 62 | + name: 'light', |
| 63 | + age: 30, |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + // Update `age`: Full Update |
| 68 | + changeValue(container, 'age', '20'); |
| 69 | + expect(container.querySelector('#raw')!.textContent).toEqual('3'); |
| 70 | + expect(container.querySelector('#value')!.textContent).toEqual( |
| 71 | + JSON.stringify({ |
| 72 | + name: 'light', |
| 73 | + age: 20, |
| 74 | + }), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('PropName', () => { |
| 79 | + const PropName = ({ name }: { name: keyof User }) => { |
| 80 | + const value = useContext(UserContext, name); |
| 81 | + |
| 82 | + return ( |
| 83 | + <> |
| 84 | + <Value id={`${name}-value`} value={value} /> |
| 85 | + <RenderTimer id={`${name}-times`} /> |
| 86 | + </> |
| 87 | + ); |
| 88 | + }; |
| 89 | + |
| 90 | + const { container } = render( |
| 91 | + <Root> |
| 92 | + <PropName name="name" /> |
| 93 | + <PropName name="age" /> |
| 94 | + </Root>, |
| 95 | + ); |
| 96 | + |
| 97 | + // Mount |
| 98 | + expect(container.querySelector('#name-times')!.textContent).toEqual('1'); |
| 99 | + expect(container.querySelector('#age-times')!.textContent).toEqual('1'); |
| 100 | + |
| 101 | + // Update `name`: Partial Update |
| 102 | + changeValue(container, 'name', 'light'); |
| 103 | + expect(container.querySelector('#name-times')!.textContent).toEqual('2'); |
| 104 | + expect(container.querySelector('#name-value')!.textContent).toEqual('light'); |
| 105 | + expect(container.querySelector('#age-times')!.textContent).toEqual('1'); |
| 106 | + |
| 107 | + // Update `age`: Partial Update |
| 108 | + changeValue(container, 'age', '20'); |
| 109 | + expect(container.querySelector('#name-times')!.textContent).toEqual('2'); |
| 110 | + expect(container.querySelector('#age-times')!.textContent).toEqual('2'); |
| 111 | + expect(container.querySelector('#age-value')!.textContent).toEqual('20'); |
| 112 | + }); |
| 113 | +}); |
0 commit comments