Skip to content

Commit

Permalink
[Fix] Fixed handling of typeahead reset click.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Mar 23, 2017
1 parent ee6f3f7 commit 5f0a398
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/components/Answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export default class Answer extends React.Component {
};

_setValue(change, value) {
if (this.props.answer[Constants.HAS_OBJECT_VALUE] || FormUtils.isTypeahead(this.props.question)) {
if (value === null) {
change[Constants.HAS_OBJECT_VALUE] = null;
change[Constants.HAS_DATA_VALUE] = null;
} else if (this.props.answer[Constants.HAS_OBJECT_VALUE] || FormUtils.isTypeahead(this.props.question)) {
change[Constants.HAS_OBJECT_VALUE] = {
'@id': value
};
Expand Down
8 changes: 4 additions & 4 deletions src/components/answer/TypeaheadAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class TypeaheadAnswer extends React.Component {
}

componentWillMount() {
var question = this.props.question;
const question = this.props.question;
if (!question[Constants.HAS_OPTION] && FormUtils.getPossibleValuesQuery(question)) {
Configuration.actions.loadFormOptions(this._queryHash, FormUtils.getPossibleValuesQuery(question));
} else {
Expand All @@ -49,7 +49,7 @@ export default class TypeaheadAnswer extends React.Component {
return;
}
options = JsonLdUtils.processTypeaheadOptions(options);
var value = FormUtils.resolveValue(this.props.answer),
const value = FormUtils.resolveValue(this.props.answer),
selected = options.find((item) => {
return item.id === value;
});
Expand All @@ -60,11 +60,11 @@ export default class TypeaheadAnswer extends React.Component {
};

_onOptionSelected = (option) => {
this.props.onChange(option.id);
this.props.onChange(option ? option.id : null);
};

render() {
var value = Utils.idToName(this.state.options, this.props.value),
const value = Utils.idToName(this.state.options, this.props.value),
question = this.props.question,
inputProps = {
disabled: FormUtils.isDisabled(question)
Expand Down
57 changes: 57 additions & 0 deletions test/__tests__/TypeaheadAnswerTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

import React from "react";
import JsonLdUtils from "jsonld-utils";
import TestUtils from "react-addons-test-utils";

import Environment from "../environment/Environment";
import Generator from "../environment/Generator";

import Configuration from "../../src/model/Configuration";
import Constants from "../../src/constants/Constants";
import TypeaheadAnswer from "../../src/components/answer/TypeaheadAnswer";

describe('TypeaheadAnswer', () => {

let question,
onChange,
optionsStore,
actions;

beforeEach(() => {
question = {
"@id": Generator.getRandomUri()
};
question[Constants.LAYOUT_CLASS] = [];
question[JsonLdUtils.RDFS_LABEL] = {
"@language": "en",
"@value": "1 - Aerodrome General"
};
question[JsonLdUtils.RDFS_COMMENT] = {
"@language": "en",
"@value": "The identification of the aerodrome/helicopter landing area by name, location and status."
};
onChange = jasmine.createSpy('onChange');
Configuration.intl = {
locale: 'en'
};
optionsStore = jasmine.createSpyObj('OptionsStore', ['listen', 'getOptions']);
optionsStore.getOptions.and.returnValue([{
id: '123',
name: '123'
}]);
Configuration.optionsStore = optionsStore;
actions = jasmine.createSpyObj('Actions', ['loadFormOptions']);
Configuration.actions = actions;
});

it('passes null to onChange when value is reset', () => {
question[Constants.LAYOUT_CLASS].push(Constants.LAYOUT.QUESTION_TYPEAHEAD);
const component = Environment.render(<TypeaheadAnswer answer={{}} question={question} label="Test" value="123"
onChange={onChange}/>);

const resetButton = TestUtils.scryRenderedDOMComponentsWithTag(component, 'button')[0];
TestUtils.Simulate.click(resetButton);
expect(onChange).toHaveBeenCalledWith(null);
});
});

0 comments on commit 5f0a398

Please sign in to comment.