From 83134841a27513ba633d5f4b9ec71b76d205d6f0 Mon Sep 17 00:00:00 2001 From: GotPPay Date: Thu, 5 Apr 2018 15:40:10 +0200 Subject: [PATCH] add test for question delete; fix bug with delete question --- web/src/components/IntentDetails.js | 8 ++-- .../__tests__/IntentDetails.test.js | 42 +++++++++++++++---- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/web/src/components/IntentDetails.js b/web/src/components/IntentDetails.js index 6be2bf3..ab24450 100644 --- a/web/src/components/IntentDetails.js +++ b/web/src/components/IntentDetails.js @@ -72,7 +72,7 @@ class IntentDetails extends Component { rightIcon={ { - this.deleteQuestion (question); + this.deleteQuestion (index); }} > {' '} @@ -146,12 +146,10 @@ class IntentDetails extends Component { this.setState ({intent: newIntent}); } - deleteQuestion (question) { + deleteQuestion (index) { if (this.state.intent.questions.length > 1) { let newIntent = this.state.intent; - let removeId = newIntent.questions.indexOf (question); - if (removeId !== -1) newIntent.questions.splice (removeId, 1); - + if (index >= 0 && index < newIntent.questions.length) newIntent.questions.splice (index, 1); this.setState ({intent: newIntent}); } } diff --git a/web/src/components/__tests__/IntentDetails.test.js b/web/src/components/__tests__/IntentDetails.test.js index 09c79c4..0c38ea7 100644 --- a/web/src/components/__tests__/IntentDetails.test.js +++ b/web/src/components/__tests__/IntentDetails.test.js @@ -99,17 +99,45 @@ describe('complete testing', () => { }); - xit ('removes correct text field when delete button on text field is clicked', () => { + it ('removes correct text field when delete button on text field is clicked', () => { const addButton = wrapper.find('button').at(0); addButton.simulate('click'); addButton.simulate('click'); - const firstQuestionTextField = wrapper.find('TextField').at(2); - const secondQuestionTextField = wrapper.find('TextField').at(3); - const thirdQuestionTextField = wrapper.find('TextField').at(4); + + let firstQuestionTextField = wrapper.find('TextField').at(2); + let secondQuestionTextField = wrapper.find('TextField').at(3); + let thirdQuestionTextField = wrapper.find('TextField').at(4); + + firstQuestionTextField.instance().props.onChange('first question'); + secondQuestionTextField.instance().props.onChange('second question'); + thirdQuestionTextField.instance().props.onChange('third question'); + + expect(firstQuestionTextField.instance().value).toEqual('first question'); + expect(secondQuestionTextField.instance().value).toEqual('second question'); + expect(thirdQuestionTextField.instance().value).toEqual('third question'); + expect(wrapper.state('intent').questions.length).toBe(3); + + const rightIcon = secondQuestionTextField.props().rightIcon; + rightIcon.props.onClick(secondQuestionTextField.props().key); + + expect(wrapper.state('intent').questions.length).toBe(2); + expect(secondQuestionTextField.instance().value).toEqual('third question'); + expect(thirdQuestionTextField.instance()._field._field).toBeNull(); + }); + + it ('does not remove text field when it is only one left', () => { + let firstQuestionTextField = wrapper.find('TextField').at(2); + + firstQuestionTextField.instance().props.onChange('first question'); + expect(firstQuestionTextField.props().id).toEqual('intent question'); - expect(secondQuestionTextField.props().id).toEqual('intent question'); - expect(thirdQuestionTextField.props().id).toEqual('intent question'); - //TODO: simulate click on delete icon inside text field and check if text field is removed + expect(wrapper.state('intent').questions.length).toBe(1); + + const rightIcon = firstQuestionTextField.props().rightIcon; + rightIcon.props.onClick(firstQuestionTextField.props().key); + + expect(wrapper.state('intent').questions.length).toBe(1); + expect(firstQuestionTextField.instance().value).toEqual('first question'); }); it ('accepts text without special characters for intent explanation', () => {