test(matchers): add support for toHaveClass in tests

This commit is contained in:
Matias Niemelä 2015-08-07 16:45:26 -07:00
parent 574bbea747
commit 24eabb9bb1
3 changed files with 33 additions and 0 deletions

View File

@ -100,6 +100,7 @@ class Expect extends gns.Expect {
void toThrowError([message = ""]) => toThrowWith(message: message);
void toThrowErrorWith(message) => expectException(this.actual, message);
void toBePromise() => gns.guinness.matchers.toBeTrue(actual is Future);
void toHaveCssClass(className) => gns.guinness.matchers.toBeTrue(DOM.hasClass(actual, className));
void toImplement(expected) => toBeA(expected);
void toBeNaN() =>
gns.guinness.matchers.toBeTrue(double.NAN.compareTo(actual) == 0);
@ -138,6 +139,7 @@ class NotExpect extends gns.NotExpect {
void toEqual(expected) => toHaveSameProps(expected);
void toBePromise() => gns.guinness.matchers.toBeFalse(actual is Future);
void toHaveCssClass(className) => gns.guinness.matchers.toBeFalse(DOM.hasClass(actual, className));
void toBeNull() => gns.guinness.matchers.toBeFalse(actual == null);
Function get _expect => gns.guinness.matchers.expect;
}

View File

@ -24,6 +24,7 @@ export interface NgMatchers extends jasmine.Matchers {
toBePromise(): boolean;
toBeAnInstanceOf(expected: any): boolean;
toHaveText(expected: any): boolean;
toHaveCssClass(expected: any): boolean;
toImplement(expected: any): boolean;
toContainError(expected: any): boolean;
toThrowErrorWith(expectedMessage: any): boolean;
@ -244,6 +245,21 @@ _global.beforeEach(function() {
};
},
toHaveCssClass: function() {
return {compare: buildError(false), negativeCompare: buildError(true)};
function buildError(isNot) {
return function(actual, className) {
return {
pass: DOM.hasClass(actual, className) == !isNot,
get message() {
return `Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}to contain the CSS class "${className}"`;
}
};
};
}
},
toContainError: function() {
return {
compare: function(actual, expectedText) {

View File

@ -11,6 +11,7 @@ import {
containsRegexp
} from 'angular2/test_lib';
import {DOM} from 'angular2/src/dom/dom_adapter';
import {MapWrapper} from 'angular2/src/facade/collection';
import {IMPLEMENTS, RegExpWrapper} from 'angular2/src/facade/lang';
@ -42,6 +43,20 @@ export function main() {
});
});
describe("toHaveCssClass", () => {
it("should assert that the CSS class is present", () => {
var el = DOM.createElement('div');
DOM.addClass(el, 'matias');
expect(el).toHaveCssClass('matias');
});
it("should assert that the CSS class is not present", () => {
var el = DOM.createElement('div');
DOM.addClass(el, 'matias');
expect(el).not.toHaveCssClass('fatias');
});
});
describe('toEqual for Maps', () => {
it('should detect equality for same reference', () => {
var m1 = MapWrapper.createFromStringMap({'a': 1});