From 5781c55b1be0532f3028b912f544e8296c4d85db Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Fri, 19 Oct 2018 12:42:50 -0500 Subject: [PATCH] Add Modal tests --- .../src/components/common/Modal.spec.tsx | 24 ++++++++++++-- javascripts/src/components/common/Modal.tsx | 14 +++++--- .../common/__snapshots__/Modal.spec.tsx.snap | 32 +++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 javascripts/src/components/common/__snapshots__/Modal.spec.tsx.snap diff --git a/javascripts/src/components/common/Modal.spec.tsx b/javascripts/src/components/common/Modal.spec.tsx index 396d3f36..dcad6ee5 100644 --- a/javascripts/src/components/common/Modal.spec.tsx +++ b/javascripts/src/components/common/Modal.spec.tsx @@ -1,10 +1,28 @@ // License: LGPL-3.0-or-later import * as React from 'react'; import 'jest'; -import Modal from './Modal' +import Modal, {ModalProps} from './Modal' +import {shallow} from "enzyme"; +import {toJS} from "mobx"; +import toJson from "enzyme-to-json"; describe('Modal', () => { - test('your test here', () => { - expect(false).toBe(true) + test('nothing displayed if inactive', () => { + let modal = shallow(
}/>) + + expect(toJson(modal)).toMatchSnapshot() + }) + + test('active modal displays', () => { + let onCloseWasCalled = false + let modal = shallow( { onCloseWasCalled = true}} + childGenerator={() =>
}/>) + expect(toJson(modal)).toMatchSnapshot() + let modalComponent = modal.instance() as React.Component //casting to modal didn't work for reasons? + modalComponent.props.onClose() + expect(onCloseWasCalled).toBeTruthy() }) }) \ No newline at end of file diff --git a/javascripts/src/components/common/Modal.tsx b/javascripts/src/components/common/Modal.tsx index 194550b5..0d3fd2a5 100644 --- a/javascripts/src/components/common/Modal.tsx +++ b/javascripts/src/components/common/Modal.tsx @@ -1,12 +1,11 @@ // License: LGPL-3.0-or-later import * as React from 'react'; import { observer } from 'mobx-react'; -import {InjectedIntlProps, injectIntl} from 'react-intl'; import AriaModal = require('react-aria-modal'); export interface ModalProps { - onClose?: () => void + onClose?: () => void // if you want your modal to close, this needs to set modalActive to false modalActive?: boolean titleText?: string focusDialog?:boolean @@ -14,11 +13,16 @@ export interface ModalProps childGenerator:() => JSX.Element } -class Modal extends React.Component { +class Modal extends React.Component { + + static defaultProps = { + dialogStyle: {minWidth:'768px'} + } + render() { const modal = this.props.modalActive ? + onExit={this.props.onClose} dialogStyle={this.props.dialogStyle}>

{this.props.titleText}

@@ -31,7 +35,7 @@ class Modal extends React.Component { } } -export default injectIntl(observer(Modal)) +export default observer(Modal) diff --git a/javascripts/src/components/common/__snapshots__/Modal.spec.tsx.snap b/javascripts/src/components/common/__snapshots__/Modal.spec.tsx.snap new file mode 100644 index 00000000..bf79ae7c --- /dev/null +++ b/javascripts/src/components/common/__snapshots__/Modal.spec.tsx.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Modal active modal displays 1`] = ` + +
+

+ title text +

+
+
+
+
+ +`; + +exports[`Modal nothing displayed if inactive 1`] = `""`;