Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added tests for pagination #37

Merged
merged 24 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
98a7ad7
test: jest testing setup
anuraghazra Sep 7, 2020
77b3e5b
test: fixed jest typescript definitions
anuraghazra Sep 7, 2020
6c6851d
fix: test script
anuraghazra Sep 8, 2020
48522b3
chore(deps): move deps to dev
anuraghazra Sep 8, 2020
75be5a5
chore: fix conflicts
anuraghazra Sep 8, 2020
a2ccec2
Merge branch 'master' into tests
anuraghazra Sep 8, 2020
faf10fd
test: added tests for accordion
anuraghazra Sep 8, 2020
aa0fe98
Merge branch 'master' into tests
anuraghazra Sep 9, 2020
c2e3582
test: added husky precommit test
anuraghazra Sep 9, 2020
56e5b8c
test: axe container test
anuraghazra Sep 9, 2020
c6c1fa1
chore(infra): added github action test
anuraghazra Sep 9, 2020
7452121
test: added tests for number input
anuraghazra Sep 9, 2020
4a3af5f
fix: a11y aria-valuetext issue with NumberInput
anuraghazra Sep 9, 2020
6b9d8ef
test: added tests for Slider
anuraghazra Sep 9, 2020
51a27fa
Merge branch 'master' into tests
anuraghazra Sep 9, 2020
26a5d63
Merge branch 'master' into tests
anuraghazra Sep 9, 2020
9e2da6d
Merge branch 'master' into tests
anuraghazra Sep 10, 2020
7962d34
test: added tests for progress
anuraghazra Sep 10, 2020
4d62f99
test: added tests for link
anuraghazra Sep 10, 2020
6b1ee43
Merge branch 'master' into tests
anuraghazra Sep 10, 2020
d402c58
Merge branch 'master' into tests
anuraghazra Sep 11, 2020
3e6e092
test: added tests for breadcrumb
anuraghazra Sep 11, 2020
01c5d9e
test: added tests for pagination
anuraghazra Sep 11, 2020
8ee7ab4
Merge branch 'master' into tests
anuraghazra Sep 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/breadcrumbs/__tests__/Breadcrumbs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";
import { axe } from "jest-axe";
import { render, press, click, fireEvent } from "reakit-test-utils";
import { render } from "reakit-test-utils";

import { Breadcrumbs, BreadcrumbLink } from "..";

const BreadcrumbComp = () => {
Expand Down
101 changes: 101 additions & 0 deletions src/pagination/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as React from "react";
import { axe } from "jest-axe";
import { render, press } from "reakit-test-utils";

import {
TGoto,
Pagination,
PaginationButton,
usePaginationState,
UsePaginationProps,
} from "..";

const PaginationComp: React.FC<UsePaginationProps> = props => {
const state = usePaginationState({ count: 10, ...props });

return (
<Pagination {...state}>
<ul style={{ display: "flex", listStyle: "none" }}>
<li>
<PaginationButton goto="first" {...state}>
First
</PaginationButton>
</li>
<li>
<PaginationButton goto="prev" {...state}>
Previous
</PaginationButton>
</li>
{state.pages.map(page => {
if (page === "start-ellipsis" || page === "end-ellipsis") {
return <li key={page}>...</li>;
}

return (
<li key={page}>
<PaginationButton
goto={page as TGoto}
style={{
fontWeight: state.currentPage === page ? "bold" : undefined,
}}
{...state}
>
{page}
</PaginationButton>
</li>
);
})}
<li>
<PaginationButton goto="next" {...state}>
Next
</PaginationButton>
</li>
<li>
<PaginationButton goto="last" {...state}>
Last
</PaginationButton>
</li>
</ul>
</Pagination>
);
};

describe("Pagination", () => {
it("should render correctly", () => {
const { getByTestId: testId, queryByText: text } = render(
<PaginationComp />,
);

press.Tab();
expect(text("1")).toHaveFocus();
expect(text("Previous")).toBeDisabled();
expect(text("First")).toBeDisabled();

press.Tab();
expect(text("2")).toHaveFocus();

press.Enter();
expect(text("First")).not.toBeDisabled();
expect(text("Previous")).not.toBeDisabled();

press.Tab();
press.Tab();
press.Tab();
press.Enter();
expect(text("2")).toBeNull();

press.Tab();
press.Tab();
press.Enter();
expect(text("5")).toBeNull();
expect(text("Last")).toBeDisabled();
expect(text("Next")).toBeDisabled();
});

test("Pagination renders with no a11y violations", async () => {
const { container } = render(<PaginationComp />);
const results = await axe(container);

expect(results).toHaveNoViolations();
});
});
176 changes: 176 additions & 0 deletions src/pagination/__tests__/PaginationState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { renderHook, act } from "reakit-test-utils/hooks";

import { usePaginationState } from "..";

describe("usePaginationState", () => {
it("should render correctly", () => {
const {
result: { current },
} = renderHook(() => usePaginationState({ count: 5 }));

expect(current).toMatchInlineSnapshot(`
Object {
"currentPage": 1,
"first": [Function],
"isAtMax": false,
"isAtMin": true,
"last": [Function],
"move": [Function],
"next": [Function],
"pages": Array [
1,
2,
3,
4,
5,
],
"prev": [Function],
}
`);
});

it("should rover to next/prev/last/first/move", async () => {
const { result } = renderHook(() => usePaginationState({ count: 5 }));

expect(result.current.isAtMin).toBe(true);
expect(result.current.isAtMax).toBe(false);
expect(result.current.currentPage).toBe(1);

act(() => {
result.current.next();
});

expect(result.current.isAtMin).toBe(false);
expect(result.current.isAtMax).toBe(false);
expect(result.current.currentPage).toBe(2);

act(() => {
result.current.next();
});

expect(result.current.currentPage).toBe(3);

// last
act(() => {
result.current.last();
});

expect(result.current.isAtMin).toBe(false);
expect(result.current.isAtMax).toBe(true);
expect(result.current.currentPage).toBe(5);

// first
act(() => {
result.current.first();
});

expect(result.current.isAtMin).toBe(true);
expect(result.current.isAtMax).toBe(false);
expect(result.current.currentPage).toBe(1);

// move
act(() => {
result.current.move(3);
});

expect(result.current.isAtMin).toBe(false);
expect(result.current.isAtMax).toBe(false);
expect(result.current.currentPage).toBe(3);
});

it("has a disabled previous button & an enabled next button when count > 1", () => {
const { isAtMin, isAtMax } = renderHook(() =>
usePaginationState({ count: 2 }),
).result.current;

expect(isAtMin).toBe(true);
expect(isAtMax).toBe(false);
});

it("should have disabled next button if defaultPage === count & enabled previous button", () => {
const { isAtMin, isAtMax } = renderHook(() =>
usePaginationState({ count: 2, defaultPage: 2 }),
).result.current;

expect(isAtMin).toBe(false);
expect(isAtMax).toBe(true);
});

it("has no ellipses when count <= 7", () => {
const { pages } = renderHook(() =>
usePaginationState({ count: 7 }),
).result.current;

expect(pages).not.toContain("end-ellipsis");
});

it("should have end-ellipses when count >= 8", () => {
const { pages } = renderHook(() =>
usePaginationState({ count: 8 }),
).result.current;

expect(pages).toContain("end-ellipsis");
});

it("should have start-ellipses when defaultPage >= 5", () => {
const { pages } = renderHook(() =>
usePaginationState({ count: 8, defaultPage: 5 }),
).result.current;

expect(pages).toContain("start-ellipsis");
});

it("should have start-ellipses & end-ellipsis when count >= 5", () => {
const { pages } = renderHook(() =>
usePaginationState({ count: 9, defaultPage: 5 }),
).result.current;

expect(pages).toContain("start-ellipsis");
expect(pages).toContain("end-ellipsis");
});

it("should have proper boundryCount", () => {
const {
result: {
current: { pages },
},
} = renderHook(() =>
usePaginationState({
count: 40,
boundaryCount: 5,
defaultPage: 20,
}),
);

expect(pages).toHaveLength(15);
expect(pages.slice(0, 6)).toEqual([1, 2, 3, 4, 5, "start-ellipsis"]);
expect(pages.slice(-6)).toEqual(["end-ellipsis", 36, 37, 38, 39, 40]);
});

it("should have proper siblingCount", () => {
const {
result: {
current: { pages },
},
} = renderHook(() =>
usePaginationState({
count: 11,
siblingCount: 2,
defaultPage: 6,
}),
);

expect(pages).toHaveLength(9);
expect(pages).toEqual([
1,
"start-ellipsis",
4, // siblings
5, // siblings
6, // default page
7, // siblings
8, // siblings
"end-ellipsis",
11,
]);
});
});