Forked from abhirathore2006/mocking axios async redux thunk actions using axios-mock-adapter.js
Created
June 24, 2021 02:07
-
-
Save shikelong/eb7ac21b8da86574053f9095a788fff7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import configureMockStore from 'redux-mock-store' | |
import thunk from 'redux-thunk' | |
import { shallow, mount, render } from 'enzyme'; | |
import {Home} from '../src/common/components/home/Home'; | |
import {AxiosApi} from 'some-package' | |
import { Provider } from 'react-redux'; | |
import MockAdapter from 'axios-mock-adapter'; | |
import {testAction} from '../src/common/actions/testAction' | |
declare var describe,it,expect, beforeEach, jasmine | |
/** testAction | |
import {AxiosApi} from 'some-package' | |
export function testAction(){ | |
return (dispatch,getState)=>{ | |
console.log("api") | |
return AxiosApi.get("/someurl",{},'',(result)=>{ | |
console.log("result",result) | |
dispatch({type:"sucess",data:result.data}) | |
},(error)=>{ | |
console.log("error",error) | |
dispatch({type:"fail",data:error}) | |
}) | |
} | |
} | |
**/ | |
const middlewares = [thunk,apiMiddleware] | |
const mockStore = configureMockStore(middlewares) | |
const store = mockStore({}) | |
describe("Foo Test Case Suite", function() { | |
var instance; | |
var mock; | |
beforeEach(function() { | |
//jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; | |
instance = AxiosApi.getAxiosInstance() //Axios.create() | |
mock = new MockAdapter(instance); | |
}); | |
it('allows resolving with Promise', function(done) { | |
console.log("mocking") | |
mock | |
.onGet('/someurl') | |
.reply(function() { | |
return new Promise(function(resolve, reject) { | |
resolve([200, { bar: 'fooPromised' }]); | |
}); | |
}); | |
store.dispatch(testAction()).then(()=>{ | |
console.log("returning in dispatch test") | |
let expectedActions = [{ | |
type: 'sucess', | |
data:{ bar: 'fooPromised' } | |
}] | |
console.log(store.getActions()); | |
expect(store.getActions()).toEqual(expectedActions); | |
}); | |
setTimeout(()=>{ | |
done(); | |
},1000) | |
// instance | |
// .get('/promise') | |
// .then(function(response) { | |
// expect(response.status).to.equal(200); | |
// expect(response.data.bar).to.equal('fooPromised'); | |
// }) | |
// .catch(function() { | |
// expect(true).to.equal(false); | |
// }); | |
}); | |
it("contains html ", function() { | |
expect(mount( | |
<Provider store={store}> | |
<Home | |
dispatch={store.dispatch}/> | |
</Provider>).find('.loader').length).toBe(2); | |
}); | |
it("checks class with Shallow", function() { | |
expect(shallow(<Foo />).is('.foo')).toBe(true); | |
}); | |
it("checks class with Mount -should fail", function() { | |
expect(mount(<Foo />).find('.foo1').length).toBe(1); | |
}); | |
it("checks class with render", function() { | |
expect(render(<Foo />).find('.foo').length).toBe(1); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment