|
1 | 1 | import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
|
2 | 2 | import context from '@aws-lambda-powertools/testing-utils/context';
|
3 | 3 | import type { Context } from 'aws-lambda';
|
4 |
| -import { describe, expect, it } from 'vitest'; |
5 |
| -import { idempotent } from '../../src/index.js'; |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | +import { idempotent, IdempotencyConfig } from '../../src/index.js'; |
6 | 6 | import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js';
|
| 7 | +import { BasePersistenceLayer } from '../../src/persistence/BasePersistenceLayer.js'; |
7 | 8 |
|
8 | 9 | describe('Given a class with a function to decorate', () => {
|
9 | 10 | it('maintains the scope of the decorated function', async () => {
|
@@ -35,4 +36,41 @@ describe('Given a class with a function to decorate', () => {
|
35 | 36 | // Assess
|
36 | 37 | expect(result).toBe('private foo');
|
37 | 38 | });
|
| 39 | + |
| 40 | + it('passes the custom keyPrefix to the persistenceStore', async () => { |
| 41 | + // Prepare |
| 42 | + const configureSpy = vi.spyOn(BasePersistenceLayer.prototype, 'configure'); |
| 43 | + const idempotencyConfig = new IdempotencyConfig({}); |
| 44 | + |
| 45 | + class TestClass implements LambdaInterface { |
| 46 | + @idempotent({ |
| 47 | + persistenceStore: new PersistenceLayerTestClass(), |
| 48 | + config: idempotencyConfig, |
| 49 | + keyPrefix: 'my-custom-prefix', |
| 50 | + }) |
| 51 | + public async handler( |
| 52 | + _event: unknown, |
| 53 | + _context: Context |
| 54 | + ): Promise<boolean> { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const handlerClass = new TestClass(); |
| 60 | + const handler = handlerClass.handler.bind(handlerClass); |
| 61 | + |
| 62 | + // Act |
| 63 | + const result = await handler({}, context); |
| 64 | + |
| 65 | + // Assess |
| 66 | + expect(result).toBeTruthy(); |
| 67 | + |
| 68 | + expect(configureSpy).toHaveBeenCalled(); |
| 69 | + const configureCallArgs = configureSpy.mock.calls[0][0]; // Extract first call's arguments |
| 70 | + expect(configureCallArgs.config).toBe(idempotencyConfig); |
| 71 | + expect(configureCallArgs.keyPrefix).toBe('my-custom-prefix'); |
| 72 | + |
| 73 | + // Restore the spy |
| 74 | + configureSpy.mockRestore(); |
| 75 | + }); |
38 | 76 | });
|
0 commit comments