1
- // Copyright 2016, Google, Inc.
2
- // Licensed under the Apache License, Version 2.0 (the "License");
3
- // you may not use this file except in compliance with the License.
4
- // You may obtain a copy of the License at
5
- //
6
- // http://www.apache.org/licenses/LICENSE-2.0
7
- //
8
- // Unless required by applicable law or agreed to in writing, software
9
- // distributed under the License is distributed on an "AS IS" BASIS,
10
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
- // See the License for the specific language governing permissions and
12
- // limitations under the License.
1
+ /**
2
+ * Copyright 2016, Google, Inc.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
13
15
14
16
'use strict' ;
15
17
16
- var proxyquire = require ( ' proxyquire' ) . noCallThru ( ) ;
18
+ const proxyquire = require ( ` proxyquire` ) . noCallThru ( ) ;
17
19
18
20
function getSample ( ) {
19
- var requestPromise = sinon . stub ( ) . returns ( new Promise ( function ( resolve ) {
20
- resolve ( 'test' ) ;
21
- } ) ) ;
21
+ const requestPromise = sinon . stub ( ) . returns ( Promise . resolve ( `test` ) ) ;
22
+
22
23
return {
23
- sample : proxyquire ( ' ../' , {
24
+ program : proxyquire ( ` ../` , {
24
25
'request-promise' : requestPromise
25
26
} ) ,
26
27
mocks : {
@@ -29,62 +30,64 @@ function getSample () {
29
30
} ;
30
31
}
31
32
32
- function getMockContext ( ) {
33
- return {
34
- success : sinon . stub ( ) ,
35
- failure : sinon . stub ( )
36
- } ;
37
- }
33
+ describe ( `functions:background` , ( ) => {
34
+ it ( `should echo message` , ( ) => {
35
+ const event = {
36
+ payload : {
37
+ myMessage : `hi`
38
+ }
39
+ } ;
40
+ const sample = getSample ( ) ;
41
+ const callback = sinon . stub ( ) ;
38
42
39
- describe ( 'functions:background' , function ( ) {
40
- it ( 'should echo message' , function ( ) {
41
- var expectedMsg = 'hi' ;
42
- var context = getMockContext ( ) ;
43
- var backgroundSample = getSample ( ) ;
44
- backgroundSample . sample . helloWorld ( context , {
45
- message : expectedMsg
46
- } ) ;
43
+ sample . program . helloWorld ( event , callback ) ;
47
44
48
- assert ( context . success . calledOnce ) ;
49
- assert . equal ( context . failure . called , false ) ;
50
- assert ( console . log . calledWith ( expectedMsg ) ) ;
45
+ assert . equal ( console . log . callCount , 1 ) ;
46
+ assert . deepEqual ( console . log . firstCall . args , [ event . payload . myMessage ] ) ;
47
+ assert . equal ( callback . callCount , 1 ) ;
48
+ assert . deepEqual ( callback . firstCall . args , [ ] ) ;
51
49
} ) ;
52
- it ( 'should say no message was provided' , function ( ) {
53
- var expectedMsg = 'No message defined!' ;
54
- var context = getMockContext ( ) ;
55
- var backgroundSample = getSample ( ) ;
56
- backgroundSample . sample . helloWorld ( context , { } ) ;
57
50
58
- assert ( context . failure . calledOnce ) ;
59
- assert ( context . failure . firstCall . args [ 0 ] === expectedMsg ) ;
60
- assert . equal ( context . success . called , false ) ;
51
+ it ( `should say no message was provided` , ( ) => {
52
+ const error = new Error ( `No message defined!` ) ;
53
+ const callback = sinon . stub ( ) ;
54
+ const sample = getSample ( ) ;
55
+ sample . program . helloWorld ( { payload : { } } , callback ) ;
56
+
57
+ assert . equal ( callback . callCount , 1 ) ;
58
+ assert . deepEqual ( callback . firstCall . args , [ error ] ) ;
61
59
} ) ;
62
- it ( 'should make a promise request' , function ( done ) {
63
- var backgroundSample = getSample ( ) ;
64
- backgroundSample . sample . helloPromise ( {
65
- endpoint : 'foo.com'
66
- } ) . then ( function ( result ) {
67
- assert . deepEqual ( backgroundSample . mocks . requestPromise . firstCall . args [ 0 ] , {
68
- uri : 'foo.com'
60
+
61
+ it ( `should make a promise request` , ( ) => {
62
+ const sample = getSample ( ) ;
63
+ const event = {
64
+ payload : {
65
+ endpoint : `foo.com`
66
+ }
67
+ } ;
68
+
69
+ return sample . program . helloPromise ( event )
70
+ . then ( ( result ) => {
71
+ assert . deepEqual ( sample . mocks . requestPromise . firstCall . args , [ { uri : `foo.com` } ] ) ;
72
+ assert . equal ( result , `test` ) ;
69
73
} ) ;
70
- assert . equal ( result , 'test' ) ;
71
- done ( ) ;
72
- } , function ( ) {
73
- assert . fail ( ) ;
74
- } ) ;
75
74
} ) ;
76
- it ( 'should return synchronously' , function ( ) {
77
- var backgroundSample = getSample ( ) ;
78
- assert ( backgroundSample . sample . helloSynchronous ( {
79
- something : true
80
- } ) === 'Something is true!' ) ;
75
+
76
+ it ( `should return synchronously` , ( ) => {
77
+ assert . equal ( getSample ( ) . program . helloSynchronous ( {
78
+ payload : {
79
+ something : true
80
+ }
81
+ } ) , `Something is true!` ) ;
81
82
} ) ;
82
- it ( 'should throw an error' , function ( ) {
83
- var backgroundSample = getSample ( ) ;
84
- assert . throws ( function ( ) {
85
- backgroundSample . sample . helloSynchronous ( {
86
- something : false
83
+
84
+ it ( `should throw an error` , ( ) => {
85
+ assert . throws ( ( ) => {
86
+ getSample ( ) . program . helloSynchronous ( {
87
+ payload : {
88
+ something : false
89
+ }
87
90
} ) ;
88
- } , Error , ' Something was not true!' ) ;
91
+ } , Error , ` Something was not true!` ) ;
89
92
} ) ;
90
93
} ) ;
0 commit comments