test(matchers): add support for toHaveCssStyle matcher

This commit is contained in:
Matias Niemelä
2015-11-17 12:37:39 -08:00
parent e1d7bdcfe7
commit fad354904d
10 changed files with 94 additions and 2 deletions
@@ -6,6 +6,8 @@ import 'package:guinness/guinness.dart' as gns;
import 'package:angular2/src/platform/dom/dom_adapter.dart' show DOM;
import 'package:angular2/src/facade/lang.dart' show isString;
Expect expect(actual, [matcher]) {
final expect = new Expect(actual);
if (matcher != null) expect.to(matcher);
@@ -14,6 +16,18 @@ Expect expect(actual, [matcher]) {
const _u = const Object();
bool elementContainsStyle(element, styles) {
var allPassed = true;
if (isString(styles)) {
allPassed = DOM.hasStyle(element, styles);
} else {
styles.forEach((prop, style) {
allPassed = allPassed && DOM.hasStyle(element, prop, style);
});
}
return allPassed;
}
expectErrorMessage(actual, expectedMessage) {
expect(actual.toString()).toContain(expectedMessage);
}
@@ -38,6 +52,9 @@ class Expect extends gns.Expect {
void toBePromise() => gns.guinness.matchers.toBeTrue(actual is Future);
void toHaveCssClass(className) =>
gns.guinness.matchers.toBeTrue(DOM.hasClass(actual, className));
void toHaveCssStyle(styles) {
gns.guinness.matchers.toBeTrue(elementContainsStyle(actual, styles));
}
void toImplement(expected) => toBeA(expected);
void toBeNaN() =>
gns.guinness.matchers.toBeTrue(double.NAN.compareTo(actual) == 0);
@@ -78,6 +95,9 @@ class NotExpect extends gns.NotExpect {
void toBePromise() => gns.guinness.matchers.toBeFalse(actual is Future);
void toHaveCssClass(className) =>
gns.guinness.matchers.toBeFalse(DOM.hasClass(actual, className));
void toHaveCssStyle(styles) {
gns.guinness.matchers.toBeFalse(elementContainsStyle(actual, styles));
}
void toBeNull() => gns.guinness.matchers.toBeFalse(actual == null);
Function get _expect => gns.guinness.matchers.expect;
}
+28 -1
View File
@@ -1,5 +1,6 @@
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {global} from 'angular2/src/facade/lang';
import {global, isString} from 'angular2/src/facade/lang';
import {StringMapWrapper} from 'angular2/src/facade/collection';
export interface NgMatchers extends jasmine.Matchers {
@@ -7,6 +8,7 @@ export interface NgMatchers extends jasmine.Matchers {
toBeAnInstanceOf(expected: any): boolean;
toHaveText(expected: any): boolean;
toHaveCssClass(expected: any): boolean;
toHaveCssStyle(expected: any): boolean;
toImplement(expected: any): boolean;
toContainError(expected: any): boolean;
toThrowErrorWith(expectedMessage: any): boolean;
@@ -105,6 +107,31 @@ _global.beforeEach(function() {
}
},
toHaveCssStyle: function() {
return {
compare: function(actual, styles) {
var allPassed;
if (isString(styles)) {
allPassed = DOM.hasStyle(actual, styles);
} else {
allPassed = !StringMapWrapper.isEmpty(styles);
StringMapWrapper.forEach(styles, (style, prop) => {
allPassed = allPassed && DOM.hasStyle(actual, prop, style);
});
}
return {
pass: allPassed,
get message() {
var expectedValueStr = isString(styles) ? styles : JSON.stringify(styles);
return `Expected ${actual.outerHTML} ${!allPassed ? ' ' : 'not '}to contain the
CSS ${isString(styles) ? 'property' : 'styles'} "${expectedValueStr}"`;
}
};
}
};
},
toContainError: function() {
return {
compare: function(actual, expectedText) {