0.11.4 • Published 1 day ago

@data-client/test v0.11.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 day ago

Data Client Testing

Coverage Status

🏁Guides  |  🏁API Reference

Features

Usage

import { createResource, Entity } from '@data-client/rest';

export default class Article extends Entity {
  id = '';
  content = '';
  author: number | null = null;
  contributors: number[] = [];

  pk() {
    return this.id?.toString();
  }
}
export const ArticleResource = createResource({
  urlRoot: 'http://test.com',
  path: '/article/:id',
  schema: Article,
})
export default {
  full: [
    {
      endpoint: ArticleResource.getList,
      args: [{ maxResults: 10 }],
      response: [
        {
          id: 5,
          content: 'have a merry christmas',
          author: 2,
          contributors: [],
        },
        {
          id: 532,
          content: 'never again',
          author: 23,
          contributors: [5],
        },
      ],
    },
  ],
  empty: [
    {
      endpoint: ArticleResource.getList,
      args: [{ maxResults: 10 }],
      response: [],
    },
  ],
  error: [
    {
      endpoint: ArticleResource.getList,
      args: [{ maxResults: 10 }],
      response: { message: 'Bad request', status: 400, name: 'Not Found' },
      error: true,
    },
  ],
  loading: [],
};
import { MockResolver } from '@data-client/test';
import type { Fixture } from '@data-client/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 '@data-client/react';
import { makeRenderDataClient } from '@data-client/test';
import options from './fixtures';

const renderDataClient = makeRenderDataClient(CacheProvider);

it('should resolve list', async () => {
  const { result } = renderDataClient(
    () => {
      return useSuspense(ArticleResource.getList, {
        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 } = renderDataClient(
    () => {
      return useSuspense(ArticleResource.getList, {
        maxResults: 10,
      });
    },
    { initialFixtures: options.error },
  );
  expect(result.error).toBeDefined();
  expect((result.error as any).status).toBe(400);
});
0.11.4

1 day ago

0.11.0

19 days ago

0.10.0

3 months ago

0.9.2

8 months ago

0.9.0

8 months ago

0.8.0

8 months ago

0.3.1

8 months ago

0.3.0

9 months ago

1.0.0

9 months ago

0.2.1

9 months ago

0.2.0

9 months ago

0.1.4

9 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago