diff --git a/package.json b/package.json index 5f1db55..1cdfc94 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "front-matter-editor help you to edit front-matter in markdown file ", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha tests" }, "repository": { "type": "git", diff --git a/tests/front-matter-editor_tests.js b/tests/front-matter-editor_tests.js index cf3a185..fc7a144 100644 --- a/tests/front-matter-editor_tests.js +++ b/tests/front-matter-editor_tests.js @@ -2,66 +2,147 @@ const editor = require('../index'); const path = require('path'); +const fs = require('fs'); const extend = require('util')._extend; +const chai = require('chai'); + +const expect = chai.expect; describe('FrontMatterEditor tests', () =>{ let filePath = path.join(__dirname, 'sample.md'); + let file, originalPrintObject, printedObject; - it('show()', () => { - editor.read(filePath).show(); + beforeEach(() => { + file = editor.read(filePath); + originalPrintObject = editor._printObject; + editor._printObject = (obj) => { + printedObject = obj; + }; }); - it("show('orig')", () => { - editor.read(filePath).show('orig'); + afterEach(() => { + editor._printObject = originalPrintObject; }); - it("show('data')", () => { - editor.read(filePath).show('data'); - }); + describe( 'show()', () => { + it('outputs all parts of parsed page', () => { + file.show(); + expect(printedObject).to.deep.equal({ + content: "\n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it.", + data: { + layout: 'post', + title: 'What is the front-matter-editor?' + }, + orig: "---\nlayout: post\ntitle: What is the front-matter-editor?\n---\n\n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it." + }); + }); - it("show('content')", () => { - editor.read(filePath).show('content'); - }); + it('outputs only orig part of page with "orig" parameter', () => { + file.show('orig'); + expect(printedObject).to.equal( + "---\nlayout: post\ntitle: What is the front-matter-editor?\n---\n\n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it." + ); + }); - it("data()", () => { - editor.read(filePath) - .show('data') - .data((data, matter) => { - matter.data = extend(data, {author: "saltfactory"}); - }) - .show('data'); - }); + it('outputs the front matter data with "data" parameter', () => { + file.show('data'); + expect(printedObject).to.deep.equal({ + layout: 'post', + title: 'What is the front-matter-editor?' + }); + }); - it("content()", () => { - editor.read(filePath) - .show('content') - .content((content, matter) => { - matter.content = `add content ${content}`; - }) - .show('content'); - }); + it('outputs only content part of page with "content" parameter', () => { + file.show('content'); + expect(printedObject).to.equal( + "\n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it." + ); + }); - it("extend()", () => { - editor.read(filePath) - .show() - .extend((data, content, matter) => { - matter.data = extend(data, {author:'saltfactory'}); - matter.content = `add Content ${content}` - }) - .show(); - }); + } ); - it("fileInfo()", () => { - editor.read(filePath) - .fileInfo(); - }); + describe( 'data()', () => { + it('can change the data with a callback', () => { + file.data((data, matter) => { + matter.data = extend(data, {author: 'saltfactory'}); + }).show('data'); + expect(printedObject).to.deep.equal({ + layout: 'post', + title: 'What is the front-matter-editor?', + author: 'saltfactory' + }); + }); - it("save()", () => { - editor.read(filePath) - .data((data, matter) => matter.data.author = "saltfactory") - .save(path.join(__dirname, './'), {prefix:"2016-08-03-", postfix:".bak", filename:"abc.md"}, (err, matter) => { - console.log(matter); + } ); + + describe( 'content()', () => { + it('can change change the page content with a callback', () => { + file.content((content, matter) => { + matter.content = `add content ${content}`; + }).show('content'); + expect(printedObject).to.deep.equal( + "add content \n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it." + ); + }); + + } ); + + describe( 'extend()', () => { + it('can change change the front matter and page content with a callback', () => { + file.extend((data, content, matter) => { + matter.data = extend(data, {author: 'saltfactory'}); + matter.content = `add content ${content}`; + }).show(); + expect(printedObject).to.deep.equal({ + content: "add content \n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it.", + data: { + layout: 'post', + title: 'What is the front-matter-editor?', + author: 'saltfactory' + }, + orig: "---\nlayout: post\ntitle: What is the front-matter-editor?\n---\n\n# Abstract\n\nfront-matter-editor help you to edit front-matter in markdown of jekyll or included it." + }); + }); + + } ); + + describe( 'fileInfo()', () => { + it('outputs file metadata', () => { + file.fileInfo(); + // spot check important properties + expect(printedObject.exists).to.equal(true); + expect(printedObject.flags.isFile).to.equal(true); + expect(printedObject.name).to.equal("sample.md"); + expect(printedObject.extension).to.equal(".md"); }); - }) -}); \ No newline at end of file + } ); + + describe( 'save()', () => { + const expectedOutputPath = path.join(__dirname, './2016-08-03-abc.md.bak'); + + beforeEach(() => { + if (fs.existsSync(expectedOutputPath)) { + fs.unlinkSync(expectedOutputPath); + } + }) + + it('writes changes to specified file', (done) => { + // TODO use mock-fs to avoid writing to the real file system + file.data((data,matter) => { + matter.data = extend(data, {author: 'saltfactory'}); + }).save( + path.join(__dirname, './'), + {prefix:"2016-08-03-", postfix:".bak", filename:"abc.md"}, + (err, matter) => { + expect(err).to.equal(null); + expect(fs.existsSync(expectedOutputPath)).to.equal(true); + expect(fs.readFileSync(expectedOutputPath, 'utf8')).to.contain.string( "\nauthor: saltfactory") + done(); + } + ); + }); + + } ); + +});