From 73b61826b90d111726f631d2187c50c729e6a236 Mon Sep 17 00:00:00 2001 From: Sergio Date: Sat, 24 Dec 2022 19:41:10 +0100 Subject: [PATCH] fix(tables): make number of dashes to match header length --- src/lib/markdown/mdtable.ts | 9 ++++++-- tests/lib/markdown/mdtable.spec.ts | 2 +- tests/transformers/pathParameters.spec.ts | 22 +++++++++---------- .../transformers/securityDefinitions.spec.ts | 8 ++++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/lib/markdown/mdtable.ts b/src/lib/markdown/mdtable.ts index f65a0a9..ff12d7a 100644 --- a/src/lib/markdown/mdtable.ts +++ b/src/lib/markdown/mdtable.ts @@ -46,13 +46,18 @@ export class MDtable { const result: string[] = []; const columns = this._th.length; - if (this._th.length) { + if (columns) { const th = `| ${this._th.map( (d: MDstring | string) => (typeof d === 'string' ? d : d.get()), ).join(' | ')} |`; result.push(th); - const thead = `| ${[...Array(columns).keys()].map(() => '---').join(' | ')} |`; + const thead = `| ${ + this._th.map( + (el: string | MDstring) => '-'.repeat(el.length > 3 ? el.length : 3) + , + ).join(' | ') + } |`; result.push(thead); } diff --git a/tests/lib/markdown/mdtable.spec.ts b/tests/lib/markdown/mdtable.spec.ts index 58ae663..1108d19 100644 --- a/tests/lib/markdown/mdtable.spec.ts +++ b/tests/lib/markdown/mdtable.spec.ts @@ -40,7 +40,7 @@ describe('MDtable', () => { .td('td1') .td('td2') .td(MDstring.string('td3').strikethrough()); - const expected = '| h1 | h2 | **h3** |\n| --- | --- | --- |\n' + const expected = '| h1 | h2 | **h3** |\n| --- | --- | ------ |\n' + '| td1 | td2 | ~~td3~~ |'; const result = table.get(); expect(result).to.be.equal(expected); diff --git a/tests/transformers/pathParameters.spec.ts b/tests/transformers/pathParameters.spec.ts index 36e3957..dfbf78b 100644 --- a/tests/transformers/pathParameters.spec.ts +++ b/tests/transformers/pathParameters.spec.ts @@ -1,10 +1,10 @@ import { expect } from 'chai'; import { transformParameters } from '../../src/transformers/pathParameters'; -const tableFixture = [ +const tableFixture: string[] = [ '##### Parameters', '| Name | Located in | Description | Required | Schema |', - '| ---- | ---------- | ----------- | -------- | ---- |', + '| ---- | ---------- | ----------- | -------- | ------ |', ]; describe('Path parameters transformer', () => { @@ -27,13 +27,13 @@ describe('Path parameters transformer', () => { }, {}, ]; - const results = [].concat(tableFixture, [ + const results = [...tableFixture, ...[ '| name | formData | name | No | string |', '| year | formData | year | Yes | string |', '| | formData | | No | string |', '| | | | No | |', - ]); - const res = transformParameters(fixture).split('\n'); + ]]; + const res = (transformParameters(fixture as any) as string).split('\n'); it('Should create parameters header', () => { expect(res[0]).to.be.equal(results[0]); @@ -59,10 +59,10 @@ describe('Path parameters transformer', () => { description: 'name', type: 'string', }]; - const results = [].concat(tableFixture, [ + const results = [...tableFixture, ...[ '| name | formData | name | No | string |', - ]); - const res = transformParameters(undefined, fixture).split('\n'); + ]]; + const res = (transformParameters(undefined as any, fixture) as string).split('\n'); it('Should create parameters header', () => { expect(res[0]).to.be.equal(results[0]); @@ -93,11 +93,11 @@ describe('Path parameters transformer', () => { description: 'name', type: 'string', }]; - const results = [].concat(tableFixture, [ + const results = [...tableFixture, ...[ '| path name | formData | name | No | string |', '| method name | formData | name | No | string |', - ]); - const res = transformParameters(methodFixture, pathFixture).split('\n'); + ]]; + const res = (transformParameters(methodFixture, pathFixture) as string).split('\n'); it('Should create parameters header', () => { expect(res[0]).to.be.equal(results[0]); }); diff --git a/tests/transformers/securityDefinitions.spec.ts b/tests/transformers/securityDefinitions.spec.ts index eaf8c87..a17c975 100644 --- a/tests/transformers/securityDefinitions.spec.ts +++ b/tests/transformers/securityDefinitions.spec.ts @@ -15,8 +15,8 @@ describe('Security definitions', () => { const result = '### Security\n' + '**auth** \n\n' + `| ${type} | *${typeResolver[type]}* |\n` - + '| --- | --- |\n\n\n'; - expect(result).to.be.equal(res); + + `| ${'-'.repeat(type.length)} | ${'-'.repeat(2 + typeResolver[type].length)} |\n\n\n`; + expect(res).to.be.equal(result); }); }); it('Should resolve names', () => { @@ -50,7 +50,9 @@ describe('Security definitions', () => { 'unknown-key': 'Uknown key', }, }; - const result = transformSecurityDefinitions(fixture as OpenAPIV2.SecurityDefinitionsObject); + const result = transformSecurityDefinitions( + fixture as OpenAPIV2.SecurityDefinitionsObject, + ) as string; expect(result.match(/undefined/ig)).to.be.null; expect(result.match(/x-special-key/ig)).to.be.not.null; });