Skip to content

Commit

Permalink
feat(tests/transform): pull reusable getCommentLines function
Browse files Browse the repository at this point in the history
  • Loading branch information
I-3B committed Jun 3, 2024
1 parent 60fdc56 commit f502d3c
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions test/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function check(md, anchors, mode, maxHeaderLevel, title, notitle, entryPrefix, p
t.end()
})
}

function getCommentLines(transformRes){
var lines = transformRes.wrappedToc.split('\n')
return lines.slice(0,2).concat(lines[lines.length - 1])
}
//function check() {}

check(
Expand Down Expand Up @@ -221,7 +226,7 @@ check(


test('transforming when old toc exists', function (t) {
var md = [
var md = [
'# Header above'
, ''
, 'The above header should be ignored since it is above the existing toc'
Expand All @@ -232,25 +237,25 @@ test('transforming when old toc exists', function (t) {
, ''
, '- [OldHeader](#oldheader)'
, ''
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '## Header'
, 'some content'
, ''
].join('\n')

var res = transform(md)

t.ok(res.transformed, 'transforms it')
t.ok(res.transformed, 'transforms it')

t.deepEqual(
res.toc.split('\n')
, [ '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*',
'',
'- [Header](#header)',
'' ]
, 'replaces old toc'
, 'replaces old toc'
)

t.deepEqual(
res.wrappedToc.split('\n')
, [ '<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
Expand All @@ -259,7 +264,7 @@ test('transforming when old toc exists', function (t) {
'',
'- [Header](#header)',
'',
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
]
, 'wraps old toc'
)
Expand All @@ -281,12 +286,12 @@ test('transforming when old toc exists', function (t) {
'some content',
'' ]
, 'updates the content with the new toc and ignores header before existing toc'
)
)
t.end()
})

test('transforming when old toc exists and --all flag is set', function (t) {
var md = [
var md = [
'# Header above'
, ''
, 'The above header should be ignored since it is above the existing toc'
Expand All @@ -297,15 +302,15 @@ test('transforming when old toc exists and --all flag is set', function (t) {
, ''
, '- [OldHeader](#oldheader)'
, ''
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
, '## Header'
, 'some content'
, ''
].join('\n')

var res = transform(md, undefined, undefined, undefined, undefined, undefined, true)

t.ok(res.transformed, 'transforms it')
t.ok(res.transformed, 'transforms it')

t.deepEqual(
res.toc.split('\n')
Expand All @@ -314,9 +319,9 @@ test('transforming when old toc exists and --all flag is set', function (t) {
'- [Header above](#header-above)',
' - [Header](#header)',
'' ]
, 'replaces old toc'
, 'replaces old toc'
)

t.deepEqual(
res.wrappedToc.split('\n')
, [ '<!-- START doctoc generated TOC please keep comment here to allow auto update -->',
Expand All @@ -326,7 +331,7 @@ test('transforming when old toc exists and --all flag is set', function (t) {
'- [Header above](#header-above)',
' - [Header](#header)',
'',
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
'<!-- END doctoc generated TOC please keep comment here to allow auto update -->'
]
, 'wraps old toc'
)
Expand All @@ -349,7 +354,7 @@ test('transforming when old toc exists and --all flag is set', function (t) {
'some content',
'' ]
, 'updates the content with the new toc and ignores header before existing toc'
)
)
t.end()
})

Expand Down Expand Up @@ -547,8 +552,7 @@ test('should use <!-- --> comments if syntax=md', function (t) {
, 'md'
)

var lines = res.wrappedToc.split('\n')
var commentLines = lines.slice(0,2).concat(lines[lines.length-1])
var commentLines = getCommentLines(res);
t.deepEqual(commentLines.every((line) => line.startsWith('<!--') && line.endsWith('-->')), true)
t.end()
})
Expand All @@ -574,9 +578,7 @@ test('should use {/* */} comments if syntax=mdx', function (t) {
, 'mdx'
)

var lines = res.wrappedToc.split('\n')
var commentLines = lines.slice(0 ,2).concat(lines[lines.length - 1])

var commentLines = getCommentLines(res);
t.deepEqual(commentLines.every((line) => line.startsWith('{/*') && line.endsWith('*/}')), true)
t.end()
})

0 comments on commit f502d3c

Please sign in to comment.