Skip to content

Instantly share code, notes, and snippets.

@jasonleehodges
Last active December 11, 2021 19:40
Show Gist options
  • Save jasonleehodges/7b1ff8d27a33dc73ade98d3580f6732c to your computer and use it in GitHub Desktop.
Save jasonleehodges/7b1ff8d27a33dc73ade98d3580f6732c to your computer and use it in GitHub Desktop.
Selector Example with Currying and Selector Unit Tests
import React from "react";
import { useSelector } from "react-redux";
import {selectCountFormatted} from './selectors'
export const Counter = () => {
const countFormattedAsDollars = useSelector(selectCountFormatted('$'));
...
describe("counter selectors", () => {
const state: RootState = {
counter: {
value: 3,
status: "idle",
},
};
it("should format count in dollars", () => {
const actual = selectCountFormatted("$")(state);
expect(actual).toEqual("$3");
});
it("should format count as a percentage", () => {
const actual = selectCountFormatted("%")(state);
expect(actual).toEqual("3%");
});
it("should select multiple items from state", () => {
const actual = selectCountAndStatus(state);
expect(actual).toEqual("The current count is 3 and the status is idle");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment