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

Add expectations in unit tests #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
173 changes: 127 additions & 46 deletions tests/front-matter-editor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
})

});
} );

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();
}
);
});

} );

});