Add resolveValue and rejectValue spy strategies

This commit is contained in:
Elliot Nelson
2019-05-05 08:08:34 -04:00
parent afb24d1050
commit 72aa0bbe4d
6 changed files with 153 additions and 1 deletions

View File

@@ -46,6 +46,23 @@ describe("Env", function() {
}));
});
it('can configure a custom promise library', function() {
var myLibrary = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() };
expect(env.getPromise()).toBeUndefined();
env.configure({ promiseLibrary: myLibrary });
expect(env.getPromise()).toBe(myLibrary);
});
it('fails to configure a custom promise library if library is invalid', function() {
var myLibrary = {};
expect(env.getPromise()).toBeUndefined();
expect(function() {
env.configure({ promiseLibrary: 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

@@ -110,6 +110,62 @@ describe("SpyStrategy", function() {
})
});
describe("#resolveValue", function() {
it("allows a resolved promise to be returned", function(done) {
if (!jasmineUnderTest.getPromise()) {
pending('Environment does not support promises.');
}
var originalFn = jasmine.createSpy("original"),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
spyStrategy.resolveValue(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});
spyOn(jasmineUnderTest, 'getPromise');
expect(function() {
spyStrategy.resolveValue(37);
}).toThrowError('resolveValue is unavailable because the environment does not support promises.');
});
});
describe("#rejectValue", function() {
it("allows a rejected promise to be returned", function(done) {
if (!jasmineUnderTest.getPromise()) {
pending('Environment does not support promises.');
}
var originalFn = jasmine.createSpy("original"),
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
spyStrategy.rejectValue(new Error('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});
spyOn(jasmineUnderTest, 'getPromise');
expect(function() {
spyStrategy.rejectValue(new Error('oops'));
}).toThrowError('rejectValue is unavailable because the environment does not support promises.');
});
});
it("allows a custom strategy to be used", function() {
var plan = jasmine.createSpy('custom strategy')
.and.returnValue('custom strategy result'),

View File

@@ -27,4 +27,19 @@ describe('base helpers', function() {
}, 100);
});
});
describe('getPromise', function() {
it('returns a custom library if configured', function() {
var myLibrary = { resolve: jasmine.createSpy(), reject: jasmine.createSpy() };
jasmineUnderTest.getEnv().configure({ promiseLibrary: myLibrary });
expect(jasmineUnderTest.getPromise()).toBe(myLibrary);
});
it('returns global library if not configured', function() {
var globalLibrary = {};
var global = { Promise: globalLibrary };
spyOn(jasmineUnderTest, 'getGlobal').and.returnValue(global);
expect(jasmineUnderTest.getPromise()).toBe(globalLibrary);
});
});
});