# @rest-hooks/test

> Testing utilities for Rest Hooks

Latest version **10.3.7** (published 2023-05-02) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @rest-hooks/test
pnpm add @rest-hooks/test
yarn add @rest-hooks/test
bun add @rest-hooks/test
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 10.3.7 |
| Published | 2023-05-02 |
| First published | 2019-11-20 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^12.17 \|\| ^13.7 \|\| >=14 |
| Dependencies | 5 |
| Unpacked size | 265.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2040 |
| Author | Nathaniel Tucker |
| Maintainers | ntucker, ljharb |
| Keywords | test, storybook, hook, react, data, cache, flux, suspense, fetch, hook, networking, concurrent mode, typescript, redux, data fetching, data cache, normalized cache, async, swr |

## Links

- npm: https://www.npmjs.com/package/@rest-hooks/test
- Repository: https://github.com/data-client/rest-hooks
- Homepage: https://resthooks.io/docs/guides/storybook
- Issues: https://github.com/data-client/rest-hooks/issues
- npm.io page: https://npm.io/package/@rest-hooks/test

## Dependencies (5)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.17.0
- [react-error-boundary](https://npm.io/package/react-error-boundary.md) ^4.0.0
- [@testing-library/react](https://npm.io/package/@testing-library/react.md) ^14.0.0
- [@testing-library/react-hooks](https://npm.io/package/@testing-library/react-hooks.md) ~8.0.0
- [@testing-library/react-native](https://npm.io/package/@testing-library/react-native.md) ^12.0.1

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 10.3.7 (latest) — 2023-05-02
- 11.0.0-next.0 (next) — 2023-04-26
- 7.3.8-beta.2 (beta) — 2022-09-19
- 10.3.6 — 2023-03-31
- 10.3.5 — 2023-03-30
- 10.3.4 — 2023-03-28
- 10.3.3 — 2023-03-28
- 10.3.2 — 2023-03-27
- 10.3.1 — 2023-03-15
- 10.3.0 — 2023-03-12
- 10.2.1 — 2023-03-02
- 10.2.0 — 2023-02-21
- 10.1.2 — 2023-02-11
- 10.1.1 — 2023-01-30
- 10.1.0 — 2023-01-23
- … 85 more at https://npm.io/package/@rest-hooks/test/versions

## README

# ![🛌🎣 Rest Hooks Testing](../../packages/rest-hooks/rest_hooks_logo_and_text.svg?sanitize=true)

[![Coverage Status](https://img.shields.io/codecov/c/gh/data-client/rest-hooks/master.svg?style=flat-square)](https://app.codecov.io/gh/data-client/rest-hooks?branch=master)

<div align="center">

**[🏁Guides](https://resthooks.io/docs/guides/storybook)** &nbsp;|&nbsp; [🏁API Reference](https://resthooks.io/docs/api/makeRenderRestHook)

</div>

## Features

- [x] [Mocking for Storybook](https://resthooks.io/docs/guides/storybook)
- [x] [Fixtures for component tests](https://resthooks.io/docs/guides/unit-testing-components)
- [x] [Hook unit testing utility](https://resthooks.io/docs/guides/unit-testing-hooks)

## Usage

<details><summary><b>Resource</b></summary>

```typescript
import { Resource } from '@rest-hooks/rest';

export default class ArticleResource extends Resource {
  readonly id: number | undefined = undefined;
  readonly content: string = '';
  readonly author: number | null = null;
  readonly contributors: number[] = [];

  pk() {
    return this.id?.toString();
  }
  static urlRoot = 'http://test.com/article/';
}
```

</details>

<details><summary><b>Fixtures</b></summary>

```typescript
export default {
  full: [
    {
      request: ArticleResource.list(),
      params: { maxResults: 10 },
      result: [
        {
          id: 5,
          content: 'have a merry christmas',
          author: 2,
          contributors: [],
        },
        {
          id: 532,
          content: 'never again',
          author: 23,
          contributors: [5],
        },
      ],
    },
  ],
  empty: [
    {
      request: ArticleResource.list(),
      params: { maxResults: 10 },
      result: [],
    },
  ],
  error: [
    {
      request: ArticleResource.list(),
      params: { maxResults: 10 },
      result: { message: 'Bad request', status: 400, name: 'Not Found' },
      error: true,
    },
  ],
  loading: [],
};
```

</details>

<details open><summary><b>Storybook</b></summary>

```typescript
import { MockResolver } from '@rest-hooks/test';
import type { Fixture } from '@rest-hooks/test';
import { Story } from '@storybook/react/types-6-0';

import ArticleList from 'ArticleList';
import options from './fixtures';

export default {
  title: 'Pages/ArticleList',
  component: ArticleList,
};

export const FullArticleList = ({ result }) => (
  <MockResolver fixtures={options[result]}>
    <ArticleList maxResults={10} />
  </MockResolver>
);
```

</details>

<details open><summary><b>Hook Unit Test</b></summary>

```typescript
import { CacheProvider } from '@rest-hooks/react';
import { makeRenderRestHook } from '@rest-hooks/test';
import options from './fixtures';

const renderRestHook = makeRenderRestHook(CacheProvider);

it('should resolve list', async () => {
  const { result } = renderRestHook(
    () => {
      return useSuspense(ArticleResource.list(), {
        maxResults: 10,
      });
    },
    { initialFixtures: options.full },
  );
  expect(result.current).toBeDefined();
  expect(result.current.length).toBe(2);
  expect(result.current[0]).toBeInstanceOf(ArticleResource);
});

it('should throw errors on bad network', async () => {
  const { result } = renderRestHook(
    () => {
      return useSuspense(ArticleResource.list(), {
        maxResults: 10,
      });
    },
    { initialFixtures: options.error },
  );
  expect(result.error).toBeDefined();
  expect((result.error as any).status).toBe(400);
});
```

</details>

---
_Source: https://npm.io/package/@rest-hooks/test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
