|
1
|
import React from "react" |
|
2
|
import { beforeEach, describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock data |
|
6
|
const previousPage = { |
|
7
|
title: "Previous", |
|
8
|
parentTitle: "Parent", |
|
9
|
link: "/previous", |
|
10
|
} as Page | null |
|
11
|
const nextPage = { |
|
12
|
title: "Next", |
|
13
|
parentTitle: "Parent", |
|
14
|
link: "/next", |
|
15
|
} as Page | null |
|
16
|
|
|
17
|
// mock functions |
|
18
|
const mockUsePagination = vi.fn(() => ({ |
|
19
|
previousPage, |
|
20
|
nextPage, |
|
21
|
})) |
|
22
|
|
|
23
|
// mock components |
|
24
|
vi.mock("@/providers/Pagination", () => ({ |
|
25
|
usePagination: () => mockUsePagination(), |
|
26
|
})) |
|
27
|
|
|
28
|
vi.mock("@/components/Pagination/Card", () => ({ |
|
29
|
PaginationCard: ({ |
|
30
|
type, |
|
31
|
title, |
|
32
|
parentTitle, |
|
33
|
link, |
|
34
|
}: { |
|
35
|
type: "previous" | "next" |
|
36
|
title: string |
|
37
|
parentTitle: string |
|
38
|
link: string |
|
39
|
}) => ( |
|
40
|
<div |
|
41
|
data-testid="pagination-card" |
|
42
|
data-type={type} |
|
43
|
data-title={title} |
|
44
|
data-parent-title={parentTitle} |
|
45
|
data-link={link} |
|
46
|
> |
|
47
|
{title} |
|
48
|
</div> |
|
49
|
), |
|
50
|
})) |
|
51
|
|
|
52
|
import { Pagination } from "../index" |
|
53
|
import { Page } from "../../../providers" |
|
54
|
|
|
55
|
beforeEach(() => { |
|
56
|
vi.clearAllMocks() |
|
57
|
}) |
|
58
|
|
|
59
|
describe("render", () => { |
|
60
|
test("renders previous and next pagination cards", () => { |
|
61
|
const { container } = render(<Pagination />) |
|
62
|
expect(container).toBeInTheDocument() |
|
63
|
const paginationCards = container.querySelectorAll( |
|
64
|
"[data-testid='pagination-card']" |
|
65
|
) |
|
66
|
expect(paginationCards).toHaveLength(2) |
|
67
|
expect(paginationCards[0]).toHaveAttribute("data-type", "previous") |
|
68
|
expect(paginationCards[0]).toHaveAttribute("data-title", "Previous") |
|
69
|
expect(paginationCards[0]).toHaveAttribute("data-parent-title", "Parent") |
|
70
|
expect(paginationCards[0]).toHaveAttribute("data-link", "/previous") |
|
71
|
expect(paginationCards[1]).toHaveAttribute("data-type", "next") |
|
72
|
expect(paginationCards[1]).toHaveAttribute("data-title", "Next") |
|
73
|
expect(paginationCards[1]).toHaveAttribute("data-parent-title", "Parent") |
|
74
|
expect(paginationCards[1]).toHaveAttribute("data-link", "/next") |
|
75
|
}) |
|
76
|
|
|
77
|
test("don't render previous card when previousPage is not set", () => { |
|
78
|
mockUsePagination.mockReturnValue({ |
|
79
|
previousPage: null, |
|
80
|
nextPage, |
|
81
|
}) |
|
82
|
const { container } = render(<Pagination />) |
|
83
|
expect(container).toBeInTheDocument() |
|
84
|
const paginationCards = container.querySelectorAll( |
|
85
|
"[data-testid='pagination-card']" |
|
86
|
) |
|
87
|
expect(paginationCards).toHaveLength(1) |
|
88
|
expect(paginationCards[0]).toHaveAttribute("data-type", "next") |
|
89
|
expect(paginationCards[0]).toHaveAttribute("data-title", "Next") |
|
90
|
expect(paginationCards[0]).toHaveAttribute("data-parent-title", "Parent") |
|
91
|
expect(paginationCards[0]).toHaveAttribute("data-link", "/next") |
|
92
|
}) |
|
93
|
|
|
94
|
test("don't render next card when nextPage is not set", () => { |
|
95
|
mockUsePagination.mockReturnValue({ |
|
96
|
previousPage, |
|
97
|
nextPage: null, |
|
98
|
}) |
|
99
|
const { container } = render(<Pagination />) |
|
100
|
expect(container).toBeInTheDocument() |
|
101
|
const paginationCards = container.querySelectorAll( |
|
102
|
"[data-testid='pagination-card']" |
|
103
|
) |
|
104
|
expect(paginationCards).toHaveLength(1) |
|
105
|
expect(paginationCards[0]).toHaveAttribute("data-type", "previous") |
|
106
|
expect(paginationCards[0]).toHaveAttribute("data-title", "Previous") |
|
107
|
expect(paginationCards[0]).toHaveAttribute("data-parent-title", "Parent") |
|
108
|
expect(paginationCards[0]).toHaveAttribute("data-link", "/previous") |
|
109
|
}) |
|
110
|
}) |