Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
fix: enable to use even if initialstate is unloaded at store
Browse files Browse the repository at this point in the history
  • Loading branch information
tkow committed Nov 3, 2018
1 parent feb13c6 commit 5dd8629
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"opn-cli": "^3.1.0",
"prettier": "^1.14.3",
"redux": "^4.0.1",
"redux-actions": "^2.6.4",
"standard-version": "^4.4.0",
"trash-cli": "^1.4.0",
"tslint": "^5.11.0",
Expand Down
28 changes: 28 additions & 0 deletions src/lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from "ava";
import { combineReducers, createStore } from "redux";
import { handleActions } from "redux-actions";
import { isNumber } from "util";
import { watchRootReducer, withValidateReducer } from "./index";

Expand All @@ -26,6 +27,13 @@ const postalReducer = (
return state;
};

const initialStateUndefinedReducer = handleActions(
{
SET_NUMBER: () => 123
},
0
);

const _validateReducer = withValidateReducer(postalReducer, [
{
error: {
Expand All @@ -40,6 +48,26 @@ const identityReducer = (state: string = "hoge", action: { value: string }) => {
return action && action.value ? action.value : state;
};

test("initial state not filter if the condition is invalid", async t => {
const rootReducer = watchRootReducer(
combineReducers({
postalCode: withValidateReducer(initialStateUndefinedReducer, [
{
error: {
id: "postalCode",
message: "Invalid PostalCode"
},
validate: _state => Number(_state) > 100
}
])
})
);
const store = createStore(rootReducer);
const state = store.getState();
t.truthy(Object.keys(state.errors).length === 0);
t.truthy(state.postalCode === 0);
});

test("whether combineReducers can validate for object", async t => {
const _validateNestReducer = withValidateReducer(postalReducer, [
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ValidationWatcher {
const next = reducer(prev, action);
if (
validators.some(validator => {
const invalid = !validator.validate(next);
const invalid = !validator.validate(next) && prev !== undefined;
if (invalid) {
this.withError(validator.error);
}
Expand Down
36 changes: 35 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ interpret@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"

invariant@^2.2.2:
invariant@^2.2.2, invariant@^2.2.4:
version "2.2.4"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
dependencies:
Expand Down Expand Up @@ -2810,6 +2810,10 @@ jsprim@^1.2.2:
json-schema "0.2.3"
verror "1.10.0"

just-curry-it@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/just-curry-it/-/just-curry-it-3.1.0.tgz#ab59daed308a58b847ada166edd0a2d40766fbc5"

kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
Expand Down Expand Up @@ -3916,6 +3920,20 @@ redent@^2.0.0:
indent-string "^3.0.0"
strip-indent "^2.0.0"

reduce-reducers@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/reduce-reducers/-/reduce-reducers-0.4.3.tgz#8e052618801cd8fc2714b4915adaa8937eb6d66c"

redux-actions@^2.6.4:
version "2.6.4"
resolved "https://registry.yarnpkg.com/redux-actions/-/redux-actions-2.6.4.tgz#e1d9d7d987d274071b0134b707365d3e25ba3b26"
dependencies:
invariant "^2.2.4"
just-curry-it "^3.1.0"
loose-envify "^1.4.0"
reduce-reducers "^0.4.3"
to-camel-case "^1.0.0"

redux@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"
Expand Down Expand Up @@ -4550,6 +4568,12 @@ timed-out@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/timed-out/-/timed-out-4.0.1.tgz#f32eacac5a175bea25d7fab565ab3ed8741ef56f"

to-camel-case@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/to-camel-case/-/to-camel-case-1.0.0.tgz#1a56054b2f9d696298ce66a60897322b6f423e46"
dependencies:
to-space-case "^1.0.0"

to-fast-properties@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-1.0.3.tgz#b83571fa4d8c25b82e231b06e3a3055de4ca1a47"
Expand All @@ -4558,6 +4582,10 @@ to-fast-properties@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"

to-no-case@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/to-no-case/-/to-no-case-1.0.2.tgz#c722907164ef6b178132c8e69930212d1b4aa16a"

to-object-path@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/to-object-path/-/to-object-path-0.3.0.tgz#297588b7b0e7e0ac08e04e672f85c1f4999e17af"
Expand All @@ -4580,6 +4608,12 @@ to-regex@^3.0.1, to-regex@^3.0.2:
regex-not "^1.0.2"
safe-regex "^1.1.0"

to-space-case@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/to-space-case/-/to-space-case-1.0.0.tgz#b052daafb1b2b29dc770cea0163e5ec0ebc9fc17"
dependencies:
to-no-case "^1.0.0"

tough-cookie@~2.4.3:
version "2.4.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.4.3.tgz#53f36da3f47783b0925afa06ff9f3b165280f781"
Expand Down

0 comments on commit 5dd8629

Please # to comment.