From 87086dab17e2117b924b485414c618784acf983a Mon Sep 17 00:00:00 2001 From: Tom Fullalove Date: Wed, 1 Sep 2021 10:47:43 +0100 Subject: [PATCH 1/2] FLOW-3829 - Moved empty date check to only check definite dates Added tests for onCommit of all content types --- ui-bootstrap/__tests__/table-input.test.tsx | 84 ++++++++++++++++++++- ui-bootstrap/js/components/table-input.tsx | 27 +++---- 2 files changed, 97 insertions(+), 14 deletions(-) diff --git a/ui-bootstrap/__tests__/table-input.test.tsx b/ui-bootstrap/__tests__/table-input.test.tsx index e76604df..5300789d 100644 --- a/ui-bootstrap/__tests__/table-input.test.tsx +++ b/ui-bootstrap/__tests__/table-input.test.tsx @@ -16,6 +16,8 @@ describe('Table input component behaviour', () => { const globalAny:any = global; + const mockOnCommitted = jest.fn(); + function manyWhoMount( { contentType = 'string', @@ -31,7 +33,7 @@ describe('Table input component behaviour', () => { flowKey: 'string', contentFormat: 'string', propertyId: 'string', - onCommitted: jest.fn(), + onCommitted: mockOnCommitted, }; globalAny.window.manywho.component['contentTypes'] = { @@ -272,4 +274,84 @@ describe('Table input component behaviour', () => { expect(globalAny.window.manywho.model.setModal).toHaveBeenCalled(); }); + + test('Committing content boolean commits successfully', () => { + // This must be false so that the isDateTimeInput fails + globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + + const value = true; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.boolean, + value, + }); + + tableInputWrapper.instance().onCommit(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + }); + + test('Committing content string commits successfully', () => { + // This must be false so that the isDateTimeInput fails + globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + + const value = 'true'; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.string, + value, + }); + + tableInputWrapper.instance().onCommit(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + }); + + test('Committing content number commits successfully', () => { + // This must be false so that the isDateTimeInput fails + globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + + const value = 37; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.number, + value, + }); + + tableInputWrapper.instance().onCommit(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + }); + + test('Committing content password commits successfully', () => { + // This must be false so that the isDateTimeInput fails + globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + + const value = 'password'; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.password, + value, + }); + + tableInputWrapper.instance().onCommit(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + }); + + test('Committing content datetime commits successfully', () => { + // This must be true so that the isDateTimeInput succeeds + globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + + const value = '02/02/0002'; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.datetime, + value, + }); + + tableInputWrapper.instance().onCommit(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + }); }); diff --git a/ui-bootstrap/js/components/table-input.tsx b/ui-bootstrap/js/components/table-input.tsx index ad5d3088..1275746d 100644 --- a/ui-bootstrap/js/components/table-input.tsx +++ b/ui-bootstrap/js/components/table-input.tsx @@ -140,19 +140,20 @@ class TableInput extends React.Component { true, ); - const isEmptyDate = this.isEmptyDate(this.state.value); - - if (isDateTimeInput && !isEmptyDate) { - const dateTime = moment( - this.state.value, - ['MM/DD/YYYY hh:mm:ss A ZZ', moment.ISO_8601, this.props.contentFormat || ''], - ); - - this.props.onCommitted(this.props.id, this.props.propertyId, dateTime.format()); - manywho.model.setModal(this.props.flowKey, null); - - } else if (isDateTimeInput && isEmptyDate) { - this.renderDateTimeModal('Please select a date'); + if (isDateTimeInput) { + const isEmptyDate = this.isEmptyDate(this.state.value); + + if (isEmptyDate) { + this.renderDateTimeModal('Please select a date'); + } else { + const dateTime = moment( + this.state.value, + ['MM/DD/YYYY hh:mm:ss A ZZ', moment.ISO_8601, this.props.contentFormat || ''], + ); + + this.props.onCommitted(this.props.id, this.props.propertyId, dateTime.format()); + manywho.model.setModal(this.props.flowKey, null); + } } else { this.props.onCommitted(this.props.id, this.props.propertyId, this.state.value); } From bd1b94d1842144b47aee5923a4c6231636c8f74e Mon Sep 17 00:00:00 2001 From: Tom Fullalove Date: Wed, 1 Sep 2021 11:11:28 +0100 Subject: [PATCH 2/2] FLOW-3829 - Fixed issue in table-input tests for dates --- ui-bootstrap/__tests__/table-input.test.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ui-bootstrap/__tests__/table-input.test.tsx b/ui-bootstrap/__tests__/table-input.test.tsx index 5300789d..9b389342 100644 --- a/ui-bootstrap/__tests__/table-input.test.tsx +++ b/ui-bootstrap/__tests__/table-input.test.tsx @@ -6,6 +6,7 @@ import { shallow } from 'enzyme'; import TableInput from '../js/components/table-input'; +import * as moment from 'moment'; describe('Table input component behaviour', () => { @@ -53,6 +54,7 @@ describe('Table input component behaviour', () => { afterEach(() => { tableInputWrapper.unmount(); + mockOnCommitted.mockClear(); }); test('Table input component renders without crashing', () => { @@ -341,7 +343,7 @@ describe('Table input component behaviour', () => { test('Committing content datetime commits successfully', () => { // This must be true so that the isDateTimeInput succeeds - globalAny.window.manywho.utils.isEqual = jest.fn(() => false); + globalAny.window.manywho.utils.isEqual = jest.fn(() => true); const value = '02/02/0002'; @@ -352,6 +354,11 @@ describe('Table input component behaviour', () => { tableInputWrapper.instance().onCommit(); - expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', value); + const expectedDate = moment( + value, + ['MM/DD/YYYY hh:mm:ss A ZZ', moment.ISO_8601, 'string' || ''], + ).format(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', expectedDate); }); });