Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
redux-testkit
Advanced tools
##Testkit for redux actions (thunks or otherwise)
Install this module via npm with npm install redux-testkit --save-dev
Use this module to easily write unit tests for redux actions, including asynchronous actions using redux-thunk middleware.
To import the module in your test file, use
import {MockStore} from 'redux-testkit';
MockStore provides these methods:
Simply resets the store, usually you would use this in beforeEach
or equivalent in your test suite, for example
beforeEach(() => {
mockStore.reset();
});
This sets the redux store state that will be provided via getState
to thunks dispatched to the mockStore. Use this to set up a test that depends on an existing state.
Important note: This object is NOT affected by dispatches, or internal dispatches called in tests. This shouldn't cause any problems because thinks should not rely on the reducer logic but should only 'fire and forget' events to the store
This is the key method for running your tests. Say your thunk is:
export function actionToTest(parameters) {
return async function(dispatch, getState) {
//Asynchronous logic with lots of awaits here
}
}
then you send this to the mockStore with mockStore.dispatchSync(actionToTest(paramObjects));
.
The testkit will run this test synchronously and then you can run expect
asertations on the output with:
This is where you do the work in the tests. To unit test an action, you want to test what effect the action has given a specific starting environment. We set up this environment before the test with setState()
and by passing parameters. There are three ways a dispatched action can cause effects:
type
field to the storeIn case 3, you test the effect by mocking the external function. Typically you would extract that logic to a separate class and import it into your action's class, and so you mock it by using a tool like proxyquire
when importing you actions into the test suite.
redux-testkit allows you to unit test cases 1 and 2.
getActions()
returns an array
of all the dispatches sent by the tested action, in order. In case 1, the entire object is saved and you can expect
it to have a type and other fields, for example:
expect(mockStore.getActions()[0].type).toEqual(actionTypes.ACTION_TYPE_1);
expect(mockStore.getActions()[0].otherField).toEqual({some object});
In case 2, the name
of the dispatched function is saved, and can be tested like this
expect(mockStore.getActions()[1]).toEqual('name_of_function');
If this is another thunk, then you must name the internal anonymous async function, like this:
export function name_of_function() {
return async function name_of_function(dispatch, getState) {
}
}
To test a synchronous action that dispacthes other actions or objects, you should inject the mockDispatch()
and getState()
from the mockStore. For example:
const result = actions.syncAction(mockStore.mockDispatch, mockStore.getState(), params...);
expect(result).toEqual(123456);
expect(mockStore.getActions()).to....
[ ] Improve syntax with Matchers - Please open issues to suggest the syntax you'd want!
FAQs
Complete and opinionated testkit for testing Redux projects (reducers, selectors, actions, thunks)
We found that redux-testkit demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.