diff --git a/webapp/src/components/content/__snapshots__/checkboxElement.test.tsx.snap b/webapp/src/components/content/__snapshots__/checkboxElement.test.tsx.snap new file mode 100644 index 000000000..0cfa0df2e --- /dev/null +++ b/webapp/src/components/content/__snapshots__/checkboxElement.test.tsx.snap @@ -0,0 +1,86 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`components/content/CheckboxElement should match snapshot 1`] = ` +
+
+ +
+ test-title +
+
+
+`; + +exports[`components/content/CheckboxElement should match snapshot on change title 1`] = ` +
+
+ +
+ changed name +
+
+
+`; + +exports[`components/content/CheckboxElement should match snapshot on read only 1`] = ` +
+
+ +
+ test-title +
+
+
+`; + +exports[`components/content/CheckboxElement should match snapshot on toggle 1`] = ` +
+
+ +
+ test-title +
+
+
+`; diff --git a/webapp/src/components/content/checkboxElement.test.tsx b/webapp/src/components/content/checkboxElement.test.tsx new file mode 100644 index 000000000..ae1ce2bb1 --- /dev/null +++ b/webapp/src/components/content/checkboxElement.test.tsx @@ -0,0 +1,77 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import React from 'react' +import {fireEvent, render} from '@testing-library/react' +import '@testing-library/jest-dom' +import {IntlProvider} from 'react-intl' + +import {IContentBlock} from '../../blocks/contentBlock' + +import CheckboxElement from './checkboxElement' + +const wrapIntl = (children: any) => {children} + +describe('components/content/CheckboxElement', () => { + const defaultBlock: IContentBlock = { + id: 'test-id', + parentId: '', + rootId: '', + modifiedBy: 'test-user-id', + schema: 0, + type: 'checkbox', + title: 'test-title', + fields: {}, + createAt: 0, + updateAt: 0, + deleteAt: 0, + } + + test('should match snapshot', () => { + const component = wrapIntl( + , + ) + const {container} = render(component) + expect(container).toMatchSnapshot() + }) + + test('should match snapshot on read only', () => { + const component = wrapIntl( + , + ) + const {container} = render(component) + expect(container).toMatchSnapshot() + }) + + test('should match snapshot on change title', () => { + const component = wrapIntl( + , + ) + const {container, getByText} = render(component) + const input = getByText(/test-title/i) + fireEvent.blur(input, {target: {textContent: 'changed name'}}) + expect(container).toMatchSnapshot() + }) + + test('should match snapshot on toggle', () => { + const component = wrapIntl( + , + ) + const {container, getByRole} = render(component) + const input = getByRole('checkbox') + fireEvent.change(input, {target: {value: 'on'}}) + expect(container).toMatchSnapshot() + }) +}) diff --git a/webapp/src/components/content/checkboxElement.tsx b/webapp/src/components/content/checkboxElement.tsx index cc00bcf8b..5ea9b447e 100644 --- a/webapp/src/components/content/checkboxElement.tsx +++ b/webapp/src/components/content/checkboxElement.tsx @@ -26,7 +26,7 @@ type State = { class CheckboxElement extends React.PureComponent { constructor(props: Props) { super(props) - this.state = {active: props.block.fields?.value, title: props.block.title} + this.state = {active: Boolean(props.block.fields.value), title: props.block.title} } render(): JSX.Element { @@ -39,13 +39,11 @@ class CheckboxElement extends React.PureComponent { id={`checkbox-${block.id}`} disabled={readonly} checked={this.state.active} - onChange={() => { + value={this.state.active ? 'on' : 'off'} + onChange={(e) => { + e.preventDefault() const newBlock = new MutableCheckboxBlock(block) - if (newBlock.fields) { - newBlock.fields.value = !this.state.active - } else { - newBlock.fields = {value: !this.state.active} - } + newBlock.fields.value = !this.state.active newBlock.title = this.state.title this.setState({active: newBlock.fields.value}) mutator.updateBlock(newBlock, block, intl.formatMessage({id: 'ContentBlock.editCardCheckbox', defaultMessage: 'toggled-checkbox'})) @@ -58,11 +56,7 @@ class CheckboxElement extends React.PureComponent { this.setState({title: text}) const newBlock = new MutableCheckboxBlock(block) newBlock.title = text - if (newBlock.fields) { - newBlock.fields.value = this.state.active - } else { - newBlock.fields = {value: this.state.active} - } + newBlock.fields.value = this.state.active mutator.updateBlock(newBlock, block, intl.formatMessage({id: 'ContentBlock.editCardCheckboxText', defaultMessage: 'edit card text'})) }} readonly={readonly}