Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
k88hudson committed Jul 15, 2016
1 parent 1b88019 commit 34cfd18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ function getMetadata(doc, rules) {

class MetadataParser {
constructor(customRules = {}, options = {}) {
this._rules = options.replace ? customRules : Object.assign({}, MetadataParser.metadataRules, customRules);
if (options.replace) {
this._rules = customRules;
} else {
this._rules = Object.assign({}, MetadataParser.metadataRules, customRules);
}
}
get rules() {
return this._rules;
Expand Down
26 changes: 26 additions & 0 deletions tests/MetadataParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const {assert} = require('chai');
const MetadataParser = require('../parser');
const {stringToDom} = require('./test-utils');
const DEFAULT_RULESET = require("../lib/default-ruleset");

describe("MetadataParser", () => {
it("should extend the normal rules", () => {
const parser = new MetadataParser({
site_name: [['meta[property="og:site_name"]', node => node.element.content]]
});
const result = parser.getMetadata(stringToDom('<head><meta property="og:site_name" content="foo" /></head>'));
assert.equal(result.site_name, "foo");
});

it("should should replace the normal rules", () => {
const newRules = {
foo: [['meta[property="og:foo"]', node => node.element.content]]
};
const parser = new MetadataParser(newRules, {replace: true});
const result = parser.getMetadata(stringToDom(`<head>
<meta property="og:foo" content="bar" />
<meta name="description" content="asdasd" />
</head>`));
assert.deepEqual(result, {foo: "bar"});
});
});

0 comments on commit 34cfd18

Please sign in to comment.