Skip to content

Commit

Permalink
Added test to test for rerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ncthbrt committed Aug 5, 2019
1 parent 8a99502 commit 001b71a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# react-use-elmish

`react-use-elmish` - A React hook for unified purely-functional state and effect management.
A React hook for unified purely-functional state and effect management.

[![Coverage Status](https://coveralls.io/repos/github/ncthbrt/react-use-elmish/badge.svg?branch=master)](https://coveralls.io/github/ncthbrt/react-use-elmish?branch=master) ![GitHub](https://img.shields.io/github/license/ncthbrt/react-use-elmish) ![npm](https://img.shields.io/npm/v/react-use-elmish)

## What is it?

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"type": "git",
"url": "git+https://github.com/ncthbrt/react-use-elmish.git"
},
"homepage": "https://github.com/ncthbrt/react-use-elmish",
"author": "Nick Cuthbert",
"license": "MIT",
"bugs": {
Expand Down
25 changes: 25 additions & 0 deletions src/useElmish.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { renderHook, act } from "@testing-library/react-hooks";
import useElmish, { Effects } from "./index";
import { render } from "react-dom";

test("should act as a reducer", () => {
const { result } = renderHook(() =>
Expand Down Expand Up @@ -44,3 +45,27 @@ test("should execute effects", async () => {

expect(result.current[0].state).toBe("stoppedSinging");
});

test("should not trigger rerender if no effects and state the same", async () => {
let renderCount = 0;
const { result } = renderHook(() => {
++renderCount;
return useElmish(
(prev, action) => {
if (action == "increment") {
return [prev + 1, Effects.none()];
} else {
return [prev, Effects.none()];
}
},
() => [0, Effects.none()]
);
});

act(() => {
result.current[1]("other");
});

expect(result.current[0]).toBe(0);
expect(renderCount).toBe(1);
});

0 comments on commit 001b71a

Please sign in to comment.