1
1
import { expect } from 'chai' ;
2
2
import update from '../widget/update' ;
3
+ import * as load from '../widget/load' ;
3
4
import sinon from 'sinon' ;
4
5
5
6
describe ( 'widget update' , ( ) => {
@@ -10,11 +11,22 @@ describe('widget update', () => {
10
11
} ) ;
11
12
12
13
describe ( 'randomly successful' , ( ) => {
14
+ const widgets = [ { } , { id : 2 , color : 'Red' } ] ;
15
+
13
16
beforeEach ( ( ) => {
14
17
sinon . stub ( Math , 'random' ) . returns ( 0.3 ) ;
15
18
} ) ;
16
19
20
+ afterEach ( ( ) => {
21
+ if ( 'restore' in load . default ) {
22
+ load . default . restore ( ) ;
23
+ }
24
+ } ) ;
25
+
17
26
it ( 'does not accept green widgets' , ( ) => {
27
+ sinon . stub ( load , 'default' ) . returns ( new Promise ( ( resolve ) => {
28
+ resolve ( widgets ) ;
29
+ } ) ) ;
18
30
return update ( { session : { } , body : { color : 'Green' } } ) .
19
31
then (
20
32
( ) => {
@@ -24,12 +36,29 @@ describe('widget update', () => {
24
36
} ) ;
25
37
} ) ;
26
38
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
+
27
52
it ( 'updates a widget' , ( ) => {
53
+ sinon . stub ( load , 'default' ) . returns ( new Promise ( ( resolve ) => {
54
+ resolve ( widgets ) ;
55
+ } ) ) ;
28
56
const widget = { id : 2 , color : 'Blue' } ;
29
57
return update ( { session : { } , body : widget } ) .
30
58
then (
31
59
( res ) => {
32
60
expect ( res ) . to . deep . equal ( widget ) ;
61
+ expect ( widgets [ 1 ] ) . to . deep . equal ( widget ) ;
33
62
} ) ;
34
63
} ) ;
35
64
} ) ;
0 commit comments