Skip to content

Commit 6f37f76

Browse files
committed
feat: fix tc to verify the lamdba is not invoked and uses local output
1 parent 109d035 commit 6f37f76

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/app/utils/__tests__/convert-html-to-pdf.spec.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,43 @@ describe('convert-html-to-pdf', () => {
152152

153153
// TODO: [PDF-LAMBDA-GENERATION]: Remove tests for shadowing and using the correct output based on isUseLambdaOutput once lambda is rolled out
154154
describe('generatePdfFromHtml', () => {
155-
it('should use lambda output when isUseLambdaOutput is true and not use local output', async () => {
155+
beforeEach(() => {
156+
AwsConfig.pdfGeneratorLambda.invoke = jest.fn()
157+
})
158+
it('should not invoke lambda and just return local output when AwsConfig.pdfGeneratorLambdaFunctionName is empty', async () => {
159+
// Arrange
160+
const mockLocalBuffer = Buffer.from('local pdf content')
161+
AwsConfig.pdfGeneratorLambdaFunctionName = ''
162+
163+
// Mock local response
164+
const mockPage = {
165+
setContent: jest.fn(),
166+
pdf: jest.fn().mockResolvedValue(mockLocalBuffer),
167+
}
168+
const mockBrowser = {
169+
newPage: jest.fn().mockResolvedValue(mockPage),
170+
close: jest.fn(),
171+
}
172+
;(puppeteer.launch as jest.Mock).mockResolvedValue(mockBrowser)
173+
174+
// Act
175+
const result = await generatePdfFromHtml(MOCK_HTML, true)
176+
177+
// Assert
178+
expect(result).toEqual(mockLocalBuffer)
179+
expect(AwsConfig.pdfGeneratorLambda.invoke).not.toHaveBeenCalled()
180+
})
181+
182+
it('should use lambda output when isUseLambdaOutput is true and not use local output and AwsConfig.pdfGeneratorLambdaFunctionName is defined', async () => {
156183
// Arrange
157184
const mockLambdaBuffer = Buffer.from('lambda pdf content')
158185
const mockLocalBuffer = Buffer.from('local pdf content')
159186

187+
const DUMMY_PDF_GENERATOR_FUNCTION_NAME =
188+
'dummy-pdf-generator-function-name'
189+
AwsConfig.pdfGeneratorLambdaFunctionName =
190+
DUMMY_PDF_GENERATOR_FUNCTION_NAME
191+
160192
// Mock lambda response
161193
const mockLambdaResponse = {
162194
Payload: JSON.stringify({
@@ -188,16 +220,21 @@ describe('convert-html-to-pdf', () => {
188220

189221
// Verify the lambda invocation parameters
190222
expect(AwsConfig.pdfGeneratorLambda.invoke).toHaveBeenCalledWith({
191-
FunctionName: AwsConfig.pdfGeneratorLambdaFunctionName,
223+
FunctionName: DUMMY_PDF_GENERATOR_FUNCTION_NAME,
192224
Payload: JSON.stringify({ html: MOCK_HTML }),
193225
})
194226
})
195227

196-
it('should use local output when isUseLambdaOutput is false and not use lambda output', async () => {
228+
it('should use local output when isUseLambdaOutput is false and not use lambda output and AwsConfig.pdfGeneratorLambdaFunctionName is defined', async () => {
197229
// Arrange
198230
const mockLambdaBuffer = Buffer.from('lambda pdf content')
199231
const mockLocalBuffer = Buffer.from('local pdf content')
200232

233+
const DUMMY_PDF_GENERATOR_FUNCTION_NAME =
234+
'dummy-pdf-generator-function-name'
235+
AwsConfig.pdfGeneratorLambdaFunctionName =
236+
DUMMY_PDF_GENERATOR_FUNCTION_NAME
237+
201238
// Mock lambda response
202239
const mockLambdaResponse = {
203240
Payload: JSON.stringify({
@@ -228,7 +265,7 @@ describe('convert-html-to-pdf', () => {
228265
expect(result).not.toEqual(mockLambdaBuffer)
229266
})
230267

231-
it('should generate PDFs using both methods in parallel regardless of isUseLambdaOutput setting', async () => {
268+
it('should generate PDFs using both methods in parallel regardless of isUseLambdaOutput setting if AwsConfig.pdfGeneratorLambdaFunctionName is defined', async () => {
232269
// Arrange
233270
const mockLambdaBuffer = Buffer.from('lambda pdf content')
234271
const mockLocalBuffer = Buffer.from('local pdf content')
@@ -240,6 +277,12 @@ describe('convert-html-to-pdf', () => {
240277
body: mockLambdaBuffer.toString('base64'),
241278
}),
242279
}
280+
281+
// Mock AWS config to ensure lambda function name is defined
282+
const DUMMY_PDF_GENERATOR_FUNCTION_NAME =
283+
'dummy-pdf-generator-function-name'
284+
AwsConfig.pdfGeneratorLambdaFunctionName =
285+
DUMMY_PDF_GENERATOR_FUNCTION_NAME
243286
;(AwsConfig.pdfGeneratorLambda.invoke as jest.Mock)
244287
.mockResolvedValueOnce(mockLambdaResponse)
245288
.mockResolvedValueOnce(mockLambdaResponse)
@@ -269,7 +312,7 @@ describe('convert-html-to-pdf', () => {
269312

270313
// Verify the lambda invocation parameters
271314
expect(AwsConfig.pdfGeneratorLambda.invoke).toHaveBeenCalledWith({
272-
FunctionName: AwsConfig.pdfGeneratorLambdaFunctionName,
315+
FunctionName: DUMMY_PDF_GENERATOR_FUNCTION_NAME,
273316
Payload: JSON.stringify({ html: MOCK_HTML }),
274317
})
275318
})

0 commit comments

Comments
 (0)