UNPKG

6.75 kBJavaScriptView Raw
1import fs from 'fs-extra'
2import path from 'path'
3import test from 'ava'
4import sinon from 'sinon'
5import markdownMagic from '../index'
6
7const markdownPath = path.join(__dirname, 'fixtures', 'test.md')
8const outputDir = path.join(__dirname, 'fixtures', 'output')
9const DEBUG = false
10
11/**
12 * Test markdownMagic Function
13 */
14test('if valid string path supplied', t => {
15 markdownMagic(markdownPath)
16 t.pass()
17 // emptyDirectory(outputDir)
18})
19
20test('if valid glob pattern supplied', t => {
21 const config = {
22 outputDir: outputDir
23 }
24 markdownMagic(['test/fixtures/**/*md', '!test/fixtures/output/*.md'], config)
25 t.pass()
26 // empty dir
27 // fs.emptyDirSync(outputDir)
28})
29
30test('if valid config supplied', t => {
31 const config = {}
32 markdownMagic(markdownPath, config)
33 t.pass()
34 // emptyDirectory(outputDir)
35})
36
37test('if callback function supplied, call it once', t => {
38 const callback = sinon.spy()
39 const config = {}
40 markdownMagic(markdownPath, config, callback)
41 t.true(callback.calledOnce)
42 // emptyDirectory(outputDir)
43})
44
45test('if callback function supplied, as second arg, call it once', t => {
46 const callback = sinon.spy()
47 markdownMagic(markdownPath, callback)
48 t.true(callback.calledOnce)
49 // emptyDirectory(outputDir)
50})
51
52/**
53 * Test Config settings
54 */
55test('if config.outputDir supplied, make new file', t => {
56 const config = {
57 outputDir: outputDir
58 }
59 markdownMagic(markdownPath, config, function() {
60 const newfile = path.join(outputDir, 'test.md')
61 const fileWasCreated = filePathExists(newfile)
62 t.true(fileWasCreated)
63 // remove test file after assertion
64 // emptyDirectory(outputDir)
65 })
66})
67
68test('if config.matchWord supplied, use it for comment matching', t => {
69 const filePath = path.join(__dirname, 'fixtures', 'custom-match-word-test.md')
70 const config = {
71 matchWord: 'YOLO',
72 outputDir: outputDir
73 }
74 markdownMagic(filePath, config)
75 const newfile = path.join(config.outputDir, 'custom-match-word-test.md')
76 const newContent = fs.readFileSync(newfile, 'utf8')
77 t.regex(newContent, /module\.exports\.run/, 'local code snippet inserted')
78
79 // remove test file after assertion
80 fs.emptyDirSync(outputDir)
81})
82
83test('<!-- AUTO-GENERATED-CONTENT:START (TOC)-->', t => {
84 const filePath = path.join(__dirname, 'fixtures', 'TOC-test.md')
85 const config = {
86 outputDir: outputDir
87 }
88 markdownMagic(filePath, config)
89 const newfile = path.join(config.outputDir, 'TOC-test.md')
90 const newContent = fs.readFileSync(newfile, 'utf8')
91
92 const expectedTest1 = `
93<!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #1: without option and the content with empty line -->
94- [Title A](#title-a)
95 * [Subtitle z](#subtitle-z)
96 * [Subtitle x](#subtitle-x)
97- [Title B](#title-b)
98- [Title C](#title-c)
99<!-- AUTO-GENERATED-CONTENT:END -->`
100 const regexTest1 = new RegExp(`(?=${expectedTest1.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
101 t.regex(newContent, regexTest1, 'Test #1 : without option and the content with empty line')
102
103 const expectedTest2 = `
104<!-- AUTO-GENERATED-CONTENT:START (TOC:collapse=true&collapseText=Click Me) - Test #2: with collapse options and the content with 'aaaaaaaaa' -->
105<details>
106<summary>Click Me</summary>
107
108- [Title A](#title-a)
109 * [Subtitle z](#subtitle-z)
110 * [Subtitle x](#subtitle-x)
111- [Title B](#title-b)
112- [Title C](#title-c)
113
114</details>
115<!-- AUTO-GENERATED-CONTENT:END -->`
116 const regexTest2 = new RegExp(`(?=${expectedTest2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
117 t.regex(newContent, regexTest2, "Test #2: with collapse options and the content with 'aaaaaaaaa'")
118
119 const expectedTest3 = `
120<!-- AUTO-GENERATED-CONTENT:START (TOC:collapse=true&collapseText=Click Me=I have the power) - Test #3: with collapseText contains character '=' -->
121<details>
122<summary>Click Me=I have the power</summary>
123
124- [Title A](#title-a)
125 * [Subtitle z](#subtitle-z)
126 * [Subtitle x](#subtitle-x)
127- [Title B](#title-b)
128- [Title C](#title-c)
129
130</details>
131<!-- AUTO-GENERATED-CONTENT:END -->`
132 const regexTest3 = new RegExp(`(?=${expectedTest3.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
133 t.regex(newContent, regexTest3, "Test #3: with collapseText contains character '='")
134
135 const expectedTest4 = `
136<!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #4: without option and the content is empty -->
137- [Title A](#title-a)
138 * [Subtitle z](#subtitle-z)
139 * [Subtitle x](#subtitle-x)
140- [Title B](#title-b)
141- [Title C](#title-c)
142<!-- AUTO-GENERATED-CONTENT:END -->`
143 const regexTest4 = new RegExp(`(?=${expectedTest4.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
144 t.regex(newContent, regexTest4, 'Test #4 : without option and the content is empty')
145
146 const expectedTest5 = `
147<!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #5: without option and tags with same line -->
148- [Title A](#title-a)
149 * [Subtitle z](#subtitle-z)
150 * [Subtitle x](#subtitle-x)
151- [Title B](#title-b)
152- [Title C](#title-c)
153<!-- AUTO-GENERATED-CONTENT:END -->`
154 const regexTest5 = new RegExp(`(?=${expectedTest5.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
155 t.regex(newContent, regexTest5, 'Test #5 : without option and tags with same line')
156
157 // remove test file after assertion
158 fs.emptyDirSync(outputDir)
159})
160
161/**
162 * Test Built in transforms
163 */
164test('<!-- AUTO-GENERATED-CONTENT:START (CODE)-->', t => {
165 const filePath = path.join(__dirname, 'fixtures', 'CODE-test.md')
166 const config = { outputDir: outputDir }
167 const newfile = path.join(config.outputDir, 'CODE-test.md')
168
169 markdownMagic(filePath, config, function(err, data) {
170 // console.log('data', data)
171 const newContent = fs.readFileSync(newfile, 'utf8')
172 // check local code
173 t.regex(newContent, /module\.exports\.run/, 'local code snippet inserted')
174 // check remotely fetched code
175 t.regex(newContent, /require\('dox'\)/, 'remote code snippet inserted')
176 })
177
178 if (filePathExists(newfile)) {
179 // fs.emptyDirSync(outputDir)
180 }
181 // remove test file after assertion
182})
183
184test('<!-- AUTO-GENERATED-CONTENT:START (REMOTE)-->', t => {
185 const filePath = path.join(__dirname, 'fixtures', 'REMOTE-test.md')
186
187 const config = { outputDir: outputDir }
188 markdownMagic(filePath, config, function() {
189 const newfile = path.join(config.outputDir, 'REMOTE-test.md')
190 const newContent = fs.readFileSync(newfile, 'utf8')
191 // check local code
192 t.regex(newContent, /Markdown Magic/, 'word "Markdown Magic" not found in remote block')
193 // remove test file after assertion
194 fs.emptyDirSync(outputDir)
195 })
196})
197
198/*
199 Util functions
200*/
201function filePathExists(fp) {
202 try {
203 fs.accessSync(fp)
204 return true
205 } catch (err) {
206 return false
207 }
208}
209
210function emptyDirectory(filePath, callBack) {
211 fs.emptyDirSync(filePath)
212 callBack && callBack(null)
213}