npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recoil-mock

v0.1.6

Published

Recoil mocking for tests

Downloads

2,814

Readme

recoil-mock

An npm package that helps mocking Recoil states during tests.

Tested with Recoil 0.7.4 and Jest 28.1. It may not work with other testing frameworks. Help us improve it!

Usage with Jest

Setting up

First install by:

npm install -D recoil-mock

In your jest.config.js, map recoil to recoil-mock. This makes any Recoil state in your codebase mockable.

"moduleNameMapper": {
  "^recoil$": "recoil-mock"
}

Mocking Recoil atoms

In each test case, call createRecoilMockWrapper to create a pair of a mock context and a wrapper that wraps your component with a customized RecoilRoot.

The mock context provides methods set, clear and clearAll to modify mocked value of any Recoil atom or selector.

import { act, render } from "@testing-library/react";
import { createRecoilMockWrapper } from "recoil-mock";

const fooAtom = atom({ key: "foo", default: "foo" });
// App for testing
const MyApp = () => {
  const foo = useRecoilValue(fooAtom);
  return <div>foo is {foo}</div>;
};

test("mock Recoil atom", async () => {
  const { context, wrapper } = createRecoilMockWrapper();
  context.set(fooAtom, "bar");
  const { findByText } = render(<MyApp />, { wrapper });
  // The mocked value is applied
  await findByText("foo is bar");

  // If you update mocked value after rendering, you should wrap it in an `act` call.
  act(() => {
    context.set(fooAtom, "pika!");
  });
  // Your app should have reflected to the update here.
  await findByText("foo is pika!");
});

Mocking Recoil selectors

With recoil-mock, you can mock selectors as well. You can apply mocked values directly to any selector without touching its dependencies. When a selector is mocked, its dependencies are not evaluated.

import { act, render } from "@testing-library/react";
import { createRecoilMockWrapper } from "recoil-mock";

const fooAtom = atom({ key: "foo", default: "foo" });
const barAtom = atom({ key: "bar", default: "bar" });
const fooBarSelector = selector({
  key: "fooBar",
  get: ({ get }) => get(fooAtom) + get(barAtom),
});
// App for testing
const MyApp = () => {
  const fooBar = useRecoilValue(fooBarSelector);
  return <div>fooBar is {fooBar}</div>;
};

test("mock Recoil selector", async () => {
  const { context, wrapper } = createRecoilMockWrapper();
  context.set(fooBarSelector, "pika!");
  const { findByText } = render(<MyApp />, { wrapper });
  // The mocked value is applied
  await findByText("fooBar is pika!");
});

Mocking atom/selector families

When you want to mock an atom family or a selector family, you mock individual atoms/selectors created by the family.

import { act, render } from "@testing-library/react";
import { createRecoilMockWrapper } from "recoil-mock";

const fooAtom = atom({
  key: "foo",
  default: "foo",
});
const repeatedFooFamily = selectorFamily({
  key: "repeatedFooFamily",
  get: (times) => ({ get }) => get(fooAtom).repeat(times),
});
// App for testing
const MyApp = () => {
  const repeatedFoo = useRecoilValue(repeatedFooFamily(3));
  return <div>repeatedFoo is {repeatedFoo}</div>;
};

test("mock Recoil selector family", async () => {
  const { context, wrapper } = createRecoilMockWrapper();
  const { findByText } = render(<MyApp />, { wrapper });
  await findByText("repeatedFoo is foofoofoo");

  act(() => {
    context.set(repeatedFooFamily(3), "pika!");
  });
  // The mocked value is applied
  await findByText("repeatedFoo is pika!");
});

For maintainers

How to publish a new version

Just build and publish locally. :sweat_smile:

License

MIT