Today's session will be on more efficient & complex ways of handling data in our application. -> Redux
Small apllication -> Context API is sufficient
When application grows bigger -> Use Redux
Redux -> external library for state management
Concerns with Redux :
- Huge learning curve
- Complex configuration
- Too much boilerplate code
So, Redux Toolkit was introduced
Redux vs Redux-Toolkit
At the end of the day -> Redux is an Object which can be accessed by all components
State variable 's scope -> Only within the component
Only single Redux store -> for the whole app -> with logical separation -> with each store having different slices
cart
user
auth
. . . slices
Today's feature that we are building -> Cart Workflow
Restaurant Menu Click on ADD button -> Item added to Cart ->
Steps :
Writing into the Store
- When we need to make a modification in the store, we must first dispatch an action that calls a reducer function which updates the slice of the store. Eg : Click Add button -> addItem -> reducer -> cart in store
Jargons : Dispatch, action, reducer, slice, store
Reading from the Store 2. When we want to read from the store, we must call a Selector Hook (function) (selecting a slice of store), which updated the cart component.
Cart component is subscribed to the store using the Selector
Eg: Store (Collection of slices) -> Selector -> subscribe -> Cart component
Jargons : Selector, slice, store
Toolkit -> Core of Redux React-redux 0> bridge between redux & React
Install
npm install @reduxjs/toolkit
npm install react-redux
Steps :
- Create store.js file in utils and import configureStore() from rtk
- Create store using configureStore() and export it.
- Import { Provider } from react-redux in root component
- Import store from utils in root component
- Enclose the components that need Store using
- Send props store={store}
- Create cartSlice.js in utils and import createSlice() from rtk
- Create a cartSlice using createSlice with name, initialState (items :[]) and reducers funtion
- reducers funtion contains actions that is dispatched - addItem with state & action params
- state contains the states of the slice and action
- modifications are done to state inside action of reducers -> addItem, removeItem, cLearCart
- export the reducer cartSlice.reducer (default export)
- export cartSlice.actions with all the action functions (destructured named exporte)
- import the cartSlice into the store
reducers is the function name -> but exported as reducer -> confusing
How createSlice is stored in background ?
cartSlice = { actions : { addItem, removeItem, clearCart }, reducer : reducers }
useSelector -> what it is subscribing to
chapter - 13
How to test react applications ? Jest & React Testing Library (alternative to enzyme)
How to setup test for our app ?
Why test cases ?
- New code should not old code
- Not breaking existing code - Maintainability
TDD - test Driven Development ? Test cases even before writing code, development becomes very slow.
Types of tesing :
-
Manual testing -> human testing the code
-
Automated testing -> code testing the code Eg: Selenium
-
E2E tetsing -> test the whole flow by stimulating . Eg:cypress This is offloaded to QA team. Headless browser -> browser does not have to do teh work of laoding UI in browser. It makes testing faster.
-
Unit testing -> core job of developers -> testing small unit of code
-
Integration testing -> Data flow between components
-
Performance, Regression and Smoke testing
React-Testing-library
- Install React Testing library
- Install Jest
- Configure Jest -> npx jest --init
- Typescript -> N
- environment -> jsdom (broswer-like)
- code coverage -> y
- provider for coverage -> babel
- automatically clear before test -> y
- Creates jest.config.js
- scripts -> test : jest
- npm install -D test-environment-jsdom
- jest is trying to find test cases in the app under tests folder
- Create first test -> sum.test.js under tests folder
- test("testcase name ", () => { })
- Every test case should have some assertion -
expect
to return -toBe
expeect(sum(2,3)).toBe(5) - import sum.js (component) inside sum.test.js
- install babel-jest @babel/core @babel/preset-env
- to configure babel -> babelrc file or babel.config.js