|
| 1 | +import React from 'react'; |
| 2 | +import { render, cleanup, act } from '@testing-library/react/pure'; |
| 3 | +import { |
| 4 | + view, |
| 5 | + store, |
| 6 | + autoEffect, |
| 7 | + clearEffect, |
| 8 | + // eslint-disable-next-line import/no-unresolved |
| 9 | +} from 'react-easy-state'; |
| 10 | + |
| 11 | +describe.only('autoEffect', () => { |
| 12 | + afterEach(cleanup); |
| 13 | + |
| 14 | + test('should auto run global effects', () => { |
| 15 | + let documentTitle = ''; |
| 16 | + const app = store({ name: 'Online Store' }); |
| 17 | + |
| 18 | + const effect = autoEffect(() => { |
| 19 | + documentTitle = app.name; |
| 20 | + }); |
| 21 | + expect(documentTitle).toBe('Online Store'); |
| 22 | + |
| 23 | + act(() => { |
| 24 | + app.name = 'Learning Platform'; |
| 25 | + }); |
| 26 | + expect(documentTitle).toBe('Learning Platform'); |
| 27 | + |
| 28 | + clearEffect(effect); |
| 29 | + act(() => { |
| 30 | + app.name = 'Social Platform'; |
| 31 | + }); |
| 32 | + expect(documentTitle).toBe('Learning Platform'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should auto run local effects in function components', () => { |
| 36 | + let documentTitle = ''; |
| 37 | + |
| 38 | + const app = store({ name: 'Online Store' }); |
| 39 | + |
| 40 | + const MyComp = view(() => { |
| 41 | + autoEffect(() => { |
| 42 | + documentTitle = app.name; |
| 43 | + }); |
| 44 | + return <div>{app.name}</div>; |
| 45 | + }); |
| 46 | + |
| 47 | + const { container, unmount } = render(<MyComp />); |
| 48 | + expect(container).toHaveTextContent('Online Store'); |
| 49 | + expect(documentTitle).toBe('Online Store'); |
| 50 | + |
| 51 | + act(() => { |
| 52 | + app.name = 'Learning Platform'; |
| 53 | + }); |
| 54 | + expect(container).toHaveTextContent('Learning Platform'); |
| 55 | + expect(documentTitle).toBe('Learning Platform'); |
| 56 | + |
| 57 | + unmount(); |
| 58 | + act(() => { |
| 59 | + app.name = 'Social Platform'; |
| 60 | + }); |
| 61 | + expect(documentTitle).toBe('Learning Platform'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should be recreated on dependency changes in function components', () => { |
| 65 | + let documentTitle = ''; |
| 66 | + |
| 67 | + const app = store({ name: 'Store' }); |
| 68 | + |
| 69 | + const MyComp = view(({ name }) => { |
| 70 | + autoEffect(() => { |
| 71 | + documentTitle = `${name} ${app.name}`; |
| 72 | + }, [name]); |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {name} |
| 76 | + {app.name} |
| 77 | + </div> |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + const { container, rerender } = render(<MyComp name="Online" />); |
| 82 | + expect(container).toHaveTextContent('Online Store'); |
| 83 | + expect(documentTitle).toBe('Online Store'); |
| 84 | + |
| 85 | + rerender(<MyComp name="Awesome" />); |
| 86 | + expect(container).toHaveTextContent('Awesome Store'); |
| 87 | + expect(documentTitle).toBe('Awesome Store'); |
| 88 | + |
| 89 | + act(() => { |
| 90 | + app.name = 'Page'; |
| 91 | + }); |
| 92 | + expect(container).toHaveTextContent('Awesome Page'); |
| 93 | + expect(documentTitle).toBe('Awesome Page'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments