Skip to content

Commit 5e1747b

Browse files
committed
1 parent ea9468b commit 5e1747b

File tree

3 files changed

+123
-10
lines changed

3 files changed

+123
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added `@vue-data` tag (#59)
99
- Added `@vue-computed` tag (#59)
1010
- Added rendering system to support more JSDoc templates (#59)
11+
- Rewrote default renderer (#64)
1112

1213
### Removals
1314

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,102 @@
11
describe('Renderers: default', () => {
2-
beforeEach(() => {
2+
before(() => {
33
cy.visit('/../../../example/docs/BetterCounter.html');
44
});
55

66
it('should renders props correctly', () => {
7-
cy.get('[data-vue="section-props"]').contains('Props');
7+
cy.get('[data-jsdoc-vuejs="section-props"]').contains('Props');
8+
cy.get('[data-jsdoc-vuejs="table-props"]').as('table-props');
9+
10+
cy
11+
.get('@table-props')
12+
.find('> thead > tr > th')
13+
.should(($headers) => {
14+
expect($headers).to.have.length(5);
15+
expect($headers.eq(0).text()).to.contains('Name');
16+
expect($headers.eq(1).text()).to.contains('Type');
17+
expect($headers.eq(2).text()).to.contains('Default value');
18+
expect($headers.eq(3).text()).to.contains('Required ?');
19+
expect($headers.eq(4).text()).to.contains('Description');
20+
});
21+
22+
cy
23+
.get('@table-props')
24+
.find('> tbody > tr')
25+
.then(($rows) => {
26+
const $firstRowChildren = $rows.eq(0).children();
27+
const $secondRowChildren = $rows.eq(1).children();
28+
29+
expect($rows).to.have.length(2);
30+
31+
expect($firstRowChildren.eq(0).html()).to.eq('<b>initialCounter</b>');
32+
expect($firstRowChildren.eq(1).html()).to.eq('Number');
33+
expect($firstRowChildren.eq(2).html()).to.eq('-');
34+
expect($firstRowChildren.eq(3).html()).to.eq('<b>Yes</b>');
35+
expect($firstRowChildren.eq(4).html()).to.eq('-');
36+
37+
expect($secondRowChildren.eq(0).html()).to.eq('<b>step</b>');
38+
expect($secondRowChildren.eq(1).html()).to.eq('Number');
39+
expect($secondRowChildren.eq(2).html()).to.eq('<code>1</code>');
40+
expect($secondRowChildren.eq(3).html()).to.eq('No');
41+
expect($secondRowChildren.eq(4).html()).to.eq('Step');
42+
});
843
});
944

1045
it('should renders data correctly', () => {
11-
cy.get('[data-vue="section-data"]').contains('Data');
46+
cy.get('[data-jsdoc-vuejs="section-data"]').contains('Data');
47+
cy.get('[data-jsdoc-vuejs="table-data"]').as('table-data');
48+
49+
cy
50+
.get('@table-data')
51+
.find('> thead > tr > th')
52+
.should(($headers) => {
53+
expect($headers).to.have.length(4);
54+
expect($headers.eq(0).text()).to.contains('Name');
55+
expect($headers.eq(1).text()).to.contains('Type');
56+
expect($headers.eq(2).text()).to.contains('Default value');
57+
expect($headers.eq(3).text()).to.contains('Description');
58+
});
59+
60+
cy
61+
.get('@table-data')
62+
.find('> tbody > tr')
63+
.then(($rows) => {
64+
const $rowChildren = $rows.eq(0).children();
65+
66+
expect($rows).to.have.length(1);
67+
68+
expect($rowChildren.eq(0).html()).to.eq('<b>counter</b>');
69+
expect($rowChildren.eq(1).html()).to.eq('Number');
70+
expect($rowChildren.eq(2).html()).to.eq('-');
71+
expect($rowChildren.eq(3).html()).to.eq('Current counter\'s value');
72+
});
1273
});
1374

1475
it('should renders computed correctly', () => {
15-
cy.get('[data-vue="section-computed"]').contains('Computed');
76+
cy.get('[data-jsdoc-vuejs="section-computed"]').contains('Computed');
77+
cy.get('[data-jsdoc-vuejs="table-computed"]').as('table-data');
78+
79+
cy
80+
.get('@table-data')
81+
.find('> thead > tr > th')
82+
.should(($headers) => {
83+
expect($headers).to.have.length(3);
84+
expect($headers.eq(0).text()).to.contains('Name');
85+
expect($headers.eq(1).text()).to.contains('Type');
86+
expect($headers.eq(2).text()).to.contains('Description');
87+
});
88+
89+
cy
90+
.get('@table-data')
91+
.find('> tbody > tr')
92+
.then(($rows) => {
93+
const $rowChildren = $rows.eq(0).children();
94+
95+
expect($rows).to.have.length(1);
96+
97+
expect($rowChildren.eq(0).html()).to.eq('<b>message</b>');
98+
expect($rowChildren.eq(1).html()).to.eq('String');
99+
expect($rowChildren.eq(2).html()).to.eq('A message');
100+
});
16101
});
17102
});

lib/renderers/default.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const makeTableHead = headers => `<thead><th>${headers.join('</th><th>')}</th></thead>`;
2+
const makeTableBody = (items, cb) => `<tbody>${items.map(item => `<tr>${cb(item).trim()}</tr>`).join('')}</tbody>`;
3+
14
module.exports = function renderDefault(description, props = [], data = [], computed = []) {
25
let html = description;
36

@@ -7,18 +10,42 @@ module.exports = function renderDefault(description, props = [], data = [], comp
710
html += '</p></div></div>';
811

912
if (props.length > 0) {
10-
html += '<h3 class="subsection-title" data-vue="section-props">Props</h3>';
11-
html += JSON.stringify(props);
13+
html += '<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-props">Props</h3>';
14+
html += '<table data-jsdoc-vuejs="table-props">';
15+
html += makeTableHead(['Name', 'Type', 'Default value', 'Required ?', 'Description']);
16+
html += makeTableBody(props, item => `
17+
<td><b>${item.name}</b></td>
18+
<td>${(item.type.names || []).join(', ')}</td>
19+
<td>${typeof item.defaultvalue === 'undefined' ? '-' : `<code>${item.defaultvalue}</code>`}</td>
20+
<td>${item.optional ? 'No' : '<b>Yes</b>'}</td>
21+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
22+
`);
23+
html += '</table>';
1224
}
1325

1426
if (data.length > 0) {
15-
html += '<h3 class="subsection-title" data-vue="section-data">Data</h3>';
16-
html += JSON.stringify(data);
27+
html += '<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-data">Data</h3>';
28+
html += '<table data-jsdoc-vuejs="table-data">';
29+
html += makeTableHead(['Name', 'Type', 'Default value', 'Description']);
30+
html += makeTableBody(data, item => `
31+
<td><b>${item.name}</b></td>
32+
<td>${(item.type.names || []).join(', ')}</td>
33+
<td>${typeof item.defaultvalue === 'undefined' ? '-' : `<code>${item.defaultvalue}</code>`}</td>
34+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
35+
`);
36+
html += '</table>';
1737
}
1838

1939
if (computed.length > 0) {
20-
html += '<h3 class="subsection-title" data-vue="section-computed">Computed</h3>';
21-
html += JSON.stringify(computed);
40+
html += '<h3 class="subsection-title" style="margin-top: 1em" data-jsdoc-vuejs="section-computed">Computed</h3>';
41+
html += '<table data-jsdoc-vuejs="table-computed">';
42+
html += makeTableHead(['Name', 'Type', 'Description']);
43+
html += makeTableBody(computed, item => `
44+
<td><b>${item.name}</b></td>
45+
<td>${(item.type.names || []).join(', ')}</td>
46+
<td>${typeof item.description === 'undefined' ? '-' : item.description}</td>
47+
`);
48+
html += '</table>';
2249
}
2350

2451
html += '<div class="container-overview"><div><p>';

0 commit comments

Comments
 (0)