Merge branch 'enelson/spystrategies' of https://github.com/elliot-nelson/jasmine into elliot-nelson-enelson/spystrategies

- Merges #1688 from @enelson
- See #1590
This commit is contained in:
Gregg Van Hove
2019-05-09 17:27:42 -07:00
8 changed files with 275 additions and 38 deletions

View File

@@ -46,6 +46,21 @@ describe("Env", function() {
}));
});
describe('promise library', function() {
it('can be configured with a custom library', function() {
var myLibrary = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() };
env.configure({ Promise: myLibrary });
});
it('cannot be configured with an invalid promise library', function() {
var myLibrary = {};
expect(function() {
env.configure({ Promise: myLibrary });
}).toThrowError('Custom promise library missing `resolve`/`reject` functions');
});
});
it('defaults to multiple failures for specs', function() {
spyOn(jasmineUnderTest, 'Spec');
env.it('bar', function() {});

View File

@@ -171,7 +171,29 @@ describe('Spies', function () {
expect(spy('foo')).toEqual(17);
});
describe("When withArgs is used without a base strategy", function() {
describe('any promise-based strategy', function() {
it('works with global Promise library when available', function(done) {
jasmine.getEnv().requirePromises();
var spy = env.createSpy('foo').and.resolveWith(42);
spy().then(function(result) {
expect(result).toEqual(42);
done();
}).catch(done.fail);
});
it('works with a custom Promise library', function() {
var customPromise = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() };
customPromise.resolve.and.returnValue('resolved');
env.configure({ Promise: customPromise });
var spy = env.createSpy('foo').and.resolveWith(42);
expect(spy()).toEqual('resolved');
expect(customPromise.resolve).toHaveBeenCalledWith(42);
});
});
describe("when withArgs is used without a base strategy", function() {
it("uses the matching strategy", function() {
var spy = env.createSpy('foo');
spy.withArgs('baz').and.returnValue(-1);

View File

@@ -110,6 +110,70 @@ describe("SpyStrategy", function() {
})
});
describe("#resolveWith", function() {
it("allows a resolved promise to be returned", function(done) {
jasmine.getEnv().requirePromises();
var originalFn = jasmine.createSpy("original"),
getPromise = function() { return Promise; },
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise});
spyStrategy.resolveWith(37);
spyStrategy.exec().then(function (returnValue) {
expect(returnValue).toEqual(37);
done();
}).catch(done.fail);
});
it("fails if promises are not available", function() {
var originalFn = jasmine.createSpy("original"),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
expect(function() {
spyStrategy.resolveWith(37);
}).toThrowError('resolveWith requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`');
});
});
describe("#rejectWith", function() {
it("allows a rejected promise to be returned", function(done) {
jasmine.getEnv().requirePromises();
var originalFn = jasmine.createSpy("original"),
getPromise = function() { return Promise; },
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise});
spyStrategy.rejectWith(new Error('oops'));
spyStrategy.exec().then(done.fail).catch(function (error) {
expect(error).toEqual(new Error('oops'));
done();
}).catch(done.fail);
});
it("allows a non-Error to be rejected, wrapping it in an exception when executed", function(done) {
jasmine.getEnv().requirePromises();
var originalFn = jasmine.createSpy("original"),
getPromise = function() { return Promise; },
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn, getPromise: getPromise});
spyStrategy.rejectWith('oops');
spyStrategy.exec().then(done.fail).catch(function (error) {
expect(error).toEqual(new Error('oops'));
done();
}).catch(done.fail);
});
it("fails if promises are not available", function() {
var originalFn = jasmine.createSpy("original"),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
expect(function() {
spyStrategy.rejectWith(new Error('oops'));
}).toThrowError('rejectWith requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`');
});
});
it("allows a custom strategy to be used", function() {
var plan = jasmine.createSpy('custom strategy')
.and.returnValue('custom strategy result'),