Skip to content

Commit 4550f99

Browse files
author
vvo
committed
fix(search-box): update value when state changes from the outside
we were previously using the previous state I believe. This bug can be seen by hitting the back button while not being focused.
1 parent 6112d52 commit 4550f99

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/widgets/search-box/__tests__/search-box-test.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sinon from 'sinon';
55
import jsdom from 'mocha-jsdom';
66

77
import searchBox from '../search-box';
8+
import EventEmitter from 'events';
89

910
import expectJSX from 'expect-jsx';
1011
expect.extend(expectJSX);
@@ -31,12 +32,12 @@ describe('search-box()', () => {
3132
query: ''
3233
};
3334
helper = {
34-
on: sinon.spy(),
3535
setQuery: sinon.spy(),
3636
search: sinon.spy(),
3737
state: {
3838
query: ''
39-
}
39+
},
40+
...EventEmitter.prototype
4041
};
4142
});
4243

@@ -272,6 +273,17 @@ describe('search-box()', () => {
272273
});
273274
});
274275

276+
it('updates the input on an helper update', () => {
277+
container = document.createElement('div');
278+
widget = searchBox({container});
279+
widget.init({state, helper});
280+
let input = container.querySelector('input');
281+
expect(input.value).toBe('');
282+
input.blur();
283+
helper.emit('change', {query: 'iphone'});
284+
expect(input.value).toBe('iphone');
285+
});
286+
275287
context('focus', () => {
276288
let input;
277289
beforeEach(() => {

src/widgets/search-box/search-box.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function searchBox({
162162
// Update value when query change outside of the input
163163
helper.on('change', function(newState) {
164164
if (input !== document.activeElement && input.value !== newState.query) {
165-
input.value = state.query;
165+
input.value = newState.query;
166166
}
167167
});
168168

0 commit comments

Comments
 (0)