|
1 | | -import { act, render, screen } from '@testing-library/react'; |
| 1 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
2 | 2 | import { observer } from 'mobx-react-lite'; |
3 | | -import { ReactNode } from 'react'; |
4 | | -import { describe, expect, test } from 'vitest'; |
| 3 | +import { ReactNode, useState } from 'react'; |
| 4 | +import { describe, expect, test, vi } from 'vitest'; |
5 | 5 |
|
6 | | -import { ViewModelsProvider } from '..'; |
| 6 | +import { ViewModelStore, ViewModelsProvider } from '..'; |
7 | 7 | import { createCounter } from '../utils'; |
8 | 8 | import { TestViewModelStoreImpl } from '../view-model/abstract-view-model.store.test'; |
9 | 9 | import { TestViewModelImpl } from '../view-model/view-model.impl.test'; |
@@ -112,30 +112,179 @@ describe('withViewModel', () => { |
112 | 112 | expect(await screen.findAllByText('hello my-test')).toHaveLength(2); |
113 | 113 | }); |
114 | 114 |
|
115 | | - test('renders with view model store', async () => { |
| 115 | + test('View should be only mounted (renders only 1 time)', () => { |
116 | 116 | class VM extends TestViewModelImpl {} |
117 | | - const View = observer(({ model }: ViewModelProps<VM>) => { |
| 117 | + const View = vi.fn(({ model }: ViewModelProps<VM>) => { |
| 118 | + return <div>{`hello ${model.id}`}</div>; |
| 119 | + }); |
| 120 | + const Component = withViewModel(VM, { generateId: createIdGenerator() })( |
| 121 | + View, |
| 122 | + ); |
| 123 | + |
| 124 | + render(<Component />); |
| 125 | + expect(View).toHaveBeenCalledTimes(1); |
| 126 | + }); |
| 127 | + |
| 128 | + test('withViewModel wrapper should by only mounted (renders only 1 time)', () => { |
| 129 | + class VM extends TestViewModelImpl {} |
| 130 | + const View = vi.fn(({ model }: ViewModelProps<VM>) => { |
| 131 | + return <div>{`hello ${model.id}`}</div>; |
| 132 | + }); |
| 133 | + |
| 134 | + const useHookSpy = vi.fn(() => {}); |
| 135 | + |
| 136 | + const Component = withViewModel(VM, { |
| 137 | + generateId: createIdGenerator(), |
| 138 | + reactHooks: useHookSpy, // the save renders count as withViewModel wrapper |
| 139 | + })(View); |
| 140 | + |
| 141 | + render(<Component />); |
| 142 | + expect(useHookSpy).toHaveBeenCalledTimes(1); |
| 143 | + }); |
| 144 | + |
| 145 | + test('View should be updated when payload is changed', async () => { |
| 146 | + class VM extends TestViewModelImpl<{ counter: number }> {} |
| 147 | + const View = vi.fn(({ model }: ViewModelProps<VM>) => { |
| 148 | + return <div>{`hello ${model.id}`}</div>; |
| 149 | + }); |
| 150 | + const Component = withViewModel(VM, { generateId: createIdGenerator() })( |
| 151 | + View, |
| 152 | + ); |
| 153 | + |
| 154 | + const SuperContainer = () => { |
| 155 | + const [counter, setCounter] = useState(0); |
| 156 | + |
118 | 157 | return ( |
119 | | - <div> |
120 | | - <div>{`hello my friend. Model id is ${model.id}`}</div> |
121 | | - </div> |
| 158 | + <> |
| 159 | + <button |
| 160 | + data-testid={'increment'} |
| 161 | + onClick={() => setCounter(counter + 1)} |
| 162 | + > |
| 163 | + increment |
| 164 | + </button> |
| 165 | + <Component payload={{ counter }} /> |
| 166 | + </> |
122 | 167 | ); |
| 168 | + }; |
| 169 | + |
| 170 | + await act(() => render(<SuperContainer />)); |
| 171 | + |
| 172 | + const incrementButton = screen.getByTestId('increment'); |
| 173 | + |
| 174 | + fireEvent.click(incrementButton); |
| 175 | + fireEvent.click(incrementButton); |
| 176 | + fireEvent.click(incrementButton); |
| 177 | + |
| 178 | + expect(View).toHaveBeenCalledTimes(4); |
| 179 | + }); |
| 180 | + |
| 181 | + test('View should have actual payload state', async () => { |
| 182 | + let vm: TestViewModelImpl<{ counter: number }> | null; |
| 183 | + |
| 184 | + class VM extends TestViewModelImpl<{ counter: number }> { |
| 185 | + constructor(...args: any[]) { |
| 186 | + super(...args); |
| 187 | + vm = this; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + const View = vi.fn(({ model }: ViewModelProps<VM>) => { |
| 192 | + return <div>{`hello ${model.id}`}</div>; |
123 | 193 | }); |
124 | | - const Component = withViewModel(VM, { generateId: () => '1' })(View); |
125 | | - const vmStore = new TestViewModelStoreImpl(); |
| 194 | + const Component = withViewModel(VM, { generateId: createIdGenerator() })( |
| 195 | + View, |
| 196 | + ); |
| 197 | + |
| 198 | + const SuperContainer = () => { |
| 199 | + const [counter, setCounter] = useState(0); |
126 | 200 |
|
127 | | - const Wrapper = ({ children }: { children?: ReactNode }) => { |
128 | 201 | return ( |
129 | | - <ViewModelsProvider value={vmStore}>{children}</ViewModelsProvider> |
| 202 | + <> |
| 203 | + <button |
| 204 | + data-testid={'increment'} |
| 205 | + onClick={() => setCounter(counter + 1)} |
| 206 | + > |
| 207 | + increment |
| 208 | + </button> |
| 209 | + <Component payload={{ counter }} /> |
| 210 | + </> |
130 | 211 | ); |
131 | 212 | }; |
132 | 213 |
|
133 | | - await act(async () => |
134 | | - render(<Component />, { |
135 | | - wrapper: Wrapper, |
136 | | - }), |
137 | | - ); |
| 214 | + await act(() => render(<SuperContainer />)); |
| 215 | + |
| 216 | + const incrementButton = screen.getByTestId('increment'); |
| 217 | + |
| 218 | + fireEvent.click(incrementButton); |
| 219 | + fireEvent.click(incrementButton); |
| 220 | + fireEvent.click(incrementButton); |
138 | 221 |
|
139 | | - expect(screen.getByText('hello my friend. Model id is VM_1')).toBeDefined(); |
| 222 | + // @ts-ignore |
| 223 | + expect(vm?.payload).toEqual({ counter: 3 }); |
| 224 | + }); |
| 225 | + |
| 226 | + describe('with ViewModelStore', () => { |
| 227 | + test('renders', async () => { |
| 228 | + class VM extends TestViewModelImpl {} |
| 229 | + const View = observer(({ model }: ViewModelProps<VM>) => { |
| 230 | + return ( |
| 231 | + <div> |
| 232 | + <div>{`hello my friend. Model id is ${model.id}`}</div> |
| 233 | + </div> |
| 234 | + ); |
| 235 | + }); |
| 236 | + const Component = withViewModel(VM, { generateId: () => '1' })(View); |
| 237 | + const vmStore = new TestViewModelStoreImpl(); |
| 238 | + |
| 239 | + const Wrapper = ({ children }: { children?: ReactNode }) => { |
| 240 | + return ( |
| 241 | + <ViewModelsProvider value={vmStore}>{children}</ViewModelsProvider> |
| 242 | + ); |
| 243 | + }; |
| 244 | + |
| 245 | + await act(async () => |
| 246 | + render(<Component />, { |
| 247 | + wrapper: Wrapper, |
| 248 | + }), |
| 249 | + ); |
| 250 | + |
| 251 | + expect( |
| 252 | + screen.getByText('hello my friend. Model id is VM_1'), |
| 253 | + ).toBeDefined(); |
| 254 | + }); |
| 255 | + |
| 256 | + test('able to get access to view model store', async () => { |
| 257 | + let viewModels: ViewModelStore = null as any; |
| 258 | + |
| 259 | + class VM extends TestViewModelImpl { |
| 260 | + constructor(params: any) { |
| 261 | + super(params); |
| 262 | + viewModels = params.viewModels; |
| 263 | + } |
| 264 | + } |
| 265 | + const View = observer(({ model }: ViewModelProps<VM>) => { |
| 266 | + return ( |
| 267 | + <div> |
| 268 | + <div>{`hello my friend. Model id is ${model.id}`}</div> |
| 269 | + </div> |
| 270 | + ); |
| 271 | + }); |
| 272 | + const Component = withViewModel(VM, { generateId: () => '1' })(View); |
| 273 | + const vmStore = new TestViewModelStoreImpl(); |
| 274 | + |
| 275 | + const Wrapper = ({ children }: { children?: ReactNode }) => { |
| 276 | + return ( |
| 277 | + <ViewModelsProvider value={vmStore}>{children}</ViewModelsProvider> |
| 278 | + ); |
| 279 | + }; |
| 280 | + |
| 281 | + await act(async () => |
| 282 | + render(<Component />, { |
| 283 | + wrapper: Wrapper, |
| 284 | + }), |
| 285 | + ); |
| 286 | + |
| 287 | + expect(viewModels).toBeDefined(); |
| 288 | + }); |
140 | 289 | }); |
141 | 290 | }); |
0 commit comments