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

Commit

Permalink
feat(jasminewd): allow custom matchers to return promises
Browse files Browse the repository at this point in the history
Allow custom jasmine matchers to return a promise which resolves to a boolean
and match against the resolution of the promise
  • Loading branch information
shelbyd authored and juliemr committed Feb 28, 2014
1 parent 02defe3 commit ad5f3aa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
26 changes: 26 additions & 0 deletions jasminewd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ global.expect = function(actual) {
}
};

// Wrap internal Jasmine function to allow custom matchers
// to return promises that resolve to truthy or falsy values
var originalMatcherFn = jasmine.Matchers.matcherFn_;
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
var matcherFnThis = this;
var matcherFnArgs = jasmine.util.argsToArray(arguments);
return function() {
var matcherThis = this;
var matcherArgs = jasmine.util.argsToArray(arguments);
var result = matcherFunction.apply(this, arguments);

if (result instanceof webdriver.promise.Promise) {
result.then(function(resolution) {
matcherFnArgs[1] = function() {
return resolution;
};
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
apply(matcherThis, matcherArgs);
});
} else {
originalMatcherFn.apply(matcherFnThis, matcherFnArgs).
apply(matcherThis, matcherArgs);
}
};
};

/**
* A Jasmine reporter which does nothing but execute the input function
* on a timeout failure.
Expand Down
29 changes: 28 additions & 1 deletion jasminewd/spec/adapterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,25 @@ var getFakeDriver = function() {
return flow.execute(function() {
return webdriver.promise.fulfilled(3.14159);
});
}
},
getDisplayedElement: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled({
isDisplayed: function() {
return webdriver.promise.fulfilled(true);
}
});
});
},
getHiddenElement: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled({
isDisplayed: function() {
return webdriver.promise.fulfilled(false);
}
});
});
}
};
};

Expand All @@ -69,6 +87,10 @@ describe('webdriverJS Jasmine adapter', function() {
this.addMatchers({
toBeLotsMoreThan: function(expected) {
return this.actual > expected + 100;
},
// Example custom matcher returning a promise that resolves to true/false.
toBeDisplayed: function() {
return this.actual.isDisplayed();
}
});
});
Expand Down Expand Up @@ -116,6 +138,11 @@ describe('webdriverJS Jasmine adapter', function() {
expect(fakeDriver.getBigNumber()).toBeLotsMoreThan(33);
});

it('should allow custom matchers to return a promise', function() {
expect(fakeDriver.getDisplayedElement()).toBeDisplayed();
expect(fakeDriver.getHiddenElement()).not.toBeDisplayed();
});

it('should pass multiple arguments to matcher', function() {
// Passing specific precision
expect(fakeDriver.getDecimalNumber()).toBeCloseTo(3.1, 1);
Expand Down

0 comments on commit ad5f3aa

Please sign in to comment.