There are three type of selectors:
- basic
- cached
- cached factory
// Basic
const selectUsers = state => get(state, '_entites.users');
import * as app from "./app"; | |
import * as math from "./math"; | |
test("calls math.add", () => { | |
// store the original implementation | |
const originalAdd = math.add; | |
// mock add with the original implementation | |
math.add = jest.fn(originalAdd); |
import * as app from "./app"; | |
import * as math from "./math"; | |
math.add = jest.fn(); | |
math.subtract = jest.fn(); | |
test("calls math.add", () => { | |
app.doAdd(1, 2); | |
expect(math.add).toHaveBeenCalledWith(1, 2); | |
}); |
import * as app from "./app"; | |
import * as math from "./math"; | |
// Set all module functions to jest.fn | |
jest.mock("./math.js"); | |
test("calls math.add", () => { | |
app.doAdd(1, 2); | |
expect(math.add).toHaveBeenCalledWith(1, 2); | |
}); |
import * as app from "./app"; | |
import * as math from "./math"; | |
test("calls math.add", () => { | |
const addMock = jest.spyOn(math, "add"); | |
// calls the original implementation | |
expect(app.doAdd(1, 2)).toEqual(3); | |
// and the spy stores the calls to add |
test("mock implementation", () => { | |
const mock = jest.fn(() => "bar"); | |
expect(mock("foo")).toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); | |
test("also mock implementation", () => { | |
const mock = jest.fn().mockImplementation(() => "bar"); |
import * as app from "./app"; | |
import * as math from "./math"; | |
test("calls math.add", () => { | |
const addMock = jest.spyOn(math, "add"); | |
// override the implementation | |
addMock.mockImplementation(() => "mock"); | |
expect(app.doAdd(1, 2)).toEqual("mock"); |
Andrew: So, yes, my name is Andrew, I'm the React atom floating. Next time I think we'll make it so I have two phones so I can join from my account. Oh, there's Dan, let me add Dan. So this is our first time hosting a space. Well, I've done one before, but nothing like this. I have a feeling there's going to be a lot more people on this space than the silly spaces I've hosted before. So we might learn as we go along, but we we've gotten some great advice from some folks ahead of time the proper way to manage these things, including from Laurie, who I think we'll hear from later. So I think this is going to be really great. Hopefully the first of many.
Andrew: Let's just give it a few minutes for people to trickle in. I'm sure we'll have additional guests as we go along. Brian is giving us a reminder: if we do invite you on stage to please mute yourself when you're not speaking, I will do the same. We've got some great questions that we've collected in advance from members of the working group. And if you do
test("returns undefined by default", () => { | |
const mock = jest.fn(); | |
let result = mock("foo"); | |
expect(result).toBeUndefined(); | |
expect(mock).toHaveBeenCalled(); | |
expect(mock).toHaveBeenCalledTimes(1); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); |