From f969e4f5b5847c032d6cddec851cee6c1879af85 Mon Sep 17 00:00:00 2001 From: Dmitry Dushkin Date: Thu, 9 May 2019 16:08:42 +0300 Subject: [PATCH] Add 2 tests: default state, method use --- __tests__/create-store.spec.tsx | 77 ++ babel.config.js | 7 + example/example.tsx | 4 +- package-lock.json | 2125 ++++++++++++++++++++++++++++++- package.json | 10 +- 5 files changed, 2189 insertions(+), 34 deletions(-) create mode 100644 __tests__/create-store.spec.tsx create mode 100644 babel.config.js diff --git a/__tests__/create-store.spec.tsx b/__tests__/create-store.spec.tsx new file mode 100644 index 0000000..a37d338 --- /dev/null +++ b/__tests__/create-store.spec.tsx @@ -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 ( + + + + ); + } + + function Item() { + const { state } = useStore(); + return
{JSON.stringify(state)}
; + } + + const app = create(); + expect(app.root.findByType(Item).findByType("div").children).toEqual([ + JSON.stringify(defaultState) + ]); + }); + + it("renders correctly on method call", () => { + const useStore = createUseTestStore(); + + function Root() { + return ( + + + + + ); + } + + function State() { + const { state } = useStore(); + return <>{JSON.stringify(state)}; + } + + function ItemWithMethodCall() { + const { methods } = useStore(); + return ; + } + + const app = create(); + act(() => { + app.root.findByType("button").props.onClick(); + }); + + expect(app.root.findByType(State).children).toEqual([ + JSON.stringify({ isLoading: false, data: "test data" }) + ]); + }); +}); diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..c879c23 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,7 @@ +module.exports = { + presets: [ + "@babel/preset-env", + "@babel/preset-react", + "@babel/preset-typescript" + ] +}; diff --git a/example/example.tsx b/example/example.tsx index 968f3e3..cb8c0f6 100644 --- a/example/example.tsx +++ b/example/example.tsx @@ -58,7 +58,9 @@ function StateSlice() { return useMemo( () => (
-

This component rerenders only on error occurance

+

+ This component rerenders only on state.error changes (error on load) +

{`Rerender counter: ${stateSliceRerenderingCounter++}`}