import { transformSync } from '@babel/core' import ReactCompiler from 'babel-plugin-react-compiler' import ReactHookFormNoMemo from './react-hook-form-no-memo' const transform = (code) => { const output = transformSync(code, { babelrc: false, configFile: false, filename: 'test.js', plugins: [ ReactHookFormNoMemo, [ ReactCompiler, { target: '18', }, ], ], }) return output.code } test('named import', () => { const code = `import { useForm } from 'react-hook-form' const Component = () => { const form = useForm() return null }; ` expect(transform(code)).toBe(`import { useForm } from 'react-hook-form'; const Component = () => { "use no memo"; const form = useForm(); return null; };`) }) test('local alias', () => { const code = `import { useForm as useFormLocal } from 'react-hook-form' const Component = () => { const form = useFormLocal() return null }; ` expect(transform(code)) .toBe(`import { useForm as useFormLocal } from 'react-hook-form'; const Component = () => { "use no memo"; const form = useFormLocal(); return null; };`) }) test('function without block statement', () => { const code = `import { useForm } from 'react-hook-form' const useThing = () => useForm() ` expect(transform(code)).toBe(`import { useForm } from 'react-hook-form'; const useThing = () => { "use no memo"; return useForm(); };`) }) test('function with manual opt-out', () => { const code = `import { useForm } from 'react-hook-form' const useThing = () => { "use no memo" return useForm() } ` expect(transform(code)).toBe(`import { useForm } from 'react-hook-form'; const useThing = () => { "use no memo"; return useForm(); };`) }) test('function compiled', () => { const code = `import { useForm } from 'something-else' const Component = () => { useForm() return null } ` expect(transform(code)).not.toMatch(/use no memo/) })