Skip to content

Commit

Permalink
Add 2 tests: default state, method use
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Dushkin committed May 9, 2019
1 parent e76a71e commit f969e4f
Show file tree
Hide file tree
Showing 5 changed files with 2,189 additions and 34 deletions.
77 changes: 77 additions & 0 deletions __tests__/create-store.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React, { Children } from "react";
import { create, act } from "react-test-renderer";
import { createUseStore } from "../src/create-store";

type State = {
data?: string;
isLoading?: boolean;
};

const defaultState: State = {
isLoading: true
};

const methods = {
setData: (_: State) => (data: string) => ({
isLoading: false,
data
})
};

const createUseTestStore = () => createUseStore(defaultState, methods);

describe("React Localux", () => {
it("renders correctly initial state", () => {
const useStore = createUseTestStore();

function Root() {
return (
<useStore.Provider initialState={defaultState}>
<Item />
</useStore.Provider>
);
}

function Item() {
const { state } = useStore();
return <div>{JSON.stringify(state)}</div>;
}

const app = create(<Root />);
expect(app.root.findByType(Item).findByType("div").children).toEqual([
JSON.stringify(defaultState)
]);
});

it("renders correctly on method call", () => {
const useStore = createUseTestStore();

function Root() {
return (
<useStore.Provider initialState={defaultState}>
<State />
<ItemWithMethodCall />
</useStore.Provider>
);
}

function State() {
const { state } = useStore();
return <>{JSON.stringify(state)}</>;
}

function ItemWithMethodCall() {
const { methods } = useStore();
return <button onClick={() => methods.setData("test data")}>Op</button>;
}

const app = create(<Root />);
act(() => {
app.root.findByType("button").props.onClick();
});

expect(app.root.findByType(State).children).toEqual([
JSON.stringify({ isLoading: false, data: "test data" })
]);
});
});
7 changes: 7 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript"
]
};
4 changes: 3 additions & 1 deletion example/example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ function StateSlice() {
return useMemo(
() => (
<div>
<p>This component rerenders only on error occurance</p>
<p>
This component rerenders only on state.error changes (error on load)
</p>
<p>{`Rerender counter: ${stateSliceRerenderingCounter++}`}</p>
<button onClick={loadItem(methods)}>
Load item from memo component
Expand Down
Loading

0 comments on commit f969e4f

Please sign in to comment.