Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit ad5f3aa

Browse files
shelbydjuliemr
authored andcommitted
feat(jasminewd): allow custom matchers to return promises
Allow custom jasmine matchers to return a promise which resolves to a boolean and match against the resolution of the promise
1 parent 02defe3 commit ad5f3aa

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

jasminewd/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ global.expect = function(actual) {
148148
}
149149
};
150150

151+
// Wrap internal Jasmine function to allow custom matchers
152+
// to return promises that resolve to truthy or falsy values
153+
var originalMatcherFn = jasmine.Matchers.matcherFn_;
154+
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
155+
var matcherFnThis = this;
156+
var matcherFnArgs = jasmine.util.argsToArray(arguments);
157+
return function() {
158+
var matcherThis = this;
159+
var matcherArgs = jasmine.util.argsToArray(arguments);
160+
var result = matcherFunction.apply(this, arguments);
161+
162+
if (result instanceof webdriver.promise.Promise) {
163+
result.then(function(resolution) {
164+
matcherFnArgs[1] = function() {
165+
return resolution;
166+
};
167+
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
168+
apply(matcherThis, matcherArgs);
169+
});
170+
} else {
171+
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
172+
apply(matcherThis, matcherArgs);
173+
}
174+
};
175+
};
176+
151177
/**
152178
* A Jasmine reporter which does nothing but execute the input function
153179
* on a timeout failure.

jasminewd/spec/adapterSpec.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ var getFakeDriver = function() {
4747
return flow.execute(function() {
4848
return webdriver.promise.fulfilled(3.14159);
4949
});
50-
}
50+
},
51+
getDisplayedElement: function() {
52+
return flow.execute(function() {
53+
return webdriver.promise.fulfilled({
54+
isDisplayed: function() {
55+
return webdriver.promise.fulfilled(true);
56+
}
57+
});
58+
});
59+
},
60+
getHiddenElement: function() {
61+
return flow.execute(function() {
62+
return webdriver.promise.fulfilled({
63+
isDisplayed: function() {
64+
return webdriver.promise.fulfilled(false);
65+
}
66+
});
67+
});
68+
}
5169
};
5270
};
5371

@@ -69,6 +87,10 @@ describe('webdriverJS Jasmine adapter', function() {
6987
this.addMatchers({
7088
toBeLotsMoreThan: function(expected) {
7189
return this.actual > expected + 100;
90+
},
91+
// Example custom matcher returning a promise that resolves to true/false.
92+
toBeDisplayed: function() {
93+
return this.actual.isDisplayed();
7294
}
7395
});
7496
});
@@ -116,6 +138,11 @@ describe('webdriverJS Jasmine adapter', function() {
116138
expect(fakeDriver.getBigNumber()).toBeLotsMoreThan(33);
117139
});
118140

141+
it('should allow custom matchers to return a promise', function() {
142+
expect(fakeDriver.getDisplayedElement()).toBeDisplayed();
143+
expect(fakeDriver.getHiddenElement()).not.toBeDisplayed();
144+
});
145+
119146
it('should pass multiple arguments to matcher', function() {
120147
// Passing specific precision
121148
expect(fakeDriver.getDecimalNumber()).toBeCloseTo(3.1, 1);

0 commit comments

Comments
 (0)