|
| 1 | +import type { FieldValues } from 'react-hook-form'; |
| 2 | +import { useForm } from 'react-hook-form'; |
| 3 | +import { LensCore, LensesStorage } from '@hookform/lenses'; |
| 4 | +import { renderHook } from '@testing-library/react'; |
| 5 | + |
| 6 | +// Create a custom LensCore class that extends the base class |
| 7 | +class CustomLensCore<T extends FieldValues> extends LensCore<T> { |
| 8 | + // Add a custom method |
| 9 | + public getValue(): string { |
| 10 | + return `Custom value at path: ${this.path}`; |
| 11 | + } |
| 12 | + |
| 13 | + // Add another custom method |
| 14 | + public getCustomInfo(): string { |
| 15 | + return `CustomLensCore instance at ${this.path}`; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +test('LensCore extension should preserve custom methods when using focus()', () => { |
| 20 | + const { result } = renderHook(() => |
| 21 | + useForm<{ |
| 22 | + user: { |
| 23 | + name: string; |
| 24 | + email: string; |
| 25 | + }; |
| 26 | + }>({ |
| 27 | + defaultValues: { |
| 28 | + user: { |
| 29 | + name: 'John', |
| 30 | + |
| 31 | + }, |
| 32 | + }, |
| 33 | + }), |
| 34 | + ); |
| 35 | + |
| 36 | + const control = result.current.control; |
| 37 | + const cache = new LensesStorage(control); |
| 38 | + const rootLens = new CustomLensCore(control, '', cache); |
| 39 | + |
| 40 | + // Focus on nested paths |
| 41 | + const userLens = rootLens.focus('user'); |
| 42 | + const nameLens = userLens.focus('name'); |
| 43 | + |
| 44 | + // Verify that the focused lenses are instances of CustomLensCore |
| 45 | + expect(userLens).toBeInstanceOf(CustomLensCore); |
| 46 | + expect(nameLens).toBeInstanceOf(CustomLensCore); |
| 47 | + |
| 48 | + // Verify that custom methods are available |
| 49 | + expect((userLens as CustomLensCore<any>).getValue()).toBe('Custom value at path: user'); |
| 50 | + expect((userLens as CustomLensCore<any>).getCustomInfo()).toBe('CustomLensCore instance at user'); |
| 51 | + |
| 52 | + expect((nameLens as CustomLensCore<any>).getValue()).toBe('Custom value at path: user.name'); |
| 53 | + expect((nameLens as CustomLensCore<any>).getCustomInfo()).toBe('CustomLensCore instance at user.name'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('LensCore extension should preserve custom methods when using reflect()', () => { |
| 57 | + const { result } = renderHook(() => |
| 58 | + useForm<{ |
| 59 | + profile: { |
| 60 | + contact: { |
| 61 | + firstName: string; |
| 62 | + phone: string; |
| 63 | + }; |
| 64 | + }; |
| 65 | + }>({ |
| 66 | + defaultValues: { |
| 67 | + profile: { |
| 68 | + contact: { |
| 69 | + firstName: 'Jane', |
| 70 | + phone: '123-456-7890', |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }), |
| 75 | + ); |
| 76 | + |
| 77 | + const control = result.current.control; |
| 78 | + const cache = new LensesStorage(control); |
| 79 | + const rootLens = new CustomLensCore(control, '', cache); |
| 80 | + |
| 81 | + // Use reflect to restructure |
| 82 | + const contactLens = rootLens.reflect((_dictionary, l) => ({ |
| 83 | + name: l.focus('profile.contact.firstName'), |
| 84 | + phoneNumber: l.focus('profile.contact.phone'), |
| 85 | + })); |
| 86 | + |
| 87 | + // Verify that the reflected lens is an instance of CustomLensCore |
| 88 | + expect(contactLens).toBeInstanceOf(CustomLensCore); |
| 89 | + expect((contactLens as CustomLensCore<any>).getValue()).toBe('Custom value at path: '); |
| 90 | + expect((contactLens as CustomLensCore<any>).getCustomInfo()).toBe('CustomLensCore instance at '); |
| 91 | + |
| 92 | + // Focus on the reflected structure |
| 93 | + const nameLens = contactLens.focus('name'); |
| 94 | + expect(nameLens).toBeInstanceOf(CustomLensCore); |
| 95 | +}); |
| 96 | + |
| 97 | +test('LensCore extension should preserve custom methods when using static create()', () => { |
| 98 | + const { result } = renderHook(() => |
| 99 | + useForm<{ |
| 100 | + firstName: string; |
| 101 | + lastName: string; |
| 102 | + }>({ |
| 103 | + defaultValues: { |
| 104 | + firstName: 'Test', |
| 105 | + lastName: 'User', |
| 106 | + }, |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + const control = result.current.control; |
| 111 | + const cache = new LensesStorage(control); |
| 112 | + |
| 113 | + // Use the static create method on the custom class |
| 114 | + const lens = CustomLensCore.create(control, cache); |
| 115 | + |
| 116 | + // Verify that it creates an instance of CustomLensCore |
| 117 | + expect(lens).toBeInstanceOf(CustomLensCore); |
| 118 | + expect((lens as unknown as CustomLensCore<any>).getValue()).toBe('Custom value at path: '); |
| 119 | + expect((lens as unknown as CustomLensCore<any>).getCustomInfo()).toBe('CustomLensCore instance at '); |
| 120 | + |
| 121 | + // Verify that focused lenses also preserve the custom class |
| 122 | + const firstNameLens = lens.focus('firstName'); |
| 123 | + expect(firstNameLens).toBeInstanceOf(CustomLensCore); |
| 124 | + expect((firstNameLens as unknown as CustomLensCore<any>).getValue()).toBe('Custom value at path: firstName'); |
| 125 | +}); |
0 commit comments