created tests for components related to importing from source

This commit is contained in:
GotPPay
2018-04-03 23:45:53 +02:00
parent f64155e061
commit ecb06fd4e2
9 changed files with 2842 additions and 141 deletions

2729
web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,9 @@
"react-md": "^1.2.8",
"react-popup": "^0.9.1",
"react-scripts": "1.0.17",
"webfontloader": "^1.6.28"
"webfontloader": "^1.6.28",
"node-sass": "^4.7.2",
"npm-run-all": "^4.1.2"
},
"scripts": {
"build-css": "node-sass-chokidar --include-path ./node_modules src/ -o src/",
@@ -24,8 +26,12 @@
"eject": "react-scripts eject"
},
"devDependencies": {
"node-sass": "^4.7.2",
"babel-jest": "^22.4.3",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jest": "^20.0.3",
"jest-enzyme": "^6.0.0",
"nodemon": "^1.12.1",
"npm-run-all": "^4.1.2"
"react-test-renderer": "^16.3.0"
}
}

View File

@@ -1,8 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';
import App from './App';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
shallow(<App />);
});

View File

@@ -0,0 +1,52 @@
import React from 'react';
import {Button} from 'react-md';
import {shallow, mount} from 'enzyme';
import AnswerSource from '../AnswerSource';
import {ANSWER_TYPE} from '../../config/constants'
it ('renders without crashing', () => {
shallow (<AnswerSource />);
});
describe('functional tests', ()=>{
let wrapper;
beforeEach(()=>{
const onSaveAnswerTypeFunction = jest.fn();
wrapper = mount(<AnswerSource onSaveAnswerType={onSaveAnswerTypeFunction} />);
wrapper.setState({
isModalOpen: false,
answerType: 0
});
});
it ('renders only a button', () => {
expect(wrapper.first().text()).toEqual('Answer type');
expect(wrapper.find('AnswerSourceForm').exists()).toEqual(false);
});
it('answer type button click opens modal form', () => {
expect(wrapper.find('AnswerSourceFormA').exists()).toEqual(false);
expect(wrapper.state().isModalOpen).toEqual(false);
const AnswerTypeButton = wrapper.find('button').first();
AnswerTypeButton.simulate('click');
expect(wrapper.state().isModalOpen).toEqual(true);
expect(wrapper.find('AnswerSourceForm').exists()).toEqual(true);
expect(wrapper.find('button').length).toBe(3);
expect(wrapper.find('button').first().text()).toEqual('Answer type');
});
it('save button changes answerType value in state and closes the form ', () => {
const AnswerTypeButton = wrapper.find('button').first();
AnswerTypeButton.simulate('click');
const saveButton = wrapper.find('button').at(2);
const optionControl = wrapper.find('SelectionControlGroup');
expect(saveButton.text()).toEqual('Save');
expect(optionControl.exists()).toEqual(true);
optionControl.simulate('change',{target:{value:String(ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS)}});
saveButton.simulate('click');
expect(wrapper.state().answerType).toBe(ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS)
expect(wrapper.state().isModalOpen).toEqual(false);
expect(wrapper.find('button').length).toBe(1);
});
});

View File

@@ -0,0 +1,10 @@
import React from 'react';
import { shallow } from 'enzyme';
import IntentDetails from '../IntentDetails';
it('renders without crashing', () => {
let dummyIntent = {
questions:['q1','q2']
}
shallow(<IntentDetails selectedIntent={dummyIntent} />);
});

View File

@@ -0,0 +1,71 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import AnswerSourceForm from '../AnswerSourceForm';
import {ANSWER_TYPE} from '../../../config/constants'
it('renders without crashing', () => {
shallow(<AnswerSourceForm onClose={()=>{}} onSave={()=>{}} onSourceChange={()=>{}} />);
});
it('calls onClose when cancel is pressed', () => {
const onClose = jest.fn();
const wrapper = mount(<AnswerSourceForm onClose={onClose} onSave={()=>{}} onSourceChange={()=>{}} />);
const cancelButton = wrapper.find('button').at(0);
expect(cancelButton.text()).toEqual('Cancel');
cancelButton.simulate('click');
expect(onClose).toBeCalled();
});
it('calls onSave when save is pressed', () => {
const onSave = jest.fn();
const wrapper = mount(<AnswerSourceForm onClose={()=>{}} onSave={onSave} onSourceChange={()=>{}} />);
const saveButton = wrapper.find('button').at(1);
expect(saveButton.text()).toEqual('Save');
saveButton.simulate('click');
expect(onSave).toBeCalled();
});
it('sets PREDEFINED value when Predefined answer is selected', () => {
let selectedValue = null;
const wrapper = mount(<AnswerSourceForm
onClose={()=>{}}
onSave={()=>{}}
onSourceChange={(value)=>{selectedValue=value}} />);
const optionControl = wrapper.find('SelectionControlGroup');
expect(optionControl.exists()).toEqual(true);
optionControl.simulate('change',{target:{value:String(ANSWER_TYPE.PREDEFINED)}});
expect(selectedValue).toBe(String(ANSWER_TYPE.PREDEFINED));
});
it('sets EXTERNAL_SOURCE_WP_TITLES value when WordPress titles is selected', () => {
let selectedValue = null;
const wrapper = mount(<AnswerSourceForm
onClose={()=>{}}
onSave={()=>{}}
onSourceChange={(value)=>{selectedValue=value}} />);
const optionControl = wrapper.find('SelectionControlGroup');
expect(optionControl.exists()).toEqual(true);
optionControl.simulate('change',{target:{value:String(ANSWER_TYPE.EXTERNAL_SOURCE_WP_TITLES)}});
expect(selectedValue).toBe(String(ANSWER_TYPE.EXTERNAL_SOURCE_WP_TITLES));
});
it('sets EXTERNAL_SOURCE_WP_NEWS value when WordPress latest news is selected', () => {
let selectedValue = null;
const wrapper = mount(<AnswerSourceForm
onClose={()=>{}}
onSave={()=>{}}
onSourceChange={(value)=>{selectedValue=value}} />);
const optionControl = wrapper.find('SelectionControlGroup');
expect(optionControl.exists()).toEqual(true);
optionControl.simulate('change',{target:{value:String(ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS)}});
expect(selectedValue).toBe(String(ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS));
});

View File

@@ -0,0 +1,81 @@
import React from 'react';
import {shallow, mount} from 'enzyme';
import AnswerTextBox from '../AnswerTextBox';
import {ANSWER_TYPE} from '../../../config/constants';
it ('renders without crashing', () => {
shallow (<AnswerTextBox />);
});
describe ('predefined answer selected', () => {
let wrapper;
let textField;
beforeEach (() => {
const onChange = jest.fn();
wrapper = mount (
<AnswerTextBox
answerType={ANSWER_TYPE.PREDEFINED}
answer={'Dummy answer'}
handleAnswerEdit={onChange}
/>
);
textField = wrapper.find ('TextField').first ();
});
it ('renders text box for normal answer', () => {
expect (textField.props ().label).toEqual ('Answer');
});
it ('receives valid answer text', () => {
expect (textField.props ().value).toEqual ('Dummy answer');
});
});
describe ('WordPress titles selected', () => {
let wrapper;
let textField;
beforeEach (() => {
const onChange = jest.fn();
wrapper = mount (
<AnswerTextBox
answerType={ANSWER_TYPE.EXTERNAL_SOURCE_WP_TITLES}
externalAnswerSource={'Dummy answer'}
handleAnswerSourceEdit={onChange}
/>
);
textField = wrapper.find ('TextField').first ();
});
it ('renders text box for external source input', () => {
expect (textField.props ().label).toEqual ('Answer source');
});
it ('receives valid answer text', () => {
expect (textField.props ().value).toEqual ('Dummy answer');
});
});
describe ('WordPress latest news selected', () => {
let wrapper;
let textField;
beforeEach (() => {
const onChange = jest.fn();
wrapper = mount (
<AnswerTextBox
answerType={ANSWER_TYPE.EXTERNAL_SOURCE_WP_NEWS}
externalAnswerSource={'Dummy answer'}
handleAnswerSourceEdit={onChange}
/>
);
textField = wrapper.find ('TextField').first ();
});
it ('renders text box for external source input', () => {
expect (textField.props ().label).toEqual ('Answer source');
});
it ('receives valid answer text', () => {
expect (textField.props ().value).toEqual ('Dummy answer');
});
});

View File

@@ -0,0 +1,16 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import Modal from '../Modal';
it('renders without crashing', () => {
shallow(<Modal />);
});
it('receives props as expected', () =>{
const actionButton = <button key={0}>Dummy action button</button>;
const childButton = <button key={1}>Child button</button>;
const wrapper = mount(<Modal title={'Dummy title'} actions={[actionButton]}>{childButton}</Modal>);
expect(wrapper.props().title).toEqual('Dummy title');
expect(wrapper.props().actions).toEqual([actionButton]);
expect(wrapper.props().children).toEqual(childButton);
});

5
web/src/setupTests.js Normal file
View File

@@ -0,0 +1,5 @@
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import 'jest-enzyme';
configure({ adapter: new Adapter() });