|
3 | 3 | *
|
4 | 4 | * @group unit/parser
|
5 | 5 | */
|
6 |
| - |
7 | 6 | import { z } from 'zod';
|
8 | 7 | import { JSONStringified } from '../../src/helpers.js';
|
| 8 | +import { AlbSchema } from '../../src/schemas/alb.js'; |
9 | 9 | import {
|
10 |
| - AlbSchema, |
11 | 10 | SnsNotificationSchema,
|
12 | 11 | SnsRecordSchema,
|
13 |
| - SqsRecordSchema, |
14 |
| - SqsSchema, |
15 |
| -} from '../../src/schemas'; |
16 |
| -import type { SnsEvent, SqsEvent } from '../../src/types'; |
17 |
| -import { getTestEvent } from './schema/utils'; |
| 12 | +} from '../../src/schemas/sns.js'; |
| 13 | +import { SqsRecordSchema, SqsSchema } from '../../src/schemas/sqs.js'; |
| 14 | +import type { SnsEvent, SqsEvent } from '../../src/types/schema.js'; |
| 15 | +import { getTestEvent } from './schema/utils.js'; |
| 16 | + |
| 17 | +const bodySchema = z.object({ |
| 18 | + id: z.number(), |
| 19 | + name: z.string(), |
| 20 | + email: z.string().email(), |
| 21 | +}); |
| 22 | +const envelopeSchema = z.object({ |
| 23 | + body: z.string(), |
| 24 | +}); |
| 25 | +const basePayload = { |
| 26 | + id: 1, |
| 27 | + name: 'John Doe', |
| 28 | + |
| 29 | +}; |
18 | 30 |
|
19 | 31 | describe('JSONStringified', () => {
|
20 |
| - const schema = z.object({ |
21 |
| - id: z.number(), |
22 |
| - name: z.string(), |
23 |
| - email: z.string().email(), |
24 |
| - }); |
25 |
| - const baseSchema = z.object({ |
26 |
| - body: z.string(), |
27 |
| - }); |
28 | 32 | it('should return a valid JSON', () => {
|
| 33 | + // Prepare |
29 | 34 | const data = {
|
30 |
| - body: JSON.stringify({ |
31 |
| - id: 1, |
32 |
| - name: 'John Doe', |
33 |
| - |
34 |
| - }), |
| 35 | + body: JSON.stringify(structuredClone(basePayload)), |
35 | 36 | };
|
36 | 37 |
|
37 |
| - const extendedSchema = baseSchema.extend({ |
38 |
| - body: JSONStringified(schema), |
| 38 | + // Act |
| 39 | + const extendedSchema = envelopeSchema.extend({ |
| 40 | + body: JSONStringified(bodySchema), |
39 | 41 | });
|
40 | 42 |
|
41 |
| - const result = extendedSchema.parse(data); |
42 |
| - expect(result).toEqual({ |
43 |
| - body: { id: 1, name: 'John Doe', email: '[email protected]' }, |
| 43 | + // Assess |
| 44 | + expect(extendedSchema.parse(data)).toStrictEqual({ |
| 45 | + body: basePayload, |
44 | 46 | });
|
45 | 47 | });
|
46 | 48 |
|
47 | 49 | it('should throw an error if the JSON payload is invalid', () => {
|
| 50 | + // Prepare |
48 | 51 | const data = {
|
49 |
| - body: JSON.stringify({ |
50 |
| - id: 1, |
51 |
| - name: 'John Doe', |
52 |
| - email: 'foo', |
53 |
| - }), |
| 52 | + body: JSON.stringify({ ...basePayload, email: 'invalid' }), |
54 | 53 | };
|
55 | 54 |
|
56 |
| - const extendedSchema = baseSchema.extend({ |
57 |
| - body: JSONStringified(schema), |
| 55 | + // Act |
| 56 | + const extendedSchema = envelopeSchema.extend({ |
| 57 | + body: JSONStringified(bodySchema), |
58 | 58 | });
|
59 | 59 |
|
| 60 | + // Assess |
60 | 61 | expect(() => extendedSchema.parse(data)).toThrow();
|
61 | 62 | });
|
62 | 63 |
|
63 | 64 | it('should throw an error if the JSON is malformed', () => {
|
| 65 | + // Prepare |
64 | 66 | const data = {
|
65 | 67 | body: 'invalid',
|
66 | 68 | };
|
67 | 69 |
|
68 |
| - const extendedSchema = baseSchema.extend({ |
69 |
| - body: JSONStringified(schema), |
| 70 | + // Act |
| 71 | + const extendedSchema = envelopeSchema.extend({ |
| 72 | + body: JSONStringified(bodySchema), |
70 | 73 | });
|
71 | 74 |
|
| 75 | + // Assess |
72 | 76 | expect(() => extendedSchema.parse(data)).toThrow();
|
73 | 77 | });
|
74 | 78 |
|
75 |
| - describe('should parse common built-in schemas', () => { |
76 |
| - const customSchema = z.object({ |
77 |
| - id: z.number(), |
78 |
| - name: z.string(), |
79 |
| - email: z.string().email(), |
| 79 | + it('should parse extended AlbSchema', () => { |
| 80 | + // Prepare |
| 81 | + const testEvent = getTestEvent({ |
| 82 | + eventsPath: '.', |
| 83 | + filename: 'albEvent', |
80 | 84 | });
|
| 85 | + testEvent.body = JSON.stringify(structuredClone(basePayload)); |
81 | 86 |
|
82 |
| - const payload = { |
83 |
| - id: 1, |
84 |
| - name: 'John Doe', |
85 |
| - |
86 |
| - }; |
| 87 | + // Act |
| 88 | + const extendedSchema = AlbSchema.extend({ |
| 89 | + body: JSONStringified(bodySchema), |
| 90 | + }); |
87 | 91 |
|
88 |
| - it('should parse extended AlbSchema', () => { |
89 |
| - const extendedSchema = AlbSchema.extend({ |
90 |
| - body: JSONStringified(customSchema), |
91 |
| - }); |
92 |
| - |
93 |
| - const testEvent = getTestEvent({ |
94 |
| - eventsPath: '.', |
95 |
| - filename: 'albEvent', |
96 |
| - }); |
97 |
| - testEvent.body = JSON.stringify(payload); |
98 |
| - |
99 |
| - const result = extendedSchema.parse(testEvent); |
100 |
| - expect(result).toEqual({ |
101 |
| - ...testEvent, |
102 |
| - body: payload, |
103 |
| - }); |
| 92 | + // Assess |
| 93 | + expect(extendedSchema.parse(testEvent)).toStrictEqual({ |
| 94 | + ...testEvent, |
| 95 | + body: basePayload, |
104 | 96 | });
|
| 97 | + }); |
105 | 98 |
|
106 |
| - it('should parse extended SqsSchema', () => { |
107 |
| - const extendedSchema = SqsSchema.extend({ |
108 |
| - Records: z.array( |
109 |
| - SqsRecordSchema.extend({ |
110 |
| - body: JSONStringified(customSchema), |
111 |
| - }) |
112 |
| - ), |
113 |
| - }); |
114 |
| - |
115 |
| - const testEvent = getTestEvent<SqsEvent>({ |
116 |
| - eventsPath: '.', |
117 |
| - filename: 'sqsEvent', |
118 |
| - }); |
119 |
| - testEvent.Records[0].body = JSON.stringify(payload); |
120 |
| - testEvent.Records[1].body = JSON.stringify(payload); |
121 |
| - |
122 |
| - const result = extendedSchema.parse(testEvent); |
123 |
| - expect(result).toEqual({ |
124 |
| - ...testEvent, |
125 |
| - Records: [ |
126 |
| - { ...testEvent.Records[0], body: payload }, |
127 |
| - { ...testEvent.Records[1], body: payload }, |
128 |
| - ], |
129 |
| - }); |
| 99 | + it('should parse extended SqsSchema', () => { |
| 100 | + // Prepare |
| 101 | + const testEvent = getTestEvent<SqsEvent>({ |
| 102 | + eventsPath: '.', |
| 103 | + filename: 'sqsEvent', |
| 104 | + }); |
| 105 | + const stringifiedBody = JSON.stringify(basePayload); |
| 106 | + testEvent.Records[0].body = stringifiedBody; |
| 107 | + testEvent.Records[1].body = stringifiedBody; |
| 108 | + |
| 109 | + // Act |
| 110 | + const extendedSchema = SqsSchema.extend({ |
| 111 | + Records: z.array( |
| 112 | + SqsRecordSchema.extend({ |
| 113 | + body: JSONStringified(bodySchema), |
| 114 | + }) |
| 115 | + ), |
| 116 | + }); |
| 117 | + |
| 118 | + // Assess |
| 119 | + expect(extendedSchema.parse(testEvent)).toStrictEqual({ |
| 120 | + ...testEvent, |
| 121 | + Records: [ |
| 122 | + { ...testEvent.Records[0], body: basePayload }, |
| 123 | + { ...testEvent.Records[1], body: basePayload }, |
| 124 | + ], |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should parse extended SnsSchema', () => { |
| 129 | + // Prepare |
| 130 | + const testEvent = getTestEvent<SnsEvent>({ |
| 131 | + eventsPath: '.', |
| 132 | + filename: 'snsEvent', |
| 133 | + }); |
| 134 | + testEvent.Records[0].Sns.Message = JSON.stringify(basePayload); |
| 135 | + |
| 136 | + // Act |
| 137 | + const extendedSchema = SqsSchema.extend({ |
| 138 | + Records: z.array( |
| 139 | + SnsRecordSchema.extend({ |
| 140 | + Sns: SnsNotificationSchema.extend({ |
| 141 | + Message: JSONStringified(bodySchema), |
| 142 | + }), |
| 143 | + }) |
| 144 | + ), |
130 | 145 | });
|
131 | 146 |
|
132 |
| - it('should parse extended SnsSchema', () => { |
133 |
| - const extendedSchema = SqsSchema.extend({ |
134 |
| - Records: z.array( |
135 |
| - SnsRecordSchema.extend({ |
136 |
| - Sns: SnsNotificationSchema.extend({ |
137 |
| - Message: JSONStringified(customSchema), |
138 |
| - }), |
139 |
| - }) |
140 |
| - ), |
141 |
| - }); |
142 |
| - |
143 |
| - const testEvent = getTestEvent<SnsEvent>({ |
144 |
| - eventsPath: '.', |
145 |
| - filename: 'snsEvent', |
146 |
| - }); |
147 |
| - testEvent.Records[0].Sns.Message = JSON.stringify(payload); |
148 |
| - |
149 |
| - const result = extendedSchema.parse(testEvent); |
150 |
| - expect(result).toEqual({ |
151 |
| - ...testEvent, |
152 |
| - Records: [ |
153 |
| - { |
154 |
| - ...testEvent.Records[0], |
155 |
| - Sns: { ...testEvent.Records[0].Sns, Message: payload }, |
156 |
| - }, |
157 |
| - ], |
158 |
| - }); |
| 147 | + // Assess |
| 148 | + expect(extendedSchema.parse(testEvent)).toStrictEqual({ |
| 149 | + ...testEvent, |
| 150 | + Records: [ |
| 151 | + { |
| 152 | + ...testEvent.Records[0], |
| 153 | + Sns: { ...testEvent.Records[0].Sns, Message: basePayload }, |
| 154 | + }, |
| 155 | + ], |
159 | 156 | });
|
160 | 157 | });
|
161 | 158 | });
|
0 commit comments