diff --git a/modules/angular2/src/test_lib/test_lib.dart b/modules/angular2/src/test_lib/test_lib.dart index d007a7b4e4..812100ed13 100644 --- a/modules/angular2/src/test_lib/test_lib.dart +++ b/modules/angular2/src/test_lib/test_lib.dart @@ -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; } diff --git a/modules/angular2/src/test_lib/test_lib.ts b/modules/angular2/src/test_lib/test_lib.ts index 2e29d92bc0..51b0845f53 100644 --- a/modules/angular2/src/test_lib/test_lib.ts +++ b/modules/angular2/src/test_lib/test_lib.ts @@ -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) { diff --git a/modules/angular2/test/test_lib/test_lib_spec.ts b/modules/angular2/test/test_lib/test_lib_spec.ts index be71ce3496..51e6b96052 100644 --- a/modules/angular2/test/test_lib/test_lib_spec.ts +++ b/modules/angular2/test/test_lib/test_lib_spec.ts @@ -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});