Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit482e3c4

Browse files
committed
Update actions to handle thunks
1 parenta4c0a56 commit482e3c4

File tree

8 files changed

+109
-16
lines changed

8 files changed

+109
-16
lines changed

‎src/actions/Create.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importImmutablefrom'immutable'
22
importBaseActionfrom'./BaseAction'
3+
import{isPromise}from'../utils/is'
34

45
/**
56
* Class representing a create action.
@@ -40,14 +41,17 @@ export class Create extends BaseAction {
4041
this.start(dispatch,data)
4142

4243
// If config.creator is provided, call it
43-
if(isAsync){
44+
if(isAsync){
4445
// Prepare BaseAction.success and BaseAction.error handlers
4546
// by currying with dispatch and action data
4647
constsuccess=this.success.bind(this,dispatch,data)
4748
consterror=this.error.bind(this,dispatch,data)
4849

4950
// Call creator
50-
returncreator(record,success,error)
51+
constcreatorAction=creator(record,success,error)
52+
53+
// Return the action promise
54+
returnisPromise(creatorAction) ?creatorAction :creatorAction(dispatch)
5155
}
5256
}
5357
}

‎src/actions/Fetch.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importImmutablefrom'immutable'
22
importBaseActionfrom'./BaseAction'
3+
import{isPromise}from'../utils/is'
34

45
/**
56
* Class representing a fetch action.
@@ -50,14 +51,17 @@ export class Fetch extends BaseAction {
5051
this.start(dispatch,data)
5152

5253
// If config.fetcher is provided, call it
53-
if(typeofthis.config.fetcher==='function'){
54+
if(typeofthis.config.fetcher==='function'){
5455
// Prepare BaseAction.success and BaseAction.error handlers
5556
// by currying with dispatch
5657
constsuccess=this.success.bind(this,dispatch,data)
5758
consterror=this.error.bind(this,dispatch,data)
5859

5960
// Call fetcher
60-
returnthis.config.fetcher(params,success,error)
61+
constfetcherAction=this.config.fetcher(params,success,error)
62+
63+
// Return the action promise
64+
returnisPromise(fetcherAction) ?fetcherAction :fetcherAction(dispatch)
6165
}
6266
}
6367
}

‎src/actions/Remove.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importBaseActionfrom'./BaseAction'
2+
import{isPromise}from'../utils/is'
23

34
/**
45
* Class representing a remove action.
@@ -31,14 +32,17 @@ export class Remove extends BaseAction {
3132
this.start(dispatch,data)
3233

3334
// If config.remover is provided, call it
34-
if(isAsync){
35+
if(isAsync){
3536
// Prepare BaseAction.success and BaseAction.error handlers
3637
// by currying with dispatch
3738
constsuccess=this.success.bind(this,dispatch,data)
3839
consterror=this.error.bind(this,dispatch,data)
3940

4041
// Call remover
41-
returnremover(uid,success,error)
42+
constremoverAction=remover(uid,success,error)
43+
44+
// Return the action promise
45+
returnisPromise(removerAction) ?removerAction :removerAction(dispatch)
4246
}
4347
}
4448
}

‎src/actions/Update.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
importImmutablefrom'immutable'
22
importBaseActionfrom'./BaseAction'
3+
import{isPromise}from'../utils/is'
34

45
/**
56
* Class representing a update action.
@@ -36,14 +37,17 @@ export class Update extends BaseAction {
3637
this.start(dispatch,data)
3738

3839
// If config.updater is provided, call it
39-
if(isAsync){
40+
if(isAsync){
4041
// Prepare BaseAction.success and BaseAction.error handlers
4142
// by currying with dispatch
4243
constsuccess=this.success.bind(this,dispatch,data)
4344
consterror=this.error.bind(this,dispatch,data)
4445

4546
// Call updater
46-
returnupdater(record,success,error)
47+
constupdaterAction=updater(record,success,error)
48+
49+
// Return the action promise
50+
returnisPromise(updaterAction) ?updaterAction :updaterAction(dispatch)
4751
}
4852
}
4953
}

‎test/actions/Create.js‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Create } from '../../src/actions/Create'
55

66
describe('Actions::Create',()=>{
77

8-
letdispatchSpy
8+
letdispatchSpy,configDispatch
99

1010
constconfigBase={
1111
actionPrefix:'test',
@@ -27,11 +27,21 @@ describe('Actions::Create', () => {
2727
})
2828

2929
constconfigSpy=Object.assign({},configBase,{
30-
creator:sinon.spy()
30+
creator:sinon.stub().returns(Promise.resolve())
3131
})
3232

3333
beforeEach(()=>{
3434
dispatchSpy=sinon.spy()
35+
36+
configDispatch=Object.assign({},configBase,{
37+
creator:(data,success)=>{
38+
return(dispatch)=>{
39+
success({uid:123,name:'test'})
40+
dispatch('test')
41+
returnPromise.resolve()
42+
}
43+
}
44+
})
3545
})
3646

3747
describe('do',()=>{
@@ -149,5 +159,15 @@ describe('Actions::Create', () => {
149159
done()
150160
})
151161
})
162+
163+
it('should handle thunk',done=>{
164+
constaction=newCreate(configDispatch)
165+
constdata=Map({uid:'new',name:'test'})
166+
action.do('users',data)(dispatchSpy).then(()=>{
167+
expect(dispatchSpy.callCount).to.equal(7)
168+
expect(dispatchSpy.calledWithExactly('test')).to.be.true
169+
done()
170+
})
171+
})
152172
})
153173
})

‎test/actions/Fetch.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Fetch } from '../../src/actions/Fetch'
55

66
describe('Actions::Fetch',()=>{
77

8-
letdispatchSpy
8+
letdispatchSpy,configDispatch
99

1010
constconfigBase={
1111
actionPrefix:'test',
@@ -27,11 +27,21 @@ describe('Actions::Fetch', () => {
2727
})
2828

2929
constconfigSpy=Object.assign({},configBase,{
30-
fetcher:sinon.spy()
30+
fetcher:sinon.stub().returns(Promise.resolve())
3131
})
3232

3333
beforeEach(()=>{
3434
dispatchSpy=sinon.spy()
35+
36+
configDispatch=Object.assign({},configBase,{
37+
fetcher:(params,success)=>{
38+
return(dispatch)=>{
39+
success([{uid:123,name:'test'}])
40+
dispatch('test')
41+
returnPromise.resolve()
42+
}
43+
}
44+
})
3545
})
3646

3747
describe('do',()=>{
@@ -137,5 +147,14 @@ describe('Actions::Fetch', () => {
137147
done()
138148
})
139149
})
150+
151+
it('should handle thunk',done=>{
152+
constaction=newFetch(configDispatch)
153+
action.do('users')(dispatchSpy).then(()=>{
154+
expect(dispatchSpy.callCount).to.equal(7)
155+
expect(dispatchSpy.calledWithExactly('test')).to.be.true
156+
done()
157+
})
158+
})
140159
})
141160
})

‎test/actions/Remove.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Remove } from '../../src/actions/Remove'
44

55
describe('Actions::Remove',()=>{
66

7-
letdispatchSpy
7+
letdispatchSpy,configDispatch
88

99
constconfigBase={
1010
actionPrefix:'test',
@@ -26,11 +26,21 @@ describe('Actions::Remove', () => {
2626
})
2727

2828
constconfigSpy=Object.assign({},configBase,{
29-
remover:sinon.spy()
29+
remover:sinon.stub().returns(Promise.resolve())
3030
})
3131

3232
beforeEach(()=>{
3333
dispatchSpy=sinon.spy()
34+
35+
configDispatch=Object.assign({},configBase,{
36+
remover:(data,success,error)=>{
37+
return(dispatch)=>{
38+
error({error:'test'})
39+
dispatch('test')
40+
returnPromise.resolve()
41+
}
42+
}
43+
})
3444
})
3545

3646
describe('do',()=>{
@@ -125,5 +135,14 @@ describe('Actions::Remove', () => {
125135
done()
126136
})
127137
})
138+
139+
it('should handle thunk',done=>{
140+
constaction=newRemove(configDispatch)
141+
action.do(123)(dispatchSpy).then(()=>{
142+
expect(dispatchSpy.callCount).to.equal(7)
143+
expect(dispatchSpy.calledWithExactly('test')).to.be.true
144+
done()
145+
})
146+
})
128147
})
129148
})

‎test/actions/Update.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Update } from '../../src/actions/Update'
55

66
describe('Actions::Update',()=>{
77

8-
letdispatchSpy
8+
letdispatchSpy,configDispatch
99

1010
constconfigBase={
1111
actionPrefix:'test',
@@ -27,11 +27,21 @@ describe('Actions::Update', () => {
2727
})
2828

2929
constconfigSpy=Object.assign({},configBase,{
30-
updater:sinon.spy()
30+
updater:sinon.stub().returns(Promise.resolve())
3131
})
3232

3333
beforeEach(()=>{
3434
dispatchSpy=sinon.spy()
35+
36+
configDispatch=Object.assign({},configBase,{
37+
updater:(data,success)=>{
38+
return(dispatch)=>{
39+
success({uid:123,name:'test'})
40+
dispatch('test')
41+
returnPromise.resolve()
42+
}
43+
}
44+
})
3545
})
3646

3747
describe('do',()=>{
@@ -137,5 +147,14 @@ describe('Actions::Update', () => {
137147
done()
138148
})
139149
})
150+
151+
it('should handle thunk',done=>{
152+
constaction=newUpdate(configDispatch)
153+
action.do(Map({uid:123,name:'test'}))(dispatchSpy).then(()=>{
154+
expect(dispatchSpy.callCount).to.equal(7)
155+
expect(dispatchSpy.calledWithExactly('test')).to.be.true
156+
done()
157+
})
158+
})
140159
})
141160
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp