Skip to content

Commit

Permalink
fix(async): create new cache object for each instance by default
Browse files Browse the repository at this point in the history
fix JedWatson#1236
Previously, the same object was being used by for async cache by
default when no cache was passed to the component. This same object
was used for every instance.
Now, an object is used to determine if the default is being used
and will create a new object for each instance in which no prop
was passed.
  • Loading branch information
TheSharpieOne authored and Bala Anirudh Kurakula committed Nov 1, 2016
1 parent e8ff62d commit 6cd04c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const propTypes = {
]),
};

const defaultCache = {};

const defaultProps = {
autoload: true,
cache: {},
cache: defaultCache,
children: defaultChildren,
ignoreAccents: true,
ignoreCase: true,
Expand All @@ -44,6 +46,10 @@ export default class Async extends Component {
this._onInputChange = this._onInputChange.bind(this);
}

componentWillMount () {
this.cache = this.props.cache === defaultCache ? {} : this.props.cache;
}

componentDidMount () {
const { autoload } = this.props;

Expand All @@ -64,7 +70,8 @@ export default class Async extends Component {
}

loadOptions (inputValue) {
const { cache, loadOptions } = this.props;
const { loadOptions } = this.props;
const cache = this.cache;

if (
cache &&
Expand Down
8 changes: 8 additions & 0 deletions test/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ describe('Async', () => {
typeSearchText('a');
return expect(loadOptions, 'was called times', 1);
});

it('should not use the same cache for every instance by default', () => {
createControl();
const instance1 = asyncInstance;
createControl();
const instance2 = asyncInstance;
expect(instance1.cache !== instance2.cache, 'to equal', true);
})
});

describe('loadOptions', () => {
Expand Down

0 comments on commit 6cd04c8

Please sign in to comment.