Skip to content

Commit 3720269

Browse files
committed
Merge pull request #1165 from jneto/fix-update-widget-api
Fix update widget api -- THANKS!
2 parents 38e99f0 + 13c6808 commit 3720269

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

api/actions/__tests__/widget-update-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {expect} from 'chai';
22
import update from '../widget/update';
3+
import * as load from '../widget/load';
34
import sinon from 'sinon';
45

56
describe('widget update', () => {
@@ -10,11 +11,22 @@ describe('widget update', () => {
1011
});
1112

1213
describe('randomly successful', () => {
14+
const widgets = [{}, {id: 2, color: 'Red'}];
15+
1316
beforeEach(()=> {
1417
sinon.stub(Math, 'random').returns(0.3);
1518
});
1619

20+
afterEach(()=> {
21+
if ('restore' in load.default) {
22+
load.default.restore();
23+
}
24+
});
25+
1726
it('does not accept green widgets', () => {
27+
sinon.stub(load, 'default').returns(new Promise((resolve) => {
28+
resolve(widgets);
29+
}));
1830
return update({session: {}, body: {color: 'Green'}}).
1931
then(
2032
()=> {
@@ -24,12 +36,29 @@ describe('widget update', () => {
2436
});
2537
});
2638

39+
it('fails to load widgets', () => {
40+
sinon.stub(load, 'default').returns(new Promise((resolve, reject) => {
41+
reject('Widget fail to load.');
42+
}));
43+
return update({session: {}, body: {color: 'Blue'}}).
44+
then(
45+
()=> {
46+
},
47+
(err)=> {
48+
expect(err).to.equal('Widget fail to load.');
49+
});
50+
});
51+
2752
it('updates a widget', () => {
53+
sinon.stub(load, 'default').returns(new Promise((resolve) => {
54+
resolve(widgets);
55+
}));
2856
const widget = {id: 2, color: 'Blue'};
2957
return update({session: {}, body: widget}).
3058
then(
3159
(res)=> {
3260
expect(res).to.deep.equal(widget);
61+
expect(widgets[1]).to.deep.equal(widget);
3362
});
3463
});
3564
});

api/actions/widget/update.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ export default function update(req) {
77
if (Math.random() < 0.2) {
88
reject('Oh no! Widget save fails 20% of the time. Try again.');
99
} else {
10-
const widgets = load(req);
11-
const widget = req.body;
12-
if (widget.color === 'Green') {
13-
reject({
14-
color: 'We do not accept green widgets' // example server-side validation error
15-
});
16-
}
17-
if (widget.id) {
18-
widgets[widget.id - 1] = widget; // id is 1-based. please don't code like this in production! :-)
19-
}
20-
resolve(widget);
10+
load(req).then(data => {
11+
const widgets = data;
12+
const widget = req.body;
13+
if (widget.color === 'Green') {
14+
reject({
15+
color: 'We do not accept green widgets' // example server-side validation error
16+
});
17+
}
18+
if (widget.id) {
19+
widgets[widget.id - 1] = widget; // id is 1-based. please don't code like this in production! :-)
20+
req.session.widgets = widgets;
21+
}
22+
resolve(widget);
23+
}, err => {
24+
reject(err);
25+
});
2126
}
2227
}, 1500); // simulate async db write
2328
});

0 commit comments

Comments
 (0)