diff --git a/packages/contentful-app-extensions/user-positions/src/locations/Field.tsx b/packages/contentful-app-extensions/user-positions/src/locations/Field.tsx index 661ec98e4f..a29579c0a9 100644 --- a/packages/contentful-app-extensions/user-positions/src/locations/Field.tsx +++ b/packages/contentful-app-extensions/user-positions/src/locations/Field.tsx @@ -33,9 +33,7 @@ const Field = () => { const removePosition = (position: PositionProps) => { const newValue = positions.filter((i: PositionProps) => i !== position); setPositions(newValue); - if (newValue.length === 0) { - sdk.field.setValue(undefined); - } + sdk.field.setValue(newValue.length === 0 ? undefined : newValue); }; const updateFieldValue = () => { diff --git a/packages/contentful-app-extensions/user-positions/src/test/locations/Field.test.tsx b/packages/contentful-app-extensions/user-positions/src/test/locations/Field.test.tsx index 329dc106c6..8aaf229a6b 100644 --- a/packages/contentful-app-extensions/user-positions/src/test/locations/Field.test.tsx +++ b/packages/contentful-app-extensions/user-positions/src/test/locations/Field.test.tsx @@ -101,7 +101,49 @@ describe('Field component', () => { ]); }); - it('remove button works', () => { + it('remove button updates field', () => { + const mockSdk = { + ...mockBaseSdk, + field: { + getValue: jest.fn(() => [ + { + role: 'Some role', + department: 'Some department', + institution: 'Some institution', + }, + { + role: 'Another role', + department: 'Another department', + institution: 'Another institution', + }, + ]), + setValue: jest.fn(), + }, + }; + (useSDK as jest.Mock).mockReturnValue(mockSdk); + render(); + + expect(screen.getByText('Another role')).toBeInTheDocument(); + expect(screen.getByText('Another department')).toBeInTheDocument(); + expect(screen.getByText('Another institution')).toBeInTheDocument(); + expect(screen.getAllByRole('button', { name: /remove/i })).toHaveLength(2); + + fireEvent.click(screen.getAllByRole('button', { name: /remove/i })[0]); + + expect(mockSdk.field.setValue).toHaveBeenCalledWith([ + { + department: 'Another department', + institution: 'Another institution', + role: 'Another role', + }, + ]); + + expect(screen.queryByRole('Another role')).not.toBeInTheDocument(); + expect(screen.queryByRole('Another department')).not.toBeInTheDocument(); + expect(screen.queryByRole('Another institution')).not.toBeInTheDocument(); + }); + + it('remove button sets position as undefined if all positions are removed', () => { const mockSdk = { ...mockBaseSdk, field: {