|
| 1 | +import { act, render, screen } from '@testing-library/react'; |
| 2 | +import { createCounter } from 'mobx-vm-entities/utils/counter'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | + |
| 5 | +import { PageViewModelMock } from '../page-view-model/page-view-model.impl.test'; |
| 6 | + |
| 7 | +import { withLazyPageViewModel } from './with-lazy-page-view-model'; |
| 8 | +import { PageViewModelProps } from './with-page-view-model'; |
| 9 | + |
| 10 | +const createIdGenerator = (prefix?: string) => { |
| 11 | + const counter = createCounter(); |
| 12 | + return () => (prefix ?? '') + counter().toString(); |
| 13 | +}; |
| 14 | + |
| 15 | +describe('withLazyPageViewModel', () => { |
| 16 | + test('renders (required path param)', async () => { |
| 17 | + class VM extends PageViewModelMock<{ foo: string }> { |
| 18 | + mount() { |
| 19 | + super.mount(); |
| 20 | + } |
| 21 | + } |
| 22 | + const View = ({ model }: PageViewModelProps<VM>) => { |
| 23 | + return <div data-testid={'view'}>{`hello ${model.id}`}</div>; |
| 24 | + }; |
| 25 | + const Component = withLazyPageViewModel( |
| 26 | + async () => { |
| 27 | + return { |
| 28 | + Model: VM, |
| 29 | + View, |
| 30 | + }; |
| 31 | + }, |
| 32 | + { |
| 33 | + generateId: createIdGenerator(), |
| 34 | + }, |
| 35 | + ); |
| 36 | + |
| 37 | + await act(async () => render(<Component params={{ foo: 'bar' }} />)); |
| 38 | + expect(screen.getByText('hello VM_0')).toBeDefined(); |
| 39 | + }); |
| 40 | + test('renders (optional path param)', async () => { |
| 41 | + class VM extends PageViewModelMock<{ foo?: string }> { |
| 42 | + mount() { |
| 43 | + super.mount(); |
| 44 | + } |
| 45 | + } |
| 46 | + const View = ({ model }: PageViewModelProps<VM>) => { |
| 47 | + return <div data-testid={'view'}>{`hello ${model.id}`}</div>; |
| 48 | + }; |
| 49 | + const Component = withLazyPageViewModel( |
| 50 | + async () => { |
| 51 | + return { |
| 52 | + Model: VM, |
| 53 | + View, |
| 54 | + }; |
| 55 | + }, |
| 56 | + { |
| 57 | + generateId: createIdGenerator(), |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + await act(async () => render(<Component />)); |
| 62 | + expect(screen.getByText('hello VM_0')).toBeDefined(); |
| 63 | + }); |
| 64 | + test('renders (no path params)', async () => { |
| 65 | + class VM extends PageViewModelMock { |
| 66 | + mount() { |
| 67 | + super.mount(); |
| 68 | + } |
| 69 | + } |
| 70 | + const View = ({ model }: PageViewModelProps<VM>) => { |
| 71 | + return <div data-testid={'view'}>{`hello ${model.id}`}</div>; |
| 72 | + }; |
| 73 | + const Component = withLazyPageViewModel( |
| 74 | + async () => { |
| 75 | + return { |
| 76 | + Model: VM, |
| 77 | + View, |
| 78 | + }; |
| 79 | + }, |
| 80 | + { |
| 81 | + generateId: createIdGenerator(), |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + await act(async () => render(<Component />)); |
| 86 | + expect(screen.getByText('hello VM_0')).toBeDefined(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments