add prettier and eslint
This commit is contained in:
@@ -1,20 +1,19 @@
|
||||
describe("SpyStrategy", function() {
|
||||
|
||||
it("defaults its name to unknown", function() {
|
||||
describe('SpyStrategy', function() {
|
||||
it('defaults its name to unknown', function() {
|
||||
var spyStrategy = new jasmineUnderTest.SpyStrategy();
|
||||
|
||||
expect(spyStrategy.identity).toEqual("unknown");
|
||||
expect(spyStrategy.identity).toEqual('unknown');
|
||||
});
|
||||
|
||||
it("takes a name", function() {
|
||||
var spyStrategy = new jasmineUnderTest.SpyStrategy({name: "foo"});
|
||||
it('takes a name', function() {
|
||||
var spyStrategy = new jasmineUnderTest.SpyStrategy({ name: 'foo' });
|
||||
|
||||
expect(spyStrategy.identity).toEqual("foo");
|
||||
expect(spyStrategy.identity).toEqual('foo');
|
||||
});
|
||||
|
||||
it("stubs an original function, if provided", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
|
||||
it('stubs an original function, if provided', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyStrategy.exec();
|
||||
|
||||
@@ -22,22 +21,22 @@ describe("SpyStrategy", function() {
|
||||
});
|
||||
|
||||
it("allows an original function to be called, passed through the params and returns it's value", function() {
|
||||
var originalFn = jasmine.createSpy("original").and.returnValue(42),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
var originalFn = jasmine.createSpy('original').and.returnValue(42),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn }),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callThrough();
|
||||
returnValue = spyStrategy.exec(null, ["foo"]);
|
||||
returnValue = spyStrategy.exec(null, ['foo']);
|
||||
|
||||
expect(originalFn).toHaveBeenCalled();
|
||||
expect(originalFn.calls.mostRecent().args).toEqual(["foo"]);
|
||||
expect(originalFn.calls.mostRecent().args).toEqual(['foo']);
|
||||
expect(returnValue).toEqual(42);
|
||||
});
|
||||
|
||||
it("can return a specified value when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
it('can return a specified value when executed', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn }),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.returnValue(17);
|
||||
returnValue = spyStrategy.exec();
|
||||
@@ -46,9 +45,9 @@ describe("SpyStrategy", function() {
|
||||
expect(returnValue).toEqual(17);
|
||||
});
|
||||
|
||||
it("can return specified values in order specified when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
|
||||
it('can return specified values in order specified when executed', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyStrategy.returnValues('value1', 'value2', 'value3');
|
||||
|
||||
@@ -59,31 +58,35 @@ describe("SpyStrategy", function() {
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows an exception to be thrown when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
|
||||
it('allows an exception to be thrown when executed', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyStrategy.throwError(new TypeError("bar"));
|
||||
spyStrategy.throwError(new TypeError('bar'));
|
||||
|
||||
expect(function() { spyStrategy.exec(); }).toThrowError(TypeError, "bar");
|
||||
expect(function() {
|
||||
spyStrategy.exec();
|
||||
}).toThrowError(TypeError, 'bar');
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows a non-Error to be thrown, wrapping it into an exception when executed", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn});
|
||||
it('allows a non-Error to be thrown, wrapping it into an exception when executed', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyStrategy.throwError("bar");
|
||||
spyStrategy.throwError('bar');
|
||||
|
||||
expect(function() { spyStrategy.exec(); }).toThrowError(Error, "bar");
|
||||
expect(function() {
|
||||
spyStrategy.exec();
|
||||
}).toThrowError(Error, 'bar');
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows a fake function to be called instead", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
fakeFn = jasmine.createSpy("fake").and.returnValue(67),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
it('allows a fake function to be called instead', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
fakeFn = jasmine.createSpy('fake').and.returnValue(67),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn }),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callFake(fakeFn);
|
||||
returnValue = spyStrategy.exec();
|
||||
@@ -92,92 +95,128 @@ describe("SpyStrategy", function() {
|
||||
expect(returnValue).toEqual(67);
|
||||
});
|
||||
|
||||
it("allows a fake async function to be called instead", function(done) {
|
||||
it('allows a fake async function to be called instead', function(done) {
|
||||
jasmine.getEnv().requireAsyncAwait();
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
fakeFn = jasmine.createSpy("fake").and.callFake(eval("async () => { return 67; }")),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
fakeFn = jasmine
|
||||
.createSpy('fake')
|
||||
.and.callFake(eval('async () => { return 67; }')),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyStrategy.callFake(fakeFn);
|
||||
spyStrategy.exec().then(function (returnValue) {
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
expect(fakeFn).toHaveBeenCalled();
|
||||
expect(returnValue).toEqual(67);
|
||||
done();
|
||||
}).catch(function (err) {
|
||||
done.fail(err);
|
||||
})
|
||||
spyStrategy
|
||||
.exec()
|
||||
.then(function(returnValue) {
|
||||
expect(originalFn).not.toHaveBeenCalled();
|
||||
expect(fakeFn).toHaveBeenCalled();
|
||||
expect(returnValue).toEqual(67);
|
||||
done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
done.fail(err);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#resolveWith", function() {
|
||||
it("allows a resolved promise to be returned", function(done) {
|
||||
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});
|
||||
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);
|
||||
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});
|
||||
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()`');
|
||||
}).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) {
|
||||
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});
|
||||
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);
|
||||
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) {
|
||||
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});
|
||||
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);
|
||||
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});
|
||||
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()`');
|
||||
}).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')
|
||||
it('allows a custom strategy to be used', function() {
|
||||
var plan = jasmine
|
||||
.createSpy('custom strategy')
|
||||
.and.returnValue('custom strategy result'),
|
||||
customStrategy = jasmine.createSpy('custom strategy')
|
||||
customStrategy = jasmine
|
||||
.createSpy('custom strategy')
|
||||
.and.returnValue(plan),
|
||||
originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({
|
||||
@@ -189,8 +228,9 @@ describe("SpyStrategy", function() {
|
||||
|
||||
spyStrategy.doSomething(1, 2, 3);
|
||||
expect(customStrategy).toHaveBeenCalledWith(1, 2, 3);
|
||||
expect(spyStrategy.exec(null, ['some', 'args']))
|
||||
.toEqual('custom strategy result');
|
||||
expect(spyStrategy.exec(null, ['some', 'args'])).toEqual(
|
||||
'custom strategy result'
|
||||
);
|
||||
expect(plan).toHaveBeenCalledWith('some', 'args');
|
||||
});
|
||||
|
||||
@@ -199,46 +239,49 @@ describe("SpyStrategy", function() {
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({
|
||||
fn: originalFn,
|
||||
customStrategies: {
|
||||
doSomething: function() { return 'not a function' }
|
||||
doSomething: function() {
|
||||
return 'not a function';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
expect(function() { spyStrategy.doSomething(1, 2, 3) }).toThrowError('Spy strategy must return a function');
|
||||
expect(function() {
|
||||
spyStrategy.doSomething(1, 2, 3);
|
||||
}).toThrowError('Spy strategy must return a function');
|
||||
});
|
||||
|
||||
it("does not allow custom strategies to overwrite existing methods", function() {
|
||||
it('does not allow custom strategies to overwrite existing methods', function() {
|
||||
var spyStrategy = new jasmineUnderTest.SpyStrategy({
|
||||
fn: function() {},
|
||||
customStrategies: {
|
||||
exec: function() {}
|
||||
}
|
||||
});
|
||||
fn: function() {},
|
||||
customStrategies: {
|
||||
exec: function() {}
|
||||
}
|
||||
});
|
||||
|
||||
expect(spyStrategy.exec).toBe(jasmineUnderTest.SpyStrategy.prototype.exec);
|
||||
});
|
||||
|
||||
it('throws an error when a non-function is passed to callFake strategy', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
invalidFakes = [5, 'foo', {}, true, false, null, void 0, new Date(), /.*/];
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||
|
||||
spyOn(jasmineUnderTest, 'isFunction_').and.returnValue(false);
|
||||
spyOn(jasmineUnderTest, 'isAsyncFunction_').and.returnValue(false);
|
||||
|
||||
expect(function () {
|
||||
expect(function() {
|
||||
spyStrategy.callFake(function() {});
|
||||
}).toThrowError(/^Argument passed to callFake should be a function, got/);
|
||||
|
||||
expect(function () {
|
||||
expect(function() {
|
||||
spyStrategy.callFake(function() {});
|
||||
}).toThrowError(/^Argument passed to callFake should be a function, got/);
|
||||
});
|
||||
|
||||
it("allows a return to plan stubbing after another strategy", function() {
|
||||
var originalFn = jasmine.createSpy("original"),
|
||||
fakeFn = jasmine.createSpy("fake").and.returnValue(67),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({fn: originalFn}),
|
||||
returnValue;
|
||||
it('allows a return to plan stubbing after another strategy', function() {
|
||||
var originalFn = jasmine.createSpy('original'),
|
||||
fakeFn = jasmine.createSpy('fake').and.returnValue(67),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn }),
|
||||
returnValue;
|
||||
|
||||
spyStrategy.callFake(fakeFn);
|
||||
returnValue = spyStrategy.exec();
|
||||
@@ -252,10 +295,10 @@ describe("SpyStrategy", function() {
|
||||
expect(returnValue).toEqual(void 0);
|
||||
});
|
||||
|
||||
it("returns the spy after changing the strategy", function(){
|
||||
it('returns the spy after changing the strategy', function() {
|
||||
var spy = {},
|
||||
spyFn = jasmine.createSpy('spyFn').and.returnValue(spy),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({getSpy: spyFn});
|
||||
spyFn = jasmine.createSpy('spyFn').and.returnValue(spy),
|
||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ getSpy: spyFn });
|
||||
|
||||
expect(spyStrategy.callThrough()).toBe(spy);
|
||||
expect(spyStrategy.returnValue()).toBe(spy);
|
||||
|
||||
Reference in New Issue
Block a user