From 06f4cff97e72c7e0d1072e1b42cb0dc5c795e790 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 24 Aug 2021 20:24:34 +0200 Subject: [PATCH] Fixed missing initial values when editing one domain redirects --- .../helpers/EditDomainRedirectsModal.tsx | 8 +++++--- .../helpers/EditDomainRedirectsModal.test.tsx | 20 +++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/domains/helpers/EditDomainRedirectsModal.tsx b/src/domains/helpers/EditDomainRedirectsModal.tsx index 6734c2c2..f4214ad7 100644 --- a/src/domains/helpers/EditDomainRedirectsModal.tsx +++ b/src/domains/helpers/EditDomainRedirectsModal.tsx @@ -25,9 +25,11 @@ const FormGroup: FC = ({ isLast, export const EditDomainRedirectsModal: FC = ( { isOpen, toggle, domain, editDomainRedirects }, ) => { - const [ baseUrlRedirect, setBaseUrlRedirect ] = useState(''); - const [ regular404Redirect, setRegular404Redirect ] = useState(''); - const [ invalidShortUrlRedirect, setInvalidShortUrlRedirect ] = useState(''); + const [ baseUrlRedirect, setBaseUrlRedirect ] = useState(domain.redirects?.baseUrlRedirect ?? ''); + const [ regular404Redirect, setRegular404Redirect ] = useState(domain.redirects?.regular404Redirect ?? ''); + const [ invalidShortUrlRedirect, setInvalidShortUrlRedirect ] = useState( + domain.redirects?.invalidShortUrlRedirect ?? '', + ); const handleSubmit = handleEventPreventingDefault(async () => editDomainRedirects(domain.domain, { baseUrlRedirect: nonEmptyValueOrNull(baseUrlRedirect), regular404Redirect: nonEmptyValueOrNull(regular404Redirect), diff --git a/test/domains/helpers/EditDomainRedirectsModal.test.tsx b/test/domains/helpers/EditDomainRedirectsModal.test.tsx index 23cdf46f..2293a807 100644 --- a/test/domains/helpers/EditDomainRedirectsModal.test.tsx +++ b/test/domains/helpers/EditDomainRedirectsModal.test.tsx @@ -9,7 +9,12 @@ describe('', () => { let wrapper: ShallowWrapper; const editDomainRedirects = jest.fn().mockResolvedValue(undefined); const toggle = jest.fn(); - const domain = Mock.of({ domain: 'foo.com' }); + const domain = Mock.of({ + domain: 'foo.com', + redirects: { + baseUrlRedirect: 'baz', + }, + }); beforeEach(() => { wrapper = shallow( @@ -51,7 +56,7 @@ describe('', () => { wrapper.find('form').simulate('submit', { preventDefault: jest.fn() }); expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', { - baseUrlRedirect: null, + baseUrlRedirect: 'baz', regular404Redirect: null, invalidShortUrlRedirect: null, }); @@ -75,5 +80,16 @@ describe('', () => { regular404Redirect: 'new_regular_404', invalidShortUrlRedirect: null, }); + + formGroups.at(0).simulate('change', ''); + formGroups.at(1).simulate('change', ''); + formGroups.at(2).simulate('change', ''); + + wrapper.find('form').simulate('submit', { preventDefault: jest.fn() }); + expect(editDomainRedirects).toHaveBeenCalledWith('foo.com', { + baseUrlRedirect: null, + regular404Redirect: null, + invalidShortUrlRedirect: null, + }); }); });