diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-02-11 22:12:07 +0100 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-02-11 22:12:07 +0100 |
commit | 756694c029218510592418deb8aaf6f3b36f95c3 (patch) | |
tree | 86d938cbf6360d46845758a7cc9575ca04aa3594 /test/assertions.js | |
parent | 669f3b39849890c48171d807225cd6eaa3c9086b (diff) | |
download | gitbook-756694c029218510592418deb8aaf6f3b36f95c3.zip gitbook-756694c029218510592418deb8aaf6f3b36f95c3.tar.gz gitbook-756694c029218510592418deb8aaf6f3b36f95c3.tar.bz2 |
Page output a simple html string
Diffstat (limited to 'test/assertions.js')
-rw-r--r-- | test/assertions.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/test/assertions.js b/test/assertions.js new file mode 100644 index 0000000..b936995 --- /dev/null +++ b/test/assertions.js @@ -0,0 +1,62 @@ +var fs = require('fs'); +var _ = require('lodash'); +var cheerio = require('cheerio'); +var should = require('should'); + +// Assertions to test if an Output has generated a file +should.Assertion.add('file', function(file, description) { + this.params = { + actual: this.obj.toString(), + operator: 'have file ' + file, + message: description + }; + + this.obj.should.have.property('resolve').which.is.a.Function; + this.assert(fs.existsSync(this.obj.resolve(file))); +}); + +should.Assertion.add('html', function(rules, description) { + this.params = { actual: 'HTML string', operator: 'valid html', message: description }; + var $ = cheerio.load(this.obj); + + _.each(rules, function(validations, query) { + validations = _.defaults(validations || {}, { + // Select a specific element in the list of matched elements + index: null, + + // Check that there is the correct count of elements + count: 1, + + // Check attribute values + attributes: {}, + + // Trim inner text + trim: false, + + // Check inner text + text: undefined + }); + + var $el = $(query); + + // Select correct element + if (_.isNumber(validations.index)) $el = $($el.get(validations.index)); + + // Test number of elements + $el.length.should.be.equal(validations.count); + + // Test text + if (validations.text !== undefined) { + var text = $el.text(); + if (validations.trim) text = text.trim(); + text.should.be.equal(validations.text); + } + + // Test attributes + _.each(validations.attributes, function(value, name) { + var attr = $el.attr(name); + should(attr).be.ok(); + attr.should.be.equal(value); + }); + }); +}); |