10.3.7 • Published 2 years ago

@rest-hooks/test v10.3.7

Weekly downloads
16,826
License
Apache-2.0
Repository
github
Last release
2 years ago

🛌🎣 Rest Hooks Testing

Coverage Status

🏁Guides  |  🏁API Reference

Features

Usage

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/';
}
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: [],
};
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>
);
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);
});
10.3.7

2 years ago

11.0.0-next.0

2 years ago

10.3.2

2 years ago

10.3.3

2 years ago

10.3.4

2 years ago

10.3.5

2 years ago

10.3.6

2 years ago

10.3.0

2 years ago

10.3.1

2 years ago

10.2.1

2 years ago

10.2.0

2 years ago

10.1.1

2 years ago

10.1.2

2 years ago

10.0.0

2 years ago

9.1.1

2 years ago

9.1.4

2 years ago

9.1.3

2 years ago

9.1.2

2 years ago

10.1.0

2 years ago

7.4.2

2 years ago

7.4.1

2 years ago

9.1.0

2 years ago

7.4.0

2 years ago

7.3.9

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

9.0.0

2 years ago

7.3.8-beta.0

2 years ago

7.3.8-beta.2

2 years ago

7.3.8-beta.1

2 years ago

7.3.8

2 years ago

7.3.7

2 years ago

7.3.6

2 years ago

7.3.5

2 years ago

7.3.4

2 years ago

7.3.3

2 years ago

7.3.2

2 years ago

7.3.1

3 years ago

7.3.0

3 years ago

7.2.2

3 years ago

7.2.0-beta.0

3 years ago

7.2.1

3 years ago

7.2.0

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

6.2.1

3 years ago

7.0.0

3 years ago

7.0.0-beta.3

3 years ago

7.0.0-beta.2

3 years ago

7.0.0-beta.1

3 years ago

7.0.0-beta.0

3 years ago

6.2.0

3 years ago

6.1.0

3 years ago

6.0.1

3 years ago

5.0.0

3 years ago

6.0.0

3 years ago

4.1.4

3 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.2-beta.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-k.2

4 years ago

2.0.0-k.1

4 years ago

2.0.0-k.0

4 years ago

2.0.0-j.2

4 years ago

2.0.0-j.1

4 years ago

2.0.0-j.0

4 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta.1

5 years ago

2.0.0-beta.0

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.14-beta.0

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.1.0

5 years ago