From 770ba624c2e6c7ebc5cdbc3fa44c2d3cb89208af Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 26 Jun 2021 17:44:26 +0200 Subject: [PATCH] Created TagsSelector test --- src/tags/helpers/TagsSelector.tsx | 19 +++---- test/tags/helpers/TagsSelector.test.tsx | 66 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 test/tags/helpers/TagsSelector.test.tsx diff --git a/src/tags/helpers/TagsSelector.tsx b/src/tags/helpers/TagsSelector.tsx index b401f77f..21cd1cb8 100644 --- a/src/tags/helpers/TagsSelector.tsx +++ b/src/tags/helpers/TagsSelector.tsx @@ -25,9 +25,9 @@ const TagsSelector = (colorGenerator: ColorGenerator) => ( listTags(); }, []); - const renderTag = ({ tag, onDelete }: TagComponentProps) => + const ReactTagsTag = ({ tag, onDelete }: TagComponentProps) => ; - const renderSuggestion = ({ item }: SuggestionComponentProps) => ( + const ReactTagsSuggestion = ({ item }: SuggestionComponentProps) => ( <> {item.name} @@ -37,23 +37,20 @@ const TagsSelector = (colorGenerator: ColorGenerator) => ( return ( !selectedTags.includes(tag)).map(toComponentTag)} - suggestionComponent={renderSuggestion} + suggestionComponent={ReactTagsSuggestion} allowNew addOnBlur placeholderText={placeholder} minQueryLength={1} onDelete={(removedTagIndex) => { - selectedTags.splice(removedTagIndex, 1); + const tagsCopy = [ ...selectedTags ]; - onChange(selectedTags); - }} - onAddition={({ name: newTag }) => { - const tags = [ ...selectedTags, newTag.toLowerCase() ]; // eslint-disable-line @typescript-eslint/no-unsafe-call - - onChange(tags); + tagsCopy.splice(removedTagIndex, 1); + onChange(tagsCopy); }} + onAddition={({ name: newTag }) => onChange([ ...selectedTags, newTag.toLowerCase() ])} /> ); }; diff --git a/test/tags/helpers/TagsSelector.test.tsx b/test/tags/helpers/TagsSelector.test.tsx new file mode 100644 index 00000000..92725838 --- /dev/null +++ b/test/tags/helpers/TagsSelector.test.tsx @@ -0,0 +1,66 @@ +import { shallow, ShallowWrapper } from 'enzyme'; +import { Mock } from 'ts-mockery'; +import createTagsSelector from '../../../src/tags/helpers/TagsSelector'; +import ColorGenerator from '../../../src/utils/services/ColorGenerator'; +import { TagsList } from '../../../src/tags/reducers/tagsList'; + +describe('', () => { + const onChange = jest.fn(); + const TagsSelector = createTagsSelector(Mock.all()); + const tags = [ 'foo', 'bar' ]; + const tagsList = Mock.of({ tags: [ ...tags, 'baz' ] }); + let wrapper: ShallowWrapper; + + beforeEach(jest.clearAllMocks); + beforeEach(() => { + wrapper = shallow( + , + ); + }); + + afterEach(() => wrapper?.unmount()); + + it('has expected props', () => { + expect(wrapper.prop('placeholderText')).toEqual('Add tags to the URL'); + expect(wrapper.prop('allowNew')).toEqual(true); + expect(wrapper.prop('addOnBlur')).toEqual(true); + expect(wrapper.prop('minQueryLength')).toEqual(1); + }); + + it('contains expected tags', () => { + expect(wrapper.prop('tags')).toEqual([ + { + id: 'foo', + name: 'foo', + }, + { + id: 'bar', + name: 'bar', + }, + ]); + }); + + it('contains expected suggestions', () => { + expect(wrapper.prop('suggestions')).toEqual([ + { + id: 'baz', + name: 'baz', + }, + ]); + }); + + it('invokes onChange when new tags are added', () => { + wrapper.simulate('addition', { name: 'The-New-Tag' }); + + expect(onChange).toHaveBeenCalledWith([ ...tags, 'the-new-tag' ]); + }); + + it.each([ + [ 0, 'bar' ], + [ 1, 'foo' ], + ])('invokes onChange when tags are deleted', (index, expected) => { + wrapper.simulate('delete', index); + + expect(onChange).toHaveBeenCalledWith([ expected ]); + }); +});