This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
146 lines (134 loc) · 4 KB
/
test.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// @flow
/* eslint-env jest */
/* eslint-disable react/display-name */
import * as React from "react";
import renderer from "react-test-renderer";
import { mount } from "enzyme";
import Unsort, { ariaSortMap } from "./src";
test("exist", () => {
expect(typeof Unsort).toBe("function");
});
test("export ariaSortMap", () => {
expect(ariaSortMap).toBeDefined();
});
test("render prop renders content", () => {
const json = renderer
.create(<Unsort onSort={() => {}} render={() => <table />} />)
.toJSON();
expect(json).toMatchSnapshot();
});
describe("getSortProps", () => {
let getSortProps;
let mockOnSort = jest.fn();
beforeEach(() => {
getSortProps = setup({
onSort: mockOnSort,
initialSortKey: "foo",
initialSortDirection: "asc"
}).getSortProps;
});
afterEach(() => {
mockOnSort.mockClear();
});
test("sets tabIndex", () => {
expect(getSortProps("foo").tabIndex).toBe(0);
});
test("onClick triggers onSort", () => {
const fakeEvent = { target: null };
const { onClick } = getSortProps("foo");
onClick(fakeEvent);
expect(mockOnSort).toHaveBeenCalledTimes(1);
expect(mockOnSort).toHaveBeenCalledWith({
sortKey: "foo",
sortDirection: "desc"
});
});
test("onKeyUp triggers onSort if Enter have been pressed", () => {
const fakeEvent = { target: null, keyCode: 13 };
const { onKeyUp } = getSortProps("foo");
onKeyUp(fakeEvent);
expect(mockOnSort).toHaveBeenCalledTimes(1);
expect(mockOnSort).toHaveBeenCalledWith({
sortKey: "foo",
sortDirection: "desc"
});
});
test("onSort sets sortDirection in the right order", () => {
const fakeEvent = { target: null };
const { onClick } = getSortProps("foo");
onClick(fakeEvent);
onClick(fakeEvent);
onClick(fakeEvent);
expect(mockOnSort).toHaveBeenCalledTimes(3);
expect(mockOnSort.mock.calls).toEqual([
[{ sortKey: "foo", sortDirection: "desc" }],
[{ sortKey: null, sortDirection: null }],
[{ sortKey: "foo", sortDirection: "asc" }]
]);
});
describe("aria-sort", () => {
test("sets aria-sort if key is sorted", () => {
expect(getSortProps("foo")["aria-sort"]).toBe("ascending");
});
test("does not set aria-sort if key is not sorted", () => {
expect(getSortProps("bar")["aria-sort"]).toBeUndefined();
});
});
});
test("onClick should go to next sortDirection", () => {
const { getSortProps, renderSpy } = setup({
initialSortKey: "foo",
initialSortDirection: "asc"
});
const { onClick } = getSortProps("foo");
onClick();
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({ sortDirection: "desc" })
);
onClick();
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({ sortDirection: null, sortKey: null })
);
onClick();
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({ sortDirection: "asc", sortKey: "foo" })
);
});
test("change sort key should reset sort direction", () => {
const { getSortProps, renderSpy } = setup({
initialSortKey: "foo",
initialSortDirection: "asc"
});
const { onClick } = getSortProps("bar");
onClick();
expect(renderSpy).toHaveBeenLastCalledWith(
expect.objectContaining({ sortDirection: "asc", sortKey: "bar" })
);
});
test("sortKey is inherited from initialSortKey", () => {
const { sortKey } = setup({
onSort: jest.fn(),
initialSortKey: "foo"
});
expect(sortKey).toBe("foo");
});
test("sortDirection is inherited from initialSortDirection", () => {
const { sortDirection } = setup({
onSort: jest.fn(),
initialSortDirection: "asc"
});
expect(sortDirection).toBe("asc");
});
function setup(
{ render = () => <div />, onSort = jest.fn(), ...props }: any = {}
) {
let renderArg;
const renderSpy = jest.fn(controllerArg => {
renderArg = controllerArg;
return render(controllerArg);
});
const wrapper = mount(
<Unsort onSort={onSort} {...props} render={renderSpy} />
);
return { renderSpy, wrapper, ...renderArg };
}