Skip to content

Commit

Permalink
update field when position is removed (#4343)
Browse files Browse the repository at this point in the history
* update field when position is removed

* update
  • Loading branch information
AkosuaA authored Aug 6, 2024
1 parent 1683f6d commit e185728
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Field />);

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: {
Expand Down

0 comments on commit e185728

Please sign in to comment.