Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abhirathore2006/2bdc5e7e696e39e2cbf8b1800e33ecfc to your computer and use it in GitHub Desktop.
Save abhirathore2006/2bdc5e7e696e39e2cbf8b1800e33ecfc to your computer and use it in GitHub Desktop.
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);
});
});
@algonina
Copy link

hello sir, i have a question,
what is the use redux-mock-store and axios-mock-adapter??

@abhirathore2006
Copy link
Author

both the libraries provide mock wrapper around redux and axios

@chatay
Copy link

chatay commented Mar 5, 2021

at the below stage I send a request to thunk action, all is good at this point but I did not mock the thunk action so it attempts to connect back-end so it fails. Is there any way I can mock the thunk action?

store.dispatch(testAction()).then(()=>{
      console.log("returning in dispatch test")
      let expectedActions = [{
      type: 'sucess',
      data:{ bar: 'fooPromised' }
    }]

@jpsauve-cfx
Copy link

where do I find 'some-package' from which to source the instance? Examples like this are ridiculous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment