-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patharg.test.ts
93 lines (83 loc) · 2.85 KB
/
arg.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2020 Liam Tan. All rights reserved. MIT license.
import { assertEquals } from "./deps.ts";
import { defineParameterDecorator, Param, Body, Query, Header } from "./arg.ts";
import { Get } from "./method.ts";
import { getControllerOwnMeta } from "./metadata.ts";
import { ControllerMetadata, RouteArgument, ArgsType } from "./types.ts";
const TEST_ARG_TYPE: ArgsType = ArgsType.PARAM;
const TestDecorator = (key: string) => defineParameterDecorator(TEST_ARG_TYPE, key);
Deno.test({
name: "Arg decorator appropriately applies default metadata if class is Newable non-controller",
fn(): void {
class TestClass {
@Get()
public testAction(@TestDecorator("id") id: any): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
assertEquals(meta?.prefix, null);
assertEquals(meta?.defaultResponseCodes instanceof Map, true);
assertEquals(meta?.routes instanceof Map, true);
assertEquals(meta?.args instanceof Array, true);
},
});
Deno.test({
name: "Arg decorator adds correct argument metadata to parent controller",
fn(): void {
const key: string = "id";
const actionName: string = "testAction";
class TestClass {
@Get()
public [actionName](@TestDecorator(key) id: any): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
const appliedArg: RouteArgument | undefined = meta?.args[0];
assertEquals(appliedArg?.type, TEST_ARG_TYPE);
assertEquals(appliedArg?.key, key);
assertEquals(appliedArg?.argFor, actionName);
assertEquals(appliedArg?.index, 0);
},
});
Deno.test({
name: "@Param decorator should allow no key passed",
fn(): void {
class TestClass {
@Get()
public testAction(@Param() params: Object): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
assertEquals(meta?.args[0].key, undefined);
},
});
Deno.test({
name: "@Body decorator should allow no key passed",
fn(): void {
class TestClass {
@Get()
public testAction(@Body() body: Object): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
assertEquals(meta?.args[0].key, undefined);
},
});
Deno.test({
name: "@Query decorator should allow no key passed",
fn(): void {
class TestClass {
@Get()
public testAction(@Query() query: Object): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
assertEquals(meta?.args[0].key, undefined);
},
});
Deno.test({
name: "@Header decorator should allow no key passed",
fn(): void {
class TestClass {
@Get()
public testAction(@Header() headers: Object): void {}
}
const meta: ControllerMetadata | undefined = getControllerOwnMeta(TestClass);
assertEquals(meta?.args[0].key, undefined);
},
});