| 
									
										
										
										
											2025-04-01 18:10:11 +08:00
										 |  |  | import { render, screen } from '@testing-library/react' | 
					
						
							|  |  |  | import '@testing-library/jest-dom' | 
					
						
							|  |  |  | import { z } from 'zod' | 
					
						
							|  |  |  | import withValidation from '.' | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2025-04-01 18:10:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('withValidation HOC', () => { | 
					
						
							|  |  |  |   // schema for validation
 | 
					
						
							|  |  |  |   const schema = z.object({ name: z.string() }) | 
					
						
							|  |  |  |   type Props = z.infer<typeof schema> & { | 
					
						
							|  |  |  |     age: number | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const TestComponent = ({ name, age }: Props) => ( | 
					
						
							|  |  |  |     <div>{name} - {age}</div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  |   const WrappedComponent = withValidation(TestComponent, schema) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeAll(() => { | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |     jest.spyOn(console, 'error').mockImplementation(noop) | 
					
						
							| 
									
										
										
										
											2025-04-01 18:10:11 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(() => { | 
					
						
							|  |  |  |     jest.restoreAllMocks() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('renders the component when validation passes', () => { | 
					
						
							|  |  |  |     render(<WrappedComponent name='Valid Name' age={30} />) | 
					
						
							|  |  |  |     expect(screen.getByText('Valid Name - 30')).toBeInTheDocument() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('renders the component when props is invalid but not in schema ', () => { | 
					
						
							|  |  |  |     render(<WrappedComponent name='Valid Name' age={'aaa' as any} />) | 
					
						
							|  |  |  |     expect(screen.getByText('Valid Name - aaa')).toBeInTheDocument() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('does not render the component when validation fails', () => { | 
					
						
							|  |  |  |     render(<WrappedComponent name={123 as any} age={30} />) | 
					
						
							|  |  |  |     expect(screen.queryByText('123 - 30')).toBeNull() | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | }) |