forked from nicoespeon/gitgraph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2tsd.test.js
More file actions
210 lines (174 loc) · 4.94 KB
/
json2tsd.test.js
File metadata and controls
210 lines (174 loc) · 4.94 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const fs = require('fs');
const data = JSON.parse(fs.readFileSync("./scripts/__mocks__/doc.json"));
const {
parseComment,
pascal,
parseTypes,
getParams,
getProperties,
getReturns,
getClassFunctions,
getOptions,
getObject
} = require('./json2tsd.js')(data);
describe('parseComment', () => {
it('should remove all dot notation params', () => {
let comment = data.docs.find(d => d.name === "GitGraph").comment;
expect(parseComment(comment)).toMatchSnapshot();
});
});
describe('pascal', () => {
it('should uppercase first letter', () => {
expect(pascal("gitGraph")).toEqual("GitGraph");
})
});
describe('parseTypes', () => {
it('should join types', () => {
let param = { type: { names: ["number", "string"] } };
expect(parseTypes(param)).toEqual("number|string")
});
it('should replace object with gitgraph types', () => {
let param = { name: "options", type: { names: ["object"] } };
let doc = { see: ["Template"] };
expect(parseTypes(param, doc)).toEqual("GitGraph.TemplateOptions");
});
it('should prepend with `GitGraph.` if type exists', () => {
let param = { type: { names: ["Template"] } };
expect(parseTypes(param)).toEqual("GitGraph.Template");
});
it('should not prepend with `GitGraph.` if type not exists', () => {
let param = { type: { names: ["MouseEvent"] } };
expect(parseTypes(param)).toEqual("MouseEvent");
});
it('should deal this array notation', () => {
let param = { type: { names: ["Array.<string>"] } };
expect(parseTypes(param)).toEqual("string[]");
});
it('should put `any` if is an unknown object', () => {
let param = { type: { names: ["object"] } };
expect(parseTypes(param)).toEqual("any");
});
});
describe('getParams', () => {
it('should remove dot notation', () => {
let doc = data.docs.find(d => d.name === "GitGraph");
expect(getParams(doc)).toEqual(["options?: GitGraph.GitGraphOptions"]);
});
it('should parse type properly', () => {
let doc = data.docs.find(d => d.longname === "GitGraph#applyCommits");
expect(getParams(doc)).toEqual(["event: MouseEvent", "callbackFn: GitGraph.CommitCallback"]);
});
});
describe('getProperties', () => {
it('should extract properly all properties', () => {
let doc = data.docs.find(d => d.name === "BranchCommitOptions");
expect(getProperties(doc)).toMatchSnapshot();
});
it('should deal with `object` type', () => {
let doc = data.docs.find(d => d.name === "BranchCommitOptions");
expect(getProperties(doc)).toMatchSnapshot();
});
});
describe('getReturns', () => {
it('should parse return type', () => {
let doc = {
"returns": [
{
"type": {
"names": [
"Template"
]
},
"description": "<p>[template] - Template if exist</p>"
}
],
};
expect(getReturns(doc)).toEqual(["GitGraph.Template"]);
});
it('should return void if no returns', () => {
expect(getReturns({})).toEqual('void');
})
});
describe('getClassFunction', () => {
it('should return corresponding class function', () => {
expect(getClassFunctions("GitGraph")).toMatchSnapshot();
});
});
describe('getObject', () => {
it('should return a pretty format object', () => {
let input = [{
"type": {
"names": [
"string"
]
},
"optional": true,
"description": "<p>Arrow color</p>",
"name": "options.arrow.color"
},
{
"type": {
"names": [
"number"
]
},
"optional": true,
"description": "<p>Arrow size</p>",
"name": "options.arrow.size"
},
{
"type": {
"names": [
"number"
]
},
"optional": true,
"description": "<p>Arrow offset</p>",
"name": "options.arrow.offset"
}];
expect(getObject(input)).toMatchSnapshot();
});
it('should deal with nested objects', () => {
let input = [{
"type": {
"names": [
"string"
]
},
"optional": true,
"description": "<p>Master commit color (dot & message)</p>",
"name": "options.commit.color"
},
{
"type": {
"names": [
"string"
]
},
"optional": true,
"description": "<p>Commit dot color</p>",
"name": "options.commit.dot.color"
},
{
"type": {
"names": [
"number"
]
},
"optional": true,
"description": "<p>Commit dot size</p>",
"name": "options.commit.dot.size"
}];
expect(getObject(input)).toMatchSnapshot();
})
});
describe('getOptions', () => {
it('should return parsed options from doc', () => {
let doc = data.docs.find(d => d.name = "GitGraph");
expect(getOptions(doc)).toMatchSnapshot();
});
it('should deal with nested object format', () => {
let doc = data.docs.find(d => d.name === "Template");
expect(getOptions(doc)).toMatchSnapshot();
});
});