|
1 | | -import { ActionParameterHandler } from '../src/ActionParameterHandler'; |
2 | | -import { ActionMetadata, ControllerMetadata, ExpressDriver, ParamMetadata } from '../src'; |
3 | | -import { ActionMetadataArgs } from '../src/metadata/args/ActionMetadataArgs'; |
4 | | -import { ControllerMetadataArgs } from '../src/metadata/args/ControllerMetadataArgs'; |
| 1 | +import {ActionParameterHandler} from "../src/ActionParameterHandler"; |
| 2 | +import {ActionMetadata, ControllerMetadata, ExpressDriver, ParamMetadata} from "../src"; |
| 3 | +import {ActionMetadataArgs} from "../src/metadata/args/ActionMetadataArgs"; |
| 4 | +import {ControllerMetadataArgs} from "../src/metadata/args/ControllerMetadataArgs"; |
5 | 5 |
|
6 | | -const expect = require('chakram').expect; |
| 6 | +const expect = require("chakram").expect; |
7 | 7 |
|
8 | | -describe('ActionParameterHandler', () => { |
9 | | - it('handle', async () => { |
10 | | - const driver = new ExpressDriver(); |
11 | | - const actionParameterHandler = new ActionParameterHandler(driver); |
12 | | - |
13 | | - const action = { |
14 | | - request: { |
15 | | - params: { |
16 | | - id: '0b5ec98f-e26d-4414-b798-dcd35a5ef859', |
17 | | - }, |
18 | | - }, |
19 | | - response: {}, |
20 | | - }; |
| 8 | +describe("ActionParameterHandler", () => { |
| 9 | + const buildParamMetadata = (): ParamMetadata => { |
21 | 10 | const controllerMetadataArgs: ControllerMetadataArgs = { |
22 | | - target: function () {}, |
23 | | - route: '', |
24 | | - type: 'json', |
| 11 | + target: function () { |
| 12 | + }, |
| 13 | + route: "", |
| 14 | + type: "json", |
25 | 15 | options: {}, |
26 | 16 | }; |
27 | 17 | const controllerMetadata = new ControllerMetadata(controllerMetadataArgs); |
28 | 18 | const args: ActionMetadataArgs = { |
29 | | - route: '', |
30 | | - method: 'getProduct', |
| 19 | + route: "", |
| 20 | + method: "getProduct", |
31 | 21 | options: {}, |
32 | | - target: function () {}, |
33 | | - type: 'get', |
| 22 | + target: function () { |
| 23 | + }, |
| 24 | + type: "get", |
34 | 25 | appendParams: undefined, |
35 | 26 | }; |
36 | 27 | const actionMetadata = new ActionMetadata(controllerMetadata, args, {}); |
37 | 28 |
|
38 | | - const param: ParamMetadata = { |
39 | | - targetName: 'product', |
| 29 | + return { |
| 30 | + targetName: "product", |
40 | 31 | isTargetObject: true, |
41 | 32 | actionMetadata, |
42 | | - target: () => {}, |
43 | | - method: 'getProduct', |
44 | | - object: 'getProduct', |
| 33 | + target: () => { |
| 34 | + }, |
| 35 | + method: "getProduct", |
| 36 | + object: "getProduct", |
45 | 37 | extraOptions: undefined, |
46 | 38 | index: 0, |
47 | | - type: 'param', |
48 | | - name: 'id', |
| 39 | + type: "param", |
| 40 | + name: "id", |
49 | 41 | parse: undefined, |
50 | 42 | required: false, |
51 | 43 | transform: function (action, value) { |
52 | 44 | return value; |
53 | 45 | }, |
54 | 46 | classTransform: undefined, |
55 | 47 | validate: undefined, |
56 | | - targetType: function () {}, |
| 48 | + targetType: function () { |
| 49 | + }, |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + it("handle - should process string parameters", async () => { |
| 54 | + const driver = new ExpressDriver(); |
| 55 | + const actionParameterHandler = new ActionParameterHandler(driver); |
| 56 | + const param = buildParamMetadata(); |
| 57 | + |
| 58 | + const action = { |
| 59 | + request: { |
| 60 | + params: { |
| 61 | + id: "0b5ec98f-e26d-4414-b798-dcd35a5ef859", |
| 62 | + }, |
| 63 | + }, |
| 64 | + response: {}, |
57 | 65 | }; |
58 | 66 |
|
59 | 67 | const processedValue = await actionParameterHandler.handle(action, param); |
60 | 68 |
|
61 | 69 | expect(processedValue).to.be.eq(action.request.params.id); |
62 | 70 | }); |
| 71 | + |
| 72 | + it("handle - should process string parameters, returns empty if a given string is empty", async () => { |
| 73 | + const driver = new ExpressDriver(); |
| 74 | + const actionParameterHandler = new ActionParameterHandler(driver); |
| 75 | + const param = buildParamMetadata(); |
| 76 | + |
| 77 | + const action = { |
| 78 | + request: { |
| 79 | + params: { |
| 80 | + id: "", |
| 81 | + }, |
| 82 | + }, |
| 83 | + response: {}, |
| 84 | + }; |
| 85 | + |
| 86 | + const processedValue = await actionParameterHandler.handle(action, param); |
| 87 | + |
| 88 | + expect(processedValue).to.be.eq(action.request.params.id); |
| 89 | + }); |
| 90 | + |
| 91 | + it("handle - should process number parameters", async () => { |
| 92 | + const driver = new ExpressDriver(); |
| 93 | + const actionParameterHandler = new ActionParameterHandler(driver); |
| 94 | + const param = buildParamMetadata(); |
| 95 | + |
| 96 | + const action = { |
| 97 | + request: { |
| 98 | + params: { |
| 99 | + id: 10000, |
| 100 | + }, |
| 101 | + }, |
| 102 | + response: {}, |
| 103 | + }; |
| 104 | + |
| 105 | + const processedValue = await actionParameterHandler.handle(action, param); |
| 106 | + |
| 107 | + expect(processedValue).to.be.eq(action.request.params.id); |
| 108 | + }); |
| 109 | + |
| 110 | + it("handle - undefined on empty object provided", async () => { |
| 111 | + const driver = new ExpressDriver(); |
| 112 | + const actionParameterHandler = new ActionParameterHandler(driver); |
| 113 | + const param = buildParamMetadata(); |
| 114 | + |
| 115 | + const action = { |
| 116 | + request: { |
| 117 | + params: {}, |
| 118 | + }, |
| 119 | + response: {}, |
| 120 | + }; |
| 121 | + |
| 122 | + const processedValue = await actionParameterHandler.handle(action, param); |
| 123 | + |
| 124 | + expect(processedValue).to.be.eq(undefined); |
| 125 | + }); |
| 126 | + |
63 | 127 | }); |
0 commit comments