Skip to content

Commit f25fb22

Browse files
authored
fix(tests): fix some tests that got out of sync with the code (#77)
Signed-off-by: flakey5 <[email protected]>
1 parent dc693b7 commit f25fb22

File tree

5 files changed

+200
-41
lines changed

5 files changed

+200
-41
lines changed

src/metadata.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const createMetadata = slugger => {
132132
// a certain navigation section to a page ad the exact point of the page (scroll)
133133
// This is useful for classes, globals and other type of YAML entries, as they reside
134134
// within a module (page) and we want to link to them directly
135-
const slugHash = `#${slugger.slug(internalMetadata.heading.data.text)}`;
135+
const slugHash = `#${slugger.slug(internalMetadata.heading.text)}`;
136136

137137
const {
138138
source_link: sourceLink,

src/test/metadata.test.mjs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ describe('createMetadata', () => {
2323
const slugger = new GitHubSlugger();
2424
const metadata = createMetadata(slugger);
2525
const stability = 2;
26-
metadata.setStability(stability);
27-
strictEqual(metadata.create(new VFile(), {}).stability, stability);
26+
metadata.addStability(stability);
27+
const actual = metadata.create(new VFile(), {}).stability;
28+
delete actual.toJSON;
29+
deepStrictEqual(actual, {
30+
children: [stability],
31+
type: 'root',
32+
});
2833
});
2934

3035
it('should create a metadata entry correctly', () => {
@@ -41,7 +46,7 @@ describe('createMetadata', () => {
4146
const stability = 2;
4247
const properties = { source_link: 'test.com' };
4348
metadata.setHeading(heading);
44-
metadata.setStability(stability);
49+
metadata.addStability(stability);
4550
metadata.updateProperties(properties);
4651
const expected = {
4752
api: 'test',
@@ -50,9 +55,12 @@ describe('createMetadata', () => {
5055
updates: [],
5156
changes: [],
5257
heading,
53-
stability,
58+
stability: { type: 'root', children: [stability] },
5459
content: section,
60+
tags: [],
5561
};
56-
deepStrictEqual(metadata.create(apiDoc, section), expected);
62+
const actual = metadata.create(apiDoc, section);
63+
delete actual.stability.toJSON;
64+
deepStrictEqual(actual, expected);
5765
});
5866
});

src/test/parser.test.mjs

Lines changed: 164 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,79 @@ describe('createParser', () => {
1313
const expected = [
1414
{
1515
api: 'test',
16-
slug: 'test.html#test-heading',
17-
sourceLink: undefined,
16+
slug: 'test.html#',
1817
updates: [],
1918
changes: [],
2019
heading: {
21-
text: 'Test Heading',
22-
type: 'module',
23-
name: 'Test Heading',
20+
children: [
21+
{
22+
position: {
23+
end: {
24+
column: 15,
25+
line: 1,
26+
offset: 14,
27+
},
28+
start: {
29+
column: 3,
30+
line: 1,
31+
offset: 2,
32+
},
33+
},
34+
type: 'text',
35+
value: 'Test Heading',
36+
},
37+
],
38+
data: {
39+
depth: 1,
40+
name: 'Test Heading',
41+
text: 'Test Heading',
42+
type: 'module',
43+
},
2444
depth: 1,
45+
position: {
46+
end: {
47+
column: 15,
48+
line: 1,
49+
offset: 14,
50+
},
51+
start: {
52+
column: 1,
53+
line: 1,
54+
offset: 0,
55+
},
56+
},
57+
type: 'heading',
58+
},
59+
sourceLink: undefined,
60+
stability: { type: 'root', children: [] },
61+
content: {
62+
type: 'root',
63+
children: [
64+
{
65+
type: 'paragraph',
66+
children: [
67+
{
68+
type: 'text',
69+
value: 'This is a test.',
70+
position: {
71+
start: { line: 3, column: 1, offset: 16 },
72+
end: { line: 3, column: 16, offset: 31 },
73+
},
74+
},
75+
],
76+
position: {
77+
start: { line: 3, column: 1, offset: 16 },
78+
end: { line: 3, column: 16, offset: 31 },
79+
},
80+
},
81+
],
2582
},
26-
stability: undefined,
27-
content: { type: 'root', children: [] },
83+
tags: [],
2884
},
2985
];
3086
const actual = await parser.parseApiDoc(apiDoc);
87+
delete actual[0].heading.toJSON;
88+
delete actual[0].stability.toJSON;
3189
delete actual[0].content.toJSON;
3290
deepStrictEqual(actual, expected);
3391
});
@@ -47,37 +105,121 @@ describe('createParser', () => {
47105
const expected = [
48106
{
49107
api: 'test1',
50-
slug: 'test1.html#test-heading-1',
51-
sourceLink: undefined,
108+
slug: 'test1.html#',
52109
updates: [],
53110
changes: [],
54111
heading: {
55-
text: 'Test Heading 1',
56-
type: 'module',
57-
name: 'Test Heading 1',
112+
type: 'heading',
58113
depth: 1,
114+
children: [
115+
{
116+
type: 'text',
117+
value: 'Test Heading 1',
118+
position: {
119+
start: { line: 1, column: 3, offset: 2 },
120+
end: { line: 1, column: 17, offset: 16 },
121+
},
122+
},
123+
],
124+
position: {
125+
start: { line: 1, column: 1, offset: 0 },
126+
end: { line: 1, column: 17, offset: 16 },
127+
},
128+
data: {
129+
text: 'Test Heading 1',
130+
type: 'module',
131+
name: 'Test Heading 1',
132+
depth: 1,
133+
},
134+
},
135+
sourceLink: undefined,
136+
stability: { type: 'root', children: [] },
137+
content: {
138+
type: 'root',
139+
children: [
140+
{
141+
type: 'paragraph',
142+
children: [
143+
{
144+
type: 'text',
145+
value: 'This is a test.',
146+
position: {
147+
start: { line: 3, column: 1, offset: 18 },
148+
end: { line: 3, column: 16, offset: 33 },
149+
},
150+
},
151+
],
152+
position: {
153+
start: { line: 3, column: 1, offset: 18 },
154+
end: { line: 3, column: 16, offset: 33 },
155+
},
156+
},
157+
],
59158
},
60-
stability: undefined,
61-
content: { type: 'root', children: [] },
159+
tags: [],
62160
},
63161
{
64162
api: 'test2',
65-
slug: 'test2.html#test-heading-2',
66-
sourceLink: undefined,
163+
slug: 'test2.html#',
67164
updates: [],
68165
changes: [],
69166
heading: {
70-
text: 'Test Heading 2',
71-
type: 'module',
72-
name: 'Test Heading 2',
167+
type: 'heading',
73168
depth: 1,
169+
children: [
170+
{
171+
type: 'text',
172+
value: 'Test Heading 2',
173+
position: {
174+
start: { line: 1, column: 3, offset: 2 },
175+
end: { line: 1, column: 17, offset: 16 },
176+
},
177+
},
178+
],
179+
position: {
180+
start: { line: 1, column: 1, offset: 0 },
181+
end: { line: 1, column: 17, offset: 16 },
182+
},
183+
data: {
184+
text: 'Test Heading 2',
185+
type: 'module',
186+
name: 'Test Heading 2',
187+
depth: 1,
188+
},
74189
},
75-
stability: undefined,
76-
content: { type: 'root', children: [] },
190+
sourceLink: undefined,
191+
stability: { type: 'root', children: [] },
192+
content: {
193+
type: 'root',
194+
children: [
195+
{
196+
type: 'paragraph',
197+
children: [
198+
{
199+
type: 'text',
200+
value: 'This is another test.',
201+
position: {
202+
start: { line: 3, column: 1, offset: 18 },
203+
end: { line: 3, column: 22, offset: 39 },
204+
},
205+
},
206+
],
207+
position: {
208+
start: { line: 3, column: 1, offset: 18 },
209+
end: { line: 3, column: 22, offset: 39 },
210+
},
211+
},
212+
],
213+
},
214+
tags: [],
77215
},
78216
];
79217
const actual = await parser.parseApiDocs(apiDocs);
80-
actual.forEach(entry => delete entry.content.toJSON);
218+
actual.forEach(entry => {
219+
delete entry.heading.toJSON;
220+
delete entry.stability.toJSON;
221+
delete entry.content.toJSON;
222+
});
81223
deepStrictEqual(actual, expected);
82224
});
83225
});

src/test/queries.test.mjs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ describe('createQueries', () => {
1818
it('should update type to reference correctly', () => {
1919
const queries = createQueries();
2020
const node = { value: 'This is a {string} type.' };
21-
queries.updateTypeToReferenceLink(node);
21+
queries.updateTypeReference(node);
2222
strictEqual(node.type, 'html');
2323
strictEqual(
2424
node.value,
25-
'This is a [`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) type.'
25+
'This is a [`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) type.'
2626
);
2727
});
2828

2929
it('should update type to reference not correctly if no match', () => {
3030
const queries = createQueries();
3131
const node = { value: 'This is a {test} type.' };
32-
queries.updateTypeToReferenceLink(node);
32+
queries.updateTypeReference(node);
3333
strictEqual(node.type, 'html');
34-
strictEqual(node.value, 'This is a test type.');
34+
strictEqual(node.value, 'This is a {test} type.');
3535
});
3636

3737
it('should add heading metadata correctly', () => {
@@ -43,14 +43,23 @@ describe('createQueries', () => {
4343
const apiEntryMetadata = {
4444
setHeading: heading => {
4545
deepStrictEqual(heading, {
46-
text: 'Test Heading',
47-
type: 'module',
48-
name: 'Test Heading',
46+
children: [
47+
{
48+
type: 'text',
49+
value: 'Test Heading',
50+
},
51+
],
52+
data: {
53+
depth: 2,
54+
name: 'Test Heading',
55+
text: 'Test Heading',
56+
type: 'module',
57+
},
4958
depth: 2,
5059
});
5160
},
5261
};
53-
queries.addHeadingMetadata(node, apiEntryMetadata);
62+
queries.setHeadingMetadata(node, apiEntryMetadata);
5463
});
5564

5665
it('should update markdown link correctly', () => {
@@ -81,13 +90,13 @@ describe('createQueries', () => {
8190
],
8291
};
8392
const apiEntryMetadata = {
84-
setStability: stability => {
85-
deepStrictEqual(stability.toJSON(), {
93+
addStability: stability => {
94+
deepStrictEqual(stability.data, {
8695
index: 2,
8796
description: 'Frozen',
8897
});
8998
},
9099
};
91-
queries.addStabilityIndexMetadata(node, apiEntryMetadata);
100+
queries.addStabilityMetadata(node, apiEntryMetadata);
92101
});
93102
});

src/utils/tests/parser.test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ describe('transformTypeToReferenceLink', () => {
1111
it('should transform a JavaScript primitive type into a Markdown link', () => {
1212
strictEqual(
1313
transformTypeToReferenceLink('string'),
14-
'[`string`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)'
14+
'[`<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)'
1515
);
1616
});
1717

1818
it('should transform a JavaScript global type into a Markdown link', () => {
1919
strictEqual(
2020
transformTypeToReferenceLink('Array'),
21-
'[`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)'
21+
'[`<Array>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)'
2222
);
2323
});
2424
});

0 commit comments

Comments
 (0)