diff --git a/sources/js/src/components/entities-toggle-control.tsx b/sources/js/src/components/entities-toggle-control.tsx index 6253bb7..0732570 100644 --- a/sources/js/src/components/entities-toggle-control.tsx +++ b/sources/js/src/components/entities-toggle-control.tsx @@ -5,7 +5,7 @@ import React, { JSX } from 'react'; import { NoOptionsMessage } from './no-options-message'; export function EntitiesToggleControl( - props: EntitiesSearch.EntitiesControl + props: EntitiesSearch.EntitiesControl ): JSX.Element { const className = classnames( props.className, @@ -20,18 +20,12 @@ export function EntitiesToggleControl( const onChange = (event: React.ChangeEvent) => { const { target } = event; - if (!(target instanceof HTMLInputElement)) { + if (target.checked && !props.value?.has(target.value)) { + props.onChange(props.value?.add(target.value)); return; } - const value = Number(target.value); - - if (target.checked && !props.value?.has(value)) { - props.onChange(props.value?.add(value)); - return; - } - - props.onChange(props.value?.delete(value)); + props.onChange(props.value?.delete(target.value)); }; return ( diff --git a/tests/js/unit/components/__snapshots__/entities-toggle-control.test.tsx.snap b/tests/js/unit/components/__snapshots__/entities-toggle-control.test.tsx.snap new file mode 100644 index 0000000..1b79ff8 --- /dev/null +++ b/tests/js/unit/components/__snapshots__/entities-toggle-control.test.tsx.snap @@ -0,0 +1,58 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`EntitiesToggleControl renders correctly 1`] = ` +
+
+ +
+
+ +
+
+ +
+
+`; + +exports[`EntitiesToggleControl renders the NoOptionsMessage when there are no options 1`] = ` +

+ No options +

+`; diff --git a/tests/js/unit/components/entities-toggle-control.test.tsx b/tests/js/unit/components/entities-toggle-control.test.tsx new file mode 100644 index 0000000..59cb5c3 --- /dev/null +++ b/tests/js/unit/components/entities-toggle-control.test.tsx @@ -0,0 +1,74 @@ +import { OrderedSet } from 'immutable'; +import React from 'react'; + +import { describe, expect, it, jest } from '@jest/globals'; + +import { render, screen, fireEvent } from '@testing-library/react'; + +import { EntitiesToggleControl } from '../../../../sources/js/src/components/entities-toggle-control'; + +const options = OrderedSet([ + { label: 'Option 1', value: '1' }, + { label: 'Option 2', value: '2' }, + { label: 'Option 3', value: '3' }, +]); + +describe('EntitiesToggleControl', () => { + it('renders correctly', () => { + const { container } = render( + {}} + /> + ); + + expect(container.firstChild).toMatchSnapshot(); + }); + + it('renders the NoOptionsMessage when there are no options', () => { + const { container } = render( + {}} + /> + ); + + expect(container.firstChild).toMatchSnapshot(); + }); + + it('updates the value when one or more option are selected', () => { + const onChange = jest.fn(); + + render( + + ); + + fireEvent.click(screen.getByLabelText('Option 1')); + + expect(onChange).toHaveBeenCalledWith(OrderedSet(['1'])); + }); + + it('updates the value when one or more option are unselected', () => { + const onChange = jest.fn(); + + render( + + ); + + fireEvent.click(screen.getByLabelText('Option 1')); + + expect(onChange).toHaveBeenCalledWith(OrderedSet(['2'])); + }); +});