Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Merge 1538f23 into 6bb1fde
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelomanov Dmitry committed Mar 27, 2019
2 parents 6bb1fde + 1538f23 commit 3f0a7b0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -59,5 +59,7 @@
"redux-execute": "^2.0.1",
"redux-symbiote": "^3.0.2"
},
"dependencies": {}
"dependencies": {
"ramda.assocpath": "^0.26.1"
}
}
36 changes: 23 additions & 13 deletions src/advanced.js
@@ -1,5 +1,9 @@
const assocPath = require("ramda.assocpath")
const { fetchStatus } = require("./status")

const buildPathForRamda = (path = "", computedData, state) =>
assocPath(path.split("."), computedData, state)

/**
* @typedef {Object} Action
* @prop {string} type
Expand Down Expand Up @@ -28,19 +32,25 @@ function createSymbioteFetcher({
* fetching: createFetching('status'),
* })
*/
const createFetching = (stateProperty) => ({
start: (state) => ({
...state,
[stateProperty]: { [propStatus]: fetchStatus.loading, [propError]: null },
}),
finish: (state) => ({
...state,
[stateProperty]: { [propStatus]: fetchStatus.ready, [propError]: null },
}),
fail: (state, error) => ({
...state,
[stateProperty]: { [propStatus]: fetchStatus.failed, [propError]: error },
}),
const createFetching = (path) => ({
start: (state) =>
buildPathForRamda(
path,
{ [propStatus]: fetchStatus.loading, [propError]: null },
state,
),
finish: (state) =>
buildPathForRamda(
path,
{ [propStatus]: fetchStatus.ready, [propError]: null },
state,
),
fail: (state, error) =>
buildPathForRamda(
path,
{ [propStatus]: fetchStatus.failed, [propError]: error },
state,
),
})

/**
Expand Down
30 changes: 30 additions & 0 deletions test/default/create-fetching.js
Expand Up @@ -88,3 +88,33 @@ test("symbiote correctly updates state", (t) => {
fetching: { status: fetchStatus.failed, error: "SOME_ERROR" },
})
})

test("symbiote correctly updates nested state", (t) => {
const initial = {
fetching: {
news: initialFetching,
},
}
const symbiotes = {
fetch: createFetching("fetching.news"),
}
const { actions, reducer } = createSymbiote(initial, symbiotes)

t.deepEqual(reducer(initial, actions.fetch.start()), {
fetching: {
news: { status: fetchStatus.loading, error: null },
},
})

t.deepEqual(reducer(initial, actions.fetch.finish()), {
fetching: {
news: { status: fetchStatus.ready, error: null },
},
})

t.deepEqual(reducer(initial, actions.fetch.fail("SOME_ERROR")), {
fetching: {
news: { status: fetchStatus.failed, error: "SOME_ERROR" },
},
})
})

0 comments on commit 3f0a7b0

Please sign in to comment.