Skip to content

Instantly share code, notes, and snippets.

@Gbaja
Last active August 10, 2018 16:24
Show Gist options
  • Save Gbaja/1627668f3f0273d214007aef80367b67 to your computer and use it in GitHub Desktop.
Save Gbaja/1627668f3f0273d214007aef80367b67 to your computer and use it in GitHub Desktop.
Code snippet for testing logout-test.js as needed in my Introduction to testing your redux blog code
import { mockStore, mockAxios } from "../test-support";
//importing our helper variables previously defined in setup stage.
describe("Logout action", () => {
it("Returns the correct action to log out a user", () => {
const mockLogout = () => mockAxios.onGet("/api/logout").reply(200);
//I am creating a mock log out function using the mockAxios variable which by default has access to methods built in to the module.
//I am using the onGet method to call our api. Notice how the string I pass in, is the same as the string I pass in to my axios.get method in my real logout function. It is important that it is the same.
//The reply method is checking to make sure my request is going through hence why I am passing in the status code 200.
const store = mockStore();
//creating a variable to call the mockStore function previously defined.
mockLogout();
//calling the callback function. We need to call it to make sure that the request is actually been sent.
const expectedAction = {
type: LOGOUT_USER,
payload: false
};
store.dispatch(logout()).then(() => {
expect(store.getActions()).toEqual([expectedAction]);
});
//My original logout function, returns a promise so I am dispatching the logout function and then comparing the action I got from the store to my expected outcome.
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment