How do I test axios in Jest?

Without using any other libraries: import * as axios from “axios”; // Mock out all top level functions, such as get, put, delete and post: jest.mock(“axios”); // … test(“good response”, () => { axios.get.mockImplementation(() => Promise.resolve({ data: {…} })); // … }); test(“bad response”, () => { axios.get.mockImplementation(() => Promise.reject({ … })); // … }); … Read more

Get version number from package.json in React Redux (create-react-app)

Solving this without importing and exposing package.json to the create-react-app Requires: version 1.1.0+ of create-react-app .env REACT_APP_VERSION=$npm_package_version REACT_APP_NAME=$npm_package_name index.js console.log(`${process.env.REACT_APP_NAME} ${process.env.REACT_APP_VERSION}`) Note: the version (and many other npm config params) can be accessed Note 2: changes to the .env file will be picked only after you restart the development server

Re-render React component when prop changes

You have to add a condition in your componentDidUpdate method. The example is using fast-deep-equal to compare the objects. import equal from ‘fast-deep-equal’ … constructor(){ this.updateUser = this.updateUser.bind(this); } componentDidMount() { this.updateUser(); } componentDidUpdate(prevProps) { if(!equal(this.props.user, prevProps.user)) // Check if it’s a new user, you can also use some unique property, like the ID (this.props.user.id … Read more

How to get something from the state / store inside a redux-saga function?

As @markerikson already says, redux-saga exposes a very useful API select() to invoke a selector on the state for getting some part of it available inside the saga. For your example a simple implementation could be: /* * Selector. The query depends by the state shape */ export const getProject = (state) => state.project // … Read more

What is {this.props.children} and when you should use it?

What even is ‘children’? The React docs say that you can use props.children on components that represent ‘generic boxes’ and that don’t know their children ahead of time. For me, that didn’t really clear things up. I’m sure for some, that definition makes perfect sense but it didn’t for me. My simple explanation of what … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)