Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(renderer): rewrite default renderer #64

Merged
merged 3 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added `@vue-data` tag (#59)
- Added `@vue-computed` tag (#59)
- Added rendering system to support more JSDoc templates (#59)
- Rewrited default renderer (#64)

### Removals

Expand Down
93 changes: 89 additions & 4 deletions cypress/integration/renderers/default.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,102 @@
describe('Renderers: default', () => {
beforeEach(() => {
before(() => {
cy.visit('/../../../example/docs/BetterCounter.html');
});

it('should renders props correctly', () => {
cy.get('[data-vue="section-props"]').contains('Props');
cy.get('[data-jsdoc-vuejs="section-props"]').contains('Props');
cy.get('[data-jsdoc-vuejs="table-props"]').as('table-props');

cy
.get('@table-props')
.find('> thead > tr > th')
.should(($headers) => {
expect($headers).to.have.length(5);
expect($headers.eq(0).text()).to.contains('Name');
expect($headers.eq(1).text()).to.contains('Type');
expect($headers.eq(2).text()).to.contains('Default value');
expect($headers.eq(3).text()).to.contains('Required ?');
expect($headers.eq(4).text()).to.contains('Description');
});

cy
.get('@table-props')
.find('> tbody > tr')
.then(($rows) => {
const $firstRowChildren = $rows.eq(0).children();
const $secondRowChildren = $rows.eq(1).children();

expect($rows).to.have.length(2);

expect($firstRowChildren.eq(0).html()).to.eq('<b>initialCounter</b>');
expect($firstRowChildren.eq(1).html()).to.eq('Number');
expect($firstRowChildren.eq(2).html()).to.eq('-');
expect($firstRowChildren.eq(3).html()).to.eq('<b>Yes</b>');
expect($firstRowChildren.eq(4).html()).to.eq('-');

expect($secondRowChildren.eq(0).html()).to.eq('<b>step</b>');
expect($secondRowChildren.eq(1).html()).to.eq('Number');
expect($secondRowChildren.eq(2).html()).to.eq('<code>1</code>');
expect($secondRowChildren.eq(3).html()).to.eq('No');
expect($secondRowChildren.eq(4).html()).to.eq('Step');
});
});

it('should renders data correctly', () => {
cy.get('[data-vue="section-data"]').contains('Data');
cy.get('[data-jsdoc-vuejs="section-data"]').contains('Data');
cy.get('[data-jsdoc-vuejs="table-data"]').as('table-data');

cy
.get('@table-data')
.find('> thead > tr > th')
.should(($headers) => {
expect($headers).to.have.length(4);
expect($headers.eq(0).text()).to.contains('Name');
expect($headers.eq(1).text()).to.contains('Type');
expect($headers.eq(2).text()).to.contains('Default value');
expect($headers.eq(3).text()).to.contains('Description');
});

cy
.get('@table-data')
.find('> tbody > tr')
.then(($rows) => {
const $rowChildren = $rows.eq(0).children();

expect($rows).to.have.length(1);

expect($rowChildren.eq(0).html()).to.eq('<b>counter</b>');
expect($rowChildren.eq(1).html()).to.eq('Number');
expect($rowChildren.eq(2).html()).to.eq('-');
expect($rowChildren.eq(3).html()).to.eq('Current counter\'s value');
});
});

it('should renders computed correctly', () => {
cy.get('[data-vue="section-computed"]').contains('Computed');
cy.get('[data-jsdoc-vuejs="section-computed"]').contains('Computed');
cy.get('[data-jsdoc-vuejs="table-computed"]').as('table-data');

cy
.get('@table-data')
.find('> thead > tr > th')
.should(($headers) => {
expect($headers).to.have.length(3);
expect($headers.eq(0).text()).to.contains('Name');
expect($headers.eq(1).text()).to.contains('Type');
expect($headers.eq(2).text()).to.contains('Description');
});

cy
.get('@table-data')
.find('> tbody > tr')
.then(($rows) => {
const $rowChildren = $rows.eq(0).children();

expect($rows).to.have.length(1);

expect($rowChildren.eq(0).html()).to.eq('<b>message</b>');
expect($rowChildren.eq(1).html()).to.eq('String');
expect($rowChildren.eq(2).html()).to.eq('A message');
});
});
});
39 changes: 33 additions & 6 deletions lib/renderers/default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const makeTableHead = headers => `<thead><th>${headers.join('</th><th>')}</th></thead>`;
const makeTableBody = (items, cb) => `<tbody>${items.map(item => `<tr>${cb(item).trim()}</tr>`).join('')}</tbody>`;

module.exports = function renderDefault(description, props = [], data = [], computed = []) {
let html = description;

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

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

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

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

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