|
| 1 | +/** |
| 2 | + * Test decorator parser |
| 3 | + * |
| 4 | + * @group unit/parser |
| 5 | + */ |
| 6 | + |
| 7 | +import { LambdaInterface } from '@aws-lambda-powertools/commons/lib/esm/types'; |
| 8 | +import { Context, EventBridgeEvent } from 'aws-lambda'; |
| 9 | +import { parser } from '../../src/parser'; |
| 10 | +import { TestSchema, TestEvents } from './schema/utils'; |
| 11 | +import { generateMock } from '@anatine/zod-mock'; |
| 12 | +import { eventBridgeEnvelope } from '../../src/envelopes/event-bridge'; |
| 13 | +import { EventBridgeSchema } from '../../src/schemas/eventbridge'; |
| 14 | +import { z } from 'zod'; |
| 15 | + |
| 16 | +describe('Parser Decorator', () => { |
| 17 | + const customEventBridgeSchema = EventBridgeSchema.extend({ |
| 18 | + detail: TestSchema, |
| 19 | + }); |
| 20 | + |
| 21 | + type TestSchema = z.infer<typeof TestSchema>; |
| 22 | + |
| 23 | + class TestClass implements LambdaInterface { |
| 24 | + @parser({ schema: TestSchema }) |
| 25 | + public async handler( |
| 26 | + event: TestSchema, |
| 27 | + _context: Context |
| 28 | + ): Promise<unknown> { |
| 29 | + return event; |
| 30 | + } |
| 31 | + |
| 32 | + @parser({ schema: customEventBridgeSchema }) |
| 33 | + public async handlerWithCustomSchema( |
| 34 | + event: unknown, |
| 35 | + _context: Context |
| 36 | + ): Promise<unknown> { |
| 37 | + return event; |
| 38 | + } |
| 39 | + |
| 40 | + @parser({ schema: TestSchema, envelope: eventBridgeEnvelope }) |
| 41 | + public async handlerWithParserCallsAnotherMethod( |
| 42 | + event: unknown, |
| 43 | + _context: Context |
| 44 | + ): Promise<unknown> { |
| 45 | + return this.anotherMethod(event as TestSchema); |
| 46 | + } |
| 47 | + |
| 48 | + @parser({ envelope: eventBridgeEnvelope, schema: TestSchema }) |
| 49 | + public async handlerWithSchemaAndEnvelope( |
| 50 | + event: unknown, |
| 51 | + _context: Context |
| 52 | + ): Promise<unknown> { |
| 53 | + return event; |
| 54 | + } |
| 55 | + |
| 56 | + private async anotherMethod(event: TestSchema): Promise<TestSchema> { |
| 57 | + return event; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const lambda = new TestClass(); |
| 62 | + |
| 63 | + it('should parse custom schema event', async () => { |
| 64 | + const testEvent = generateMock(TestSchema); |
| 65 | + |
| 66 | + const resp = await lambda.handler(testEvent, {} as Context); |
| 67 | + |
| 68 | + expect(resp).toEqual(testEvent); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should parse custom schema with envelope event', async () => { |
| 72 | + const customPayload = generateMock(TestSchema); |
| 73 | + const testEvent = TestEvents.eventBridgeEvent as EventBridgeEvent< |
| 74 | + string, |
| 75 | + unknown |
| 76 | + >; |
| 77 | + testEvent.detail = customPayload; |
| 78 | + |
| 79 | + const resp = await lambda.handlerWithSchemaAndEnvelope( |
| 80 | + testEvent, |
| 81 | + {} as Context |
| 82 | + ); |
| 83 | + |
| 84 | + expect(resp).toEqual(customPayload); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should parse extended envelope event', async () => { |
| 88 | + const customPayload = generateMock(TestSchema); |
| 89 | + |
| 90 | + const testEvent = generateMock(customEventBridgeSchema); |
| 91 | + testEvent.detail = customPayload; |
| 92 | + |
| 93 | + const resp: z.infer<typeof customEventBridgeSchema> = |
| 94 | + (await lambda.handlerWithCustomSchema( |
| 95 | + testEvent, |
| 96 | + {} as Context |
| 97 | + )) as z.infer<typeof customEventBridgeSchema>; |
| 98 | + |
| 99 | + expect(customEventBridgeSchema.parse(resp)).toEqual(testEvent); |
| 100 | + expect(resp.detail).toEqual(customPayload); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should parse and call private async method', async () => { |
| 104 | + const customPayload = generateMock(TestSchema); |
| 105 | + const testEvent = TestEvents.eventBridgeEvent as EventBridgeEvent< |
| 106 | + string, |
| 107 | + unknown |
| 108 | + >; |
| 109 | + testEvent.detail = customPayload; |
| 110 | + |
| 111 | + const resp = await lambda.handlerWithParserCallsAnotherMethod( |
| 112 | + testEvent, |
| 113 | + {} as Context |
| 114 | + ); |
| 115 | + |
| 116 | + expect(resp).toEqual(customPayload); |
| 117 | + }); |
| 118 | +}); |
0 commit comments