From 88694899236702e540c75cbeec8f2711d9d706b3 Mon Sep 17 00:00:00 2001 From: tate Date: Mon, 19 Jun 2023 09:50:01 +1000 Subject: [PATCH] fix: allow removing abi in advanced editor --- src/hooks/useAdvancedEditor.ts | 11 +++-- .../AdvancedEditor/AdvancedEditor.test.tsx | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/hooks/useAdvancedEditor.ts b/src/hooks/useAdvancedEditor.ts index ebe5fefe8..69cc06f15 100644 --- a/src/hooks/useAdvancedEditor.ts +++ b/src/hooks/useAdvancedEditor.ts @@ -1,6 +1,7 @@ import { useEffect, useState } from 'react' import { useForm } from 'react-hook-form' import { useTranslation } from 'react-i18next' +import { Pattern, match } from 'ts-pattern' import { RecordOptions } from '@ensdomains/ensjs/utils/recordHelpers' @@ -242,12 +243,10 @@ const useAdvancedEditor = ({ profile, loading, overwrites, callback }: Props) => const contentHash = dirtyFields.other?.contentHash - const abi = dirtyFields.other?.abi?.data - ? { - data: dirtyFields.other.abi.data, - contentType: 1, - } - : undefined + const abi = match(dirtyFields.other?.abi?.data) + .with('', () => ({ data: '' })) + .with(Pattern.string, (data) => ({ data, contentType: 1 })) + .otherwise(() => undefined) const records = { texts, diff --git a/src/transaction-flow/input/AdvancedEditor/AdvancedEditor.test.tsx b/src/transaction-flow/input/AdvancedEditor/AdvancedEditor.test.tsx index d3e160fc5..caddcb705 100644 --- a/src/transaction-flow/input/AdvancedEditor/AdvancedEditor.test.tsx +++ b/src/transaction-flow/input/AdvancedEditor/AdvancedEditor.test.tsx @@ -95,6 +95,24 @@ const mockProfileData = { addr: 'HN7cABqLq46Es1jh92dQQisAq662SmxELLLsHHe4YWrH', }, ], + abi: { + data: JSON.stringify([ + { + inputs: [ + { + internalType: 'string', + type: 'string', + name: 'name', + }, + ], + stateMutability: 'nonpayable', + outputs: [], + name: 'setName', + type: 'function', + }, + ]), + contentType: 1, + }, }, resolverAddress: '0x0', isMigrated: true, @@ -405,4 +423,28 @@ describe('AdvancedEditor', () => { } } }) + + it('should allow removing abi', async () => { + render( + {}} data={{ name: 'test.eth' }} />, + ) + const tab = await screen.findByTestId('other-tab') + fireEvent.click(tab) + + const abiInput = await screen.findByLabelText('advancedEditor.tabs.other.abi.label') + await userEvent.clear(abiInput) + + const submitBtn = screen.getByText('action.save') + await waitFor(() => { + expect(submitBtn).not.toHaveAttribute('disabled') + }) + await userEvent.click(submitBtn) + + await waitFor(() => { + expect(mockDispatch).toHaveBeenCalled() + }) + expect(mockDispatch.mock.calls[0][0].payload[0].data.records.abi).toEqual({ + data: '', + }) + }) })