Enzyme Asynchronization Reaction and Waiting

I use mocha, an enzyme and expect my testing. I have a function in my component that runs another function that returns a promise, and I don’t know how to check the behavior of the first before it runs the second function and then check the promise (get the error after .then)

1st function:

handleUpdateInput (value) { const { access, onUpdateInput } = this.props const v = !value || typeof value === 'string' ? value : access(value) if (onUpdateInput) { onUpdateInput(value ? v : '') } this.setState({ searchText: value }) this.dataSourceUpdate(value) } 

Second function:

 dataSourceUpdate (value) { const { promise, access } = this.props if (value === '') { this.autoCompleteData = [] this.setState({ dataSource: [] }) } else { promise(value) .then(res => { this.autoCompleteData = res.data this.setState({ dataSource: this.autoCompleteData.map(access).slice(0, getMenuItemNumber(this.refs.customAutoComplete)) }) }) .catch(() => { this.autoCompleteData = [] this.setState({ dataSource: [] }) }) } } 

I would also like to send me a good tutorial for testing asynchronous functions using these tools :)

+5
source share
1 answer

Since you receive a promise from props (think carefully), you can make fun of your promise with the help of sinon.

For example, you can try:

 var Promise = require('bluebird'); // you can use any Promise module here var deferred = Promise.defer(); stub = sinon.stub(deferred, 'resolve').returns(deferred.promise); deferred.resolve({res: { data: 'YOUR_DATA' }}); // or deferred.reject(new Error('fake error')); 
0
source

Source: https://habr.com/ru/post/1260689/


All Articles