Skip to content

Commit

Permalink
fix(ESSNTL-5418): Trim group name before validate (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkarat authored Nov 23, 2023
1 parent 53ebfa8 commit 36d8403
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/components/InventoryGroups/Modals/CreateGroupModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import { createGroup, validateGroupName } from '../utils/api';
import { useDispatch } from 'react-redux';
import awesomeDebouncePromise from 'awesome-debounce-promise';

export const validate = async (value) => {
const results = await validateGroupName(value.trim());
if (results === true) {
throw 'Group name already exists';
}

return undefined;
};

const CreateGroupModal = ({
isModalOpen,
setIsModalOpen,
Expand All @@ -31,17 +40,9 @@ const CreateGroupModal = ({
);

const schema = useMemo(() => {
const check = async (value) => {
const results = await validateGroupName(value);
if (results === true) {
throw 'Group name already exists';
}

return undefined;
};

// eslint-disable-next-line new-cap
const d = awesomeDebouncePromise(check, 500, { onlyResolvesLast: false });
const d = awesomeDebouncePromise(validate, 500, {
onlyResolvesLast: false,
});
return createGroupSchema(d);
}, []);

Expand Down
25 changes: 25 additions & 0 deletions src/components/InventoryGroups/Modals/CreateGroupModal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { validateGroupName } from '../utils/api';
import { validate } from './CreateGroupModal';

jest.mock('../utils/api');

describe('validate function', () => {
it('works with basic input', async () => {
const result = await validate('test');

expect(result).toBe(undefined);
expect(validateGroupName).toHaveBeenCalledWith('test');
});

it('trims input', async () => {
const result = await validate(' test ');

expect(result).toBe(undefined);
expect(validateGroupName).toHaveBeenCalledWith('test');
});

it('throws error if the name is present', async () => {
validateGroupName.mockResolvedValue(true);
await expect(validate('test')).rejects.toBe('Group name already exists');
});
});

0 comments on commit 36d8403

Please sign in to comment.