@rest-hooks/test
TypeScript icon, indicating that this package has built-in type declarations

10.3.7 • Public • Published

🛌🎣 Rest Hooks Testing

Coverage Status

Features

Usage

Resource
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/';
}
Fixtures
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: [],
};
Storybook
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>
);
Hook Unit Test
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);
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
10.3.7500latest
7.3.8-beta.22beta
11.0.0-next.02next

Version History

VersionDownloads (Last 7 Days)Published
10.3.7500
11.0.0-next.02
10.3.62
10.3.52
10.3.42
10.3.32
10.3.22
10.3.12
10.3.02
10.2.1119
10.2.02
10.1.27
10.1.12
10.1.02
10.0.02
9.1.45
9.1.32
9.1.22
9.1.12
9.1.02
9.0.02
8.0.12
8.0.02
7.4.2153
7.4.12
7.4.017
7.3.92
7.3.812
7.3.8-beta.22
7.3.8-beta.12
7.3.8-beta.02
7.3.72
7.3.62
7.3.557
7.3.42
7.3.32
7.3.22
7.3.14
7.3.02
7.2.22
7.2.13
7.2.02
7.2.0-beta.02
7.1.37
7.1.22
7.1.12
7.1.02
7.0.02
6.2.12
7.0.0-beta.32
7.0.0-beta.22
7.0.0-beta.12
7.0.0-beta.02
6.2.04
6.1.02
6.0.12
6.0.02
5.0.02
4.1.42
4.1.32
4.1.22
4.1.2-beta.02
4.1.12
4.1.02
4.0.12
4.0.02
3.0.12
3.0.02
2.0.12
2.0.02
2.0.0-k.22
2.0.0-k.12
2.0.0-k.02
2.0.0-j.22
2.0.0-j.12
2.0.0-j.02
2.0.0-beta.22
2.0.0-beta.12
2.0.0-beta.02
1.0.1833
1.0.172
1.0.162
1.0.152
1.0.142
1.0.14-beta.02
1.0.132
1.0.122
1.0.112
1.0.102
1.0.92
1.0.82
1.0.72
1.0.62
1.0.52
1.0.42
1.0.32
1.0.22
1.0.12
1.0.02
0.1.02

Package Sidebar

Install

npm i @rest-hooks/test

Weekly Downloads

1,095

Version

10.3.7

License

Apache-2.0

Unpacked Size

272 kB

Total Files

96

Last publish

Collaborators

  • ntucker
  • ljharb