-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.test.js
89 lines (72 loc) · 2.92 KB
/
generator.test.js
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
import { concatOptionalParams, serializeObjectToQueryString, uri } from "./generator";
describe("serializeObjectToQueryString", () => {
test("concatenate various params", () => {
expect(serializeObjectToQueryString({ id: 3, age: 32, name: "Pablo" })).toEqual(
"id=3&age=32&name=Pablo"
);
});
test("encode params", () => {
expect(serializeObjectToQueryString({ id: "Pablo Alboren" })).toEqual("id=Pablo%20Alboren");
});
test("show only one param", () => {
expect(serializeObjectToQueryString({ id: 1023 })).toEqual("id=1023");
});
});
describe("concatOptionalParams", () => {
test("concatOptionalParams add ? character between url and params", () => {
const params = { id: 3, age: 32, name: "Pablo" };
expect(concatOptionalParams("/products", params)).toEqual(
"/products?" + serializeObjectToQueryString(params)
);
});
test("concatOptionalParams dont add ? character with no params", () => {
expect(concatOptionalParams("/products")).toEqual("/products");
});
test("concatOptionalParams dont add ? character with null params", () => {
expect(concatOptionalParams("/products", null)).toEqual("/products");
});
test("concatOptionalParams dont add ? character with mepty params", () => {
expect(concatOptionalParams("/products", {})).toEqual("/products");
});
test("throw exception with no object param", () => {
expect(() => concatOptionalParams("/products", 23)).toThrow();
});
});
describe("uri", () => {
test("uri return original url with no params", () => {
let f = uri("/products/:id");
expect(f()).toEqual("/products/:id");
});
test("uri return original url with no params and add optional query params", () => {
let f = uri("/products/:id");
expect(f({ orderBy: "age" })).toEqual("/products/:id?orderBy=age");
});
test("uri throw exception with null params", () => {
expect(() => uri("/products/:id", { id: null })).toThrow();
});
test("uri throw exception with null params and add optional query params", () => {
expect(() => uri("/products/:id", { id: null })).toThrow();
});
test("uri return original url with undefined params", () => {
let id;
let f = uri("/products/:id", { id });
expect(f()).toEqual("/products/:id");
});
test("uri return original url with undefined params and add optional query params", () => {
let id;
let f = uri("/products/:id", { id });
expect(f({ orderBy: "age" })).toEqual("/products/:id?orderBy=age");
});
test("uri replace params", () => {
let f = uri("/products/:id", { id: 23 });
expect(f()).toEqual("/products/23");
});
test("uri replace params and add optional query params", () => {
let f = uri("/products/:id", { id: 23 });
expect(f({ orderBy: "age" })).toEqual("/products/23?orderBy=age");
});
test("throw exception with no object param", () => {
let f = uri("/products/:id", { id: 23 });
expect(() => f(23)).toThrow();
});
});