aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-articles-list/use-articles-list.test.tsx
blob: 6191ed6ad9c5a0dba1d869c621ec181df475e04c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  it,
  jest,
} from '@jest/globals';
import { act, renderHook, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react';
import { SWRConfig } from 'swr';
import { wpPostsFixture } from '../../../../tests/fixtures';
import { getConnection } from '../../../../tests/utils/graphql';
import { convertPostPreviewToArticlePreview } from '../../../services/graphql';
import { useArticlesList } from './use-articles-list';

const wrapper = ({ children }: { children?: ReactNode }) => {
  const map = new Map();

  return (
    <SWRConfig
      value={{
        provider: () => map,
        isOnline() {
          return true;
        },
        isVisible() {
          return true;
        },
        initFocus() {
          /* nothing */
        },
        initReconnect() {
          /* nothing */
        },
      }}
    >
      {children}
    </SWRConfig>
  );
};

describe('useArticlesList', () => {
  beforeEach(() => {
    /* Not sure why it is needed, but without it Jest was complaining with `You
     * are trying to import a file after the Jest environment has been torn
     * down`... Maybe because of useSWR? */
    jest.useFakeTimers({
      doNotFake: ['queueMicrotask'],
    });
  });

  afterEach(() => {
    jest.runOnlyPendingTimers();
    jest.useRealTimers();
  });

  it('can return the first new result index when loading more posts', async () => {
    const perPage = 5;
    const { result } = renderHook(() => useArticlesList({ perPage }), {
      wrapper,
    });

    expect.assertions(2);

    expect(result.current.firstNewResultIndex).toBeUndefined();

    await act(async () => {
      await result.current.loadMore();
    });

    // Assuming there is more than one page.
    expect(result.current.firstNewResultIndex).toBe(perPage + 1);
  });

  it('converts a WordPress post connection to an article connection', async () => {
    const perPage = 1;
    const { result } = renderHook(() => useArticlesList({ perPage }), {
      wrapper,
    });
    const connection = getConnection({
      after: null,
      data: wpPostsFixture,
      first: perPage,
    });

    expect.hasAssertions();

    await waitFor(() => {
      expect(result.current.articles).toBeDefined();
    });

    expect(result.current.articles).toStrictEqual([
      {
        edges: connection.edges.map((edge) => {
          return {
            cursor: edge.cursor,
            node: convertPostPreviewToArticlePreview(edge.node),
          };
        }),
        pageInfo: {
          endCursor: connection.pageInfo.endCursor,
          hasNextPage: connection.pageInfo.hasNextPage,
          total: connection.pageInfo.total,
        },
      },
    ]);
  });
});