-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 2 tests: default state, method use
- Loading branch information
Dmitry Dushkin
committed
May 9, 2019
1 parent
e76a71e
commit f969e4f
Showing
5 changed files
with
2,189 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }) | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.