|
| 1 | +import { KinesisClient } from '@aws-sdk/client-kinesis'; |
| 2 | +import { mockDeep } from 'jest-mock-extended'; |
| 3 | +import { DynamoDBStreamEvent, Context } from 'aws-lambda'; |
| 4 | +import { Deps } from '../deps'; |
| 5 | +import { EnvVars } from '../env'; |
| 6 | +import { createHandler } from '../letter-stream-forwarder'; |
| 7 | + |
| 8 | +describe('letter-stream-forwarder Lambda', () => { |
| 9 | + |
| 10 | + const mockedDeps: jest.Mocked<Deps> = { |
| 11 | + kinesisClient: { send: jest.fn()} as unknown as KinesisClient, |
| 12 | + env: { |
| 13 | + LETTER_CHANGE_STREAM_NAME: "test-stream", |
| 14 | + } as unknown as EnvVars |
| 15 | + } as Deps; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + |
| 22 | + it('forwards status changes to Kinesis', async () => { |
| 23 | + const event: DynamoDBStreamEvent = { |
| 24 | + Records: [ |
| 25 | + { |
| 26 | + eventName: 'MODIFY', |
| 27 | + dynamodb: { |
| 28 | + Keys: { id: { S: '123' } }, |
| 29 | + OldImage: { status: { S: 'PENDING' } }, |
| 30 | + NewImage: { status: { S: 'ACCEPTED' }, id: { S: '123' } }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + ], |
| 34 | + }; |
| 35 | + |
| 36 | + const handler = createHandler(mockedDeps); |
| 37 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 38 | + |
| 39 | + expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith( |
| 40 | + expect.objectContaining({ |
| 41 | + input: expect.objectContaining({ |
| 42 | + StreamName: 'test-stream', |
| 43 | + PartitionKey: '123', |
| 44 | + }), |
| 45 | + }) |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not forward if status did not change', async () => { |
| 50 | + const event: DynamoDBStreamEvent = { |
| 51 | + Records: [ |
| 52 | + { |
| 53 | + eventName: 'MODIFY', |
| 54 | + dynamodb: { |
| 55 | + Keys: { id: { S: '123' } }, |
| 56 | + OldImage: { status: { S: 'PENDING' } }, |
| 57 | + NewImage: { status: { S: 'PENDING' }, id: { S: '123' } }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }; |
| 62 | + |
| 63 | + const handler = createHandler(mockedDeps); |
| 64 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 65 | + |
| 66 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('does not forward non-MODIFY events', async () => { |
| 70 | + const event: DynamoDBStreamEvent = { |
| 71 | + Records: [ |
| 72 | + { |
| 73 | + eventName: 'INSERT', |
| 74 | + dynamodb: { |
| 75 | + Keys: { id: { S: '123' } }, |
| 76 | + NewImage: { status: { S: 'PENDING' }, id: { S: '123' } }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }; |
| 81 | + |
| 82 | + const handler = createHandler(mockedDeps); |
| 83 | + await handler(event, mockDeep<Context>(), jest.fn()); |
| 84 | + |
| 85 | + expect(mockedDeps.kinesisClient.send).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | +}); |
0 commit comments