@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.7442latest
7.3.8-beta.20beta
11.0.0-next.00next

Version History

VersionDownloads (Last 7 Days)Published
10.3.7442
11.0.0-next.00
10.3.61
10.3.50
10.3.40
10.3.30
10.3.20
10.3.10
10.3.00
10.2.141
10.2.00
10.1.20
10.1.10
10.1.00
10.0.00
9.1.411
9.1.30
9.1.20
9.1.10
9.1.00
9.0.00
8.0.11
8.0.00
7.4.270
7.4.10
7.4.011
7.3.90
7.3.80
7.3.8-beta.20
7.3.8-beta.10
7.3.8-beta.00
7.3.70
7.3.60
7.3.554
7.3.41
7.3.30
7.3.20
7.3.12
7.3.00
7.2.20
7.2.10
7.2.00
7.2.0-beta.00
7.1.30
7.1.20
7.1.10
7.1.022
7.0.00
6.2.10
7.0.0-beta.30
7.0.0-beta.20
7.0.0-beta.10
7.0.0-beta.00
6.2.08
6.1.00
6.0.10
6.0.00
5.0.00
4.1.40
4.1.30
4.1.20
4.1.2-beta.00
4.1.10
4.1.00
4.0.10
4.0.00
3.0.10
3.0.00
2.0.10
2.0.00
2.0.0-k.20
2.0.0-k.10
2.0.0-k.00
2.0.0-j.20
2.0.0-j.10
2.0.0-j.00
2.0.0-beta.20
2.0.0-beta.10
2.0.0-beta.00
1.0.188
1.0.170
1.0.161
1.0.150
1.0.140
1.0.14-beta.00
1.0.130
1.0.120
1.0.110
1.0.100
1.0.90
1.0.80
1.0.70
1.0.60
1.0.50
1.0.40
1.0.30
1.0.21
1.0.10
1.0.01
0.1.00

Package Sidebar

Install

npm i @rest-hooks/test

Weekly Downloads

675

Version

10.3.7

License

Apache-2.0

Unpacked Size

272 kB

Total Files

96

Last publish

Collaborators

  • ntucker
  • ljharb