@@ -5,19 +5,28 @@ import { SQSEvent } from 'aws-lambda';
5
5
import * as Sentry from '@sentry/serverless' ;
6
6
7
7
describe ( 'event handlers' , ( ) => {
8
- let deleteStub : sinon . SinonStub ;
8
+ let accountDeleteStub : sinon . SinonStub ;
9
+ let saveDeleteStub : sinon . SinonStub ;
9
10
let sentryStub : sinon . SinonStub ;
11
+
10
12
beforeEach ( ( ) => {
11
13
sinon . restore ( ) ;
12
14
sentryStub = sinon . stub ( Sentry , 'captureException' ) ;
13
15
} ) ;
16
+
14
17
afterAll ( ( ) => sinon . restore ( ) ) ;
18
+
15
19
describe ( 'with no handler errors' , ( ) => {
16
20
beforeEach ( ( ) => {
17
- deleteStub = sinon . stub ( handlers , Event . ACCOUNT_DELETION ) . resolves ( ) ;
21
+ accountDeleteStub = sinon
22
+ . stub ( handlers , Event . ACCOUNT_DELETION )
23
+ . resolves ( ) ;
24
+
25
+ saveDeleteStub = sinon . stub ( handlers , Event . DELETE_ITEM ) . resolves ( ) ;
18
26
} ) ;
27
+
19
28
it ( 'routes to the correct handler function based on detail-type' , async ( ) => {
20
- const records = {
29
+ let records = {
21
30
Records : [
22
31
{
23
32
body : JSON . stringify ( {
@@ -28,32 +37,55 @@ describe('event handlers', () => {
28
37
} ,
29
38
] ,
30
39
} ;
40
+
31
41
await processor ( records as SQSEvent ) ;
32
- expect ( deleteStub . callCount ) . toEqual ( 1 ) ;
33
- expect ( deleteStub . getCall ( 0 ) . args ) . toEqual ( [ records . Records [ 0 ] ] ) ;
42
+ expect ( accountDeleteStub . callCount ) . toEqual ( 1 ) ;
43
+ expect ( accountDeleteStub . getCall ( 0 ) . args ) . toEqual ( [ records . Records [ 0 ] ] ) ;
44
+
45
+ records = {
46
+ Records : [
47
+ {
48
+ body : JSON . stringify ( {
49
+ Message : JSON . stringify ( {
50
+ 'detail-type' : Event . DELETE_ITEM ,
51
+ } ) ,
52
+ } ) ,
53
+ } ,
54
+ ] ,
55
+ } ;
56
+
57
+ await processor ( records as SQSEvent ) ;
58
+ expect ( saveDeleteStub . callCount ) . toEqual ( 1 ) ;
59
+ expect ( saveDeleteStub . getCall ( 0 ) . args ) . toEqual ( [ records . Records [ 0 ] ] ) ;
34
60
} ) ;
35
- it ( 'returns batchItemFailure and logs to Sentry if handler does not exist' , async ( ) => {
61
+ it ( 'is a NOOP if a handler does not exist' , async ( ) => {
36
62
const records = {
37
63
Records : [
38
64
{
39
65
body : JSON . stringify ( {
40
- Message : JSON . stringify ( { 'detail-type' : 'NOT_A_TYPE' } ) ,
66
+ Message : JSON . stringify ( {
67
+ 'detail-type' : 'NOT_A_TYPE_I_CAN_HANDLE' ,
68
+ } ) ,
41
69
} ) ,
42
70
messageId : 'abc' ,
43
71
} ,
44
72
] ,
45
73
} ;
74
+
46
75
const res = await processor ( records as SQSEvent ) ;
47
- expect ( res . batchItemFailures ) . toEqual ( [ { itemIdentifier : 'abc' } ] ) ;
48
- expect ( sentryStub . callCount ) . toEqual ( 1 ) ;
49
- expect ( sentryStub . getCall ( 0 ) . args [ 0 ] . message ) . toEqual (
50
- `Unable to retrieve handler for detail-type='NOT_A_TYPE'`
51
- ) ;
76
+
77
+ // no failures/errors
78
+ expect ( res . batchItemFailures ) . toEqual ( [ ] ) ;
79
+ expect ( sentryStub . callCount ) . toEqual ( 0 ) ;
80
+
81
+ // no handlers were called
82
+ expect ( accountDeleteStub . callCount ) . toEqual ( 0 ) ;
83
+ expect ( saveDeleteStub . callCount ) . toEqual ( 0 ) ;
52
84
} ) ;
53
85
} ) ;
54
86
describe ( 'with handler errors' , ( ) => {
55
87
beforeEach ( ( ) => {
56
- deleteStub = sinon
88
+ accountDeleteStub = sinon
57
89
. stub ( handlers , Event . ACCOUNT_DELETION )
58
90
. rejects ( Error ( 'got an error' ) ) ;
59
91
} ) ;
0 commit comments