diff --git a/lib/jasmine-core/jasmine-html.js b/lib/jasmine-core/jasmine-html.js index e4c34a73..41a52a47 100644 --- a/lib/jasmine-core/jasmine-html.js +++ b/lib/jasmine-core/jasmine-html.js @@ -25,7 +25,6 @@ jasmineRequire.html = function(j$) { j$.HtmlReporter = jasmineRequire.HtmlReporter(j$); j$.QueryString = jasmineRequire.QueryString(); j$.HtmlSpecFilter = jasmineRequire.HtmlSpecFilter(); - j$.matchers.toHaveClass = jasmineRequire.toHaveClass(j$); }; jasmineRequire.HtmlReporter = function(j$) { @@ -600,37 +599,3 @@ jasmineRequire.QueryString = function() { return QueryString; }; - -jasmineRequire.toHaveClass = function(j$) { - /** - * {@link expect} the actual value to be a DOM element that has the expected class - * @function - * @name matchers#toHaveClass - * @param {Object} expected - The class name to test for - * @example - * var el = document.createElement('div'); - * el.className = 'foo bar baz'; - * expect(el).toHaveClass('bar'); - */ - function toHaveClass(util, customEqualityTesters) { - return { - compare: function(actual, expected) { - if (!isElement(actual)) { - throw new Error(j$.pp(actual) + ' is not a DOM element'); - } - - return { - pass: actual.classList.contains(expected) - }; - } - }; - } - - function isElement(maybeEl) { - return maybeEl && - maybeEl.classList && - j$.isFunction_(maybeEl.classList.contains); - } - - return toHaveClass; -}; diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 63e30a66..19a6b58b 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -116,6 +116,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) { 'toHaveBeenCalledBefore', 'toHaveBeenCalledTimes', 'toHaveBeenCalledWith', + 'toHaveClass', 'toMatch', 'toThrow', 'toThrowError', @@ -3773,6 +3774,40 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) { return toHaveBeenCalledWith; }; +getJasmineRequireObj().toHaveClass = function(j$) { + /** + * {@link expect} the actual value to be a DOM element that has the expected class + * @function + * @name matchers#toHaveClass + * @param {Object} expected - The class name to test for + * @example + * var el = document.createElement('div'); + * el.className = 'foo bar baz'; + * expect(el).toHaveClass('bar'); + */ + function toHaveClass(util, customEqualityTesters) { + return { + compare: function(actual, expected) { + if (!isElement(actual)) { + throw new Error(j$.pp(actual) + ' is not a DOM element'); + } + + return { + pass: actual.classList.contains(expected) + }; + } + }; + } + + function isElement(maybeEl) { + return maybeEl && + maybeEl.classList && + j$.isFunction_(maybeEl.classList.contains); + } + + return toHaveClass; +}; + getJasmineRequireObj().toMatch = function(j$) { var getErrorMsg = j$.formatErrorMsg('', 'expect().toMatch( || )'); diff --git a/package.json b/package.json index c7fc8813..8861f9b7 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "grunt-contrib-concat": "^1.0.1", "grunt-contrib-jshint": "^1.0.0", "jasmine": "^3.0.0", + "jsdom": "^9.12.0", "load-grunt-tasks": "^0.4.0", "shelljs": "^0.7.0", "temp": "~0.8.1" diff --git a/spec/html/matchers/toHaveClassSpec.js b/spec/core/matchers/toHaveClassSpec.js similarity index 61% rename from spec/html/matchers/toHaveClassSpec.js rename to spec/core/matchers/toHaveClassSpec.js index f4d1b02b..fdf4a616 100644 --- a/spec/html/matchers/toHaveClassSpec.js +++ b/spec/core/matchers/toHaveClassSpec.js @@ -1,16 +1,38 @@ describe('toHaveClass', function() { + beforeEach(function(done) { + this.createElementWithClassName = function(className) { + var el = this.doc.createElement('div'); + el.className = className; + return el; + } + + if (typeof document !== 'undefined') { + this.doc = document; + done(); + } else { + var jsdom = require('jsdom'); + var self = this; + jsdom.env('', function(err, win) { + if (err) { + done.fail(err); + } else { + self.doc = win.document; + done(); + } + }); + } + }); + it('fails for a DOM element that lacks the expected class', function() { var matcher = jasmineUnderTest.matchers.toHaveClass(), - result = matcher.compare(document.createElement('div'), 'foo'); + result = matcher.compare(this.createElementWithClassName(''), 'foo'); expect(result.pass).toBe(false); }); it('passes for a DOM element that has the expected class', function() { var matcher = jasmineUnderTest.matchers.toHaveClass(), - el = document.createElement('div'); - - el.className = 'foo bar baz'; + el = this.createElementWithClassName('foo bar baz'); expect(matcher.compare(el, 'foo').pass).toBe(true); expect(matcher.compare(el, 'bar').pass).toBe(true); @@ -19,9 +41,7 @@ describe('toHaveClass', function() { it('fails for a DOM element that only has other classes', function() { var matcher = jasmineUnderTest.matchers.toHaveClass(), - el = document.createElement('div'); - - el.className = 'foo bar'; + el = this.createElementWithClassName('foo bar'); expect(matcher.compare(el, 'fo').pass).toBe(false); }); @@ -37,8 +57,9 @@ describe('toHaveClass', function() { matcher.compare(undefined, 'foo'); }).toThrowError('undefined is not a DOM element'); + var textNode = this.doc.createTextNode(''); expect(function() { - matcher.compare(document.createTextNode(''), 'foo') + matcher.compare(textNode, 'foo') }).toThrowError('HTMLNode is not a DOM element'); expect(function() { diff --git a/src/core/matchers/requireMatchers.js b/src/core/matchers/requireMatchers.js index 2a97b5dc..6fb52c77 100644 --- a/src/core/matchers/requireMatchers.js +++ b/src/core/matchers/requireMatchers.js @@ -21,6 +21,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) { 'toHaveBeenCalledBefore', 'toHaveBeenCalledTimes', 'toHaveBeenCalledWith', + 'toHaveClass', 'toMatch', 'toThrow', 'toThrowError', diff --git a/src/html/matchers/toHaveClass.js b/src/core/matchers/toHaveClass.js similarity index 94% rename from src/html/matchers/toHaveClass.js rename to src/core/matchers/toHaveClass.js index e90aa106..99f784f3 100644 --- a/src/html/matchers/toHaveClass.js +++ b/src/core/matchers/toHaveClass.js @@ -1,4 +1,4 @@ -jasmineRequire.toHaveClass = function(j$) { +getJasmineRequireObj().toHaveClass = function(j$) { /** * {@link expect} the actual value to be a DOM element that has the expected class * @function diff --git a/src/html/requireHtml.js b/src/html/requireHtml.js index 8d8b7b40..b60c3ba5 100644 --- a/src/html/requireHtml.js +++ b/src/html/requireHtml.js @@ -3,5 +3,4 @@ jasmineRequire.html = function(j$) { j$.HtmlReporter = jasmineRequire.HtmlReporter(j$); j$.QueryString = jasmineRequire.QueryString(); j$.HtmlSpecFilter = jasmineRequire.HtmlSpecFilter(); - j$.matchers.toHaveClass = jasmineRequire.toHaveClass(j$); };