|
| 1 | +import { TransformEnumInterceptor } from './transform-enum.interceptor'; |
| 2 | +import { |
| 3 | + ExecutionContext, |
| 4 | + CallHandler, |
| 5 | + BadRequestException, |
| 6 | +} from '@nestjs/common'; |
| 7 | +import { of } from 'rxjs'; |
| 8 | +import { IsNumber, IsString, Min } from 'class-validator'; |
| 9 | +import { JobType } from '../../common/enums/job'; |
| 10 | +import { ApiProperty } from '@nestjs/swagger'; |
| 11 | +import { IsEnumCaseInsensitive } from '../decorators/enums'; |
| 12 | + |
| 13 | +export class MockDto { |
| 14 | + @ApiProperty({ |
| 15 | + enum: JobType, |
| 16 | + }) |
| 17 | + @IsEnumCaseInsensitive(JobType) |
| 18 | + public jobType: JobType; |
| 19 | + |
| 20 | + @ApiProperty() |
| 21 | + @IsNumber() |
| 22 | + @Min(0.5) |
| 23 | + public amount: number; |
| 24 | + |
| 25 | + @ApiProperty() |
| 26 | + @IsString() |
| 27 | + public address: string; |
| 28 | +} |
| 29 | + |
| 30 | +describe('TransformEnumInterceptor', () => { |
| 31 | + let interceptor: TransformEnumInterceptor; |
| 32 | + let executionContext: ExecutionContext; |
| 33 | + let callHandler: CallHandler; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + interceptor = new TransformEnumInterceptor(); |
| 37 | + |
| 38 | + // Mocking ExecutionContext and CallHandler |
| 39 | + executionContext = { |
| 40 | + switchToHttp: jest.fn().mockReturnValue({ |
| 41 | + getRequest: jest.fn().mockReturnValue({ |
| 42 | + body: { |
| 43 | + jobType: 'FORTUNE', |
| 44 | + amount: 5, |
| 45 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 46 | + }, |
| 47 | + }), |
| 48 | + }), |
| 49 | + getHandler: jest.fn().mockReturnValue({ |
| 50 | + name: 'create', // Assume the handler is named 'create' |
| 51 | + }), |
| 52 | + getClass: jest.fn().mockReturnValue({ |
| 53 | + prototype: {}, |
| 54 | + }), |
| 55 | + } as unknown as ExecutionContext; |
| 56 | + |
| 57 | + callHandler = { |
| 58 | + handle: jest.fn().mockReturnValue(of({})), |
| 59 | + }; |
| 60 | + |
| 61 | + // Mock Reflect.getMetadata to return DTO and Enum types |
| 62 | + Reflect.getMetadata = jest.fn((metadataKey, target, propertyKey) => { |
| 63 | + // Mock design:paramtypes to return MockDto as the parameter type |
| 64 | + if (metadataKey === 'design:paramtypes') { |
| 65 | + return [MockDto]; |
| 66 | + } |
| 67 | + |
| 68 | + // Mock custom:enum to return the corresponding enum for each property |
| 69 | + if (metadataKey === 'custom:enum' && propertyKey === 'jobType') { |
| 70 | + return JobType; |
| 71 | + } |
| 72 | + return undefined; // For non-enum properties, return undefined |
| 73 | + }) as any; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should transform enum values to lowercase', async () => { |
| 77 | + // Run the interceptor |
| 78 | + await interceptor.intercept(executionContext, callHandler).toPromise(); |
| 79 | + |
| 80 | + // Access the modified request body |
| 81 | + const request = executionContext.switchToHttp().getRequest(); |
| 82 | + |
| 83 | + // Expectations |
| 84 | + expect(request.body.jobType).toBe('fortune'); // Should be transformed to lowercase |
| 85 | + expect(request.body).toEqual({ |
| 86 | + jobType: 'fortune', |
| 87 | + amount: 5, |
| 88 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 89 | + }); |
| 90 | + expect(callHandler.handle).toBeCalled(); // Ensure the handler is called |
| 91 | + }); |
| 92 | + |
| 93 | + it('should throw an error if the value is not a valid enum', async () => { |
| 94 | + // Modify the request body to have an invalid enum value for jobType |
| 95 | + executionContext.switchToHttp = jest.fn().mockReturnValue({ |
| 96 | + getRequest: jest.fn().mockReturnValue({ |
| 97 | + body: { |
| 98 | + jobType: 'invalidEnum', // Invalid enum value for jobType |
| 99 | + amount: 5, |
| 100 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 101 | + }, |
| 102 | + }), |
| 103 | + }); |
| 104 | + |
| 105 | + try { |
| 106 | + // Run the interceptor |
| 107 | + await interceptor.intercept(executionContext, callHandler).toPromise(); |
| 108 | + } catch (err) { |
| 109 | + // Expect an error to be thrown |
| 110 | + expect(err).toBeInstanceOf(BadRequestException); |
| 111 | + expect(err.response.statusCode).toBe(400); |
| 112 | + expect(err.response.message).toContain('Validation failed'); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not transform non-enum properties', async () => { |
| 117 | + // Run the interceptor with a non-enum property (amount and address) |
| 118 | + await interceptor.intercept(executionContext, callHandler).toPromise(); |
| 119 | + |
| 120 | + // Access the modified request body |
| 121 | + const request = executionContext.switchToHttp().getRequest(); |
| 122 | + |
| 123 | + // Expectations |
| 124 | + expect(request.body.amount).toBe(5); // Non-enum property should remain unchanged |
| 125 | + expect(request.body.address).toBe( |
| 126 | + '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 127 | + ); // Non-enum string should remain unchanged |
| 128 | + expect(callHandler.handle).toBeCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should handle nested objects with enums', async () => { |
| 132 | + // Modify the request body to have a nested object with enum value |
| 133 | + executionContext.switchToHttp = jest.fn().mockReturnValue({ |
| 134 | + getRequest: jest.fn().mockReturnValue({ |
| 135 | + body: { |
| 136 | + transaction: { |
| 137 | + jobType: 'FORTUNE', |
| 138 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 139 | + }, |
| 140 | + amount: 5, |
| 141 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 142 | + }, |
| 143 | + }), |
| 144 | + }); |
| 145 | + |
| 146 | + // Run the interceptor |
| 147 | + await interceptor.intercept(executionContext, callHandler).toPromise(); |
| 148 | + |
| 149 | + // Access the modified request body |
| 150 | + const request = executionContext.switchToHttp().getRequest(); |
| 151 | + |
| 152 | + // Expectations |
| 153 | + expect(request.body.transaction.jobType).toBe('fortune'); |
| 154 | + expect(request.body).toEqual({ |
| 155 | + transaction: { |
| 156 | + jobType: 'fortune', |
| 157 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 158 | + }, |
| 159 | + amount: 5, |
| 160 | + address: '0xCf88b3f1992458C2f5a229573c768D0E9F70C44e', |
| 161 | + }); |
| 162 | + expect(callHandler.handle).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments