diff --git a/ui-bootstrap/__tests__/table-input.test.tsx b/ui-bootstrap/__tests__/table-input.test.tsx index e76604df..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', () => { @@ -16,6 +17,8 @@ describe('Table input component behaviour', () => { const globalAny:any = global; + const mockOnCommitted = jest.fn(); + function manyWhoMount( { contentType = 'string', @@ -31,7 +34,7 @@ describe('Table input component behaviour', () => { flowKey: 'string', contentFormat: 'string', propertyId: 'string', - onCommitted: jest.fn(), + onCommitted: mockOnCommitted, }; globalAny.window.manywho.component['contentTypes'] = { @@ -51,6 +54,7 @@ describe('Table input component behaviour', () => { afterEach(() => { tableInputWrapper.unmount(); + mockOnCommitted.mockClear(); }); test('Table input component renders without crashing', () => { @@ -272,4 +276,89 @@ 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(() => true); + + const value = '02/02/0002'; + + tableInputWrapper = manyWhoMount({ + contentType: globalAny.window.manywho.component.contentTypes.datetime, + value, + }); + + tableInputWrapper.instance().onCommit(); + + const expectedDate = moment( + value, + ['MM/DD/YYYY hh:mm:ss A ZZ', moment.ISO_8601, 'string' || ''], + ).format(); + + expect(mockOnCommitted).toHaveBeenCalledWith('string', 'string', expectedDate); + }); }); 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); }