ts-mockery
Version:
Yet another typescript mocking library.
261 lines • 14.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var mockery_1 = require("./mockery");
describe('Mockery', function () {
var Foo = (function () {
function Foo() {
this.array = [];
this.string = ':-)';
this.anyFunction = function (param) { return param; };
this.booleanFunction = function () { return true; };
this.functionWithParam = function (param) { return param; };
this.objectFunction = function () { return ({
anotherNestedObject: { function: function () { return true; } },
string: 'hi',
stringFunction: function () { return 'hi'; }
}); };
this.stringFunction = function (buzz) { return buzz.toUpperCase(); };
this.voidFunction = function () { return undefined; };
}
Foo.static = function () {
throw new Error();
};
Foo.prototype.promiseFunction = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, Promise.resolve({ a: true, b: false })];
});
});
};
return Foo;
}());
describe('of', function () {
it('works with arrays', function () {
var mock = mockery_1.Mockery.of([{ string: 'foo' }, { string: 'bar' }]);
expect(mock[0].string).toBe('foo');
expect(mock[1].string).toBe('bar');
});
it('mocks the supplied method', function () {
var mock = mockery_1.Mockery.of({ stringFunction: function () { return 'hi'; } });
expect(mock.stringFunction('bye')).toEqual('hi');
});
it('mocks multiple methods', function () {
var mock = mockery_1.Mockery.of({ array: [{ stringFunction: function () { return 'string'; } }],
booleanFunction: function () { return false; },
functionWithParam: function () { return 'hi'; },
stringFunction: function () { return 'hi'; } });
expect(mock.stringFunction('whatevs')).toBe('hi');
expect(mock.booleanFunction()).toBe(false);
});
it('adds a spy to the supplied method on the mock object', function () {
var mock = mockery_1.Mockery.of({ stringFunction: function () { return 'hi'; } });
mock.stringFunction('bye');
expect(mock.stringFunction).toHaveBeenCalledWith('bye');
});
it('mocks a promise function', function () { return __awaiter(_this, void 0, void 0, function () {
var mock, _a;
var _this = this;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
mock = mockery_1.Mockery.of({ promiseFunction: function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2, Promise.resolve({ c: true, d: false })];
}); }); } });
_a = expect;
return [4, mock.promiseFunction()];
case 1:
_a.apply(void 0, [_b.sent()]).toEqual({ c: true, d: false });
return [2];
}
});
}); });
it('works with no arguments', function () {
expect(function () { return mockery_1.Mockery.of(); }).not.toThrow();
});
it('works with nested types', function () {
var mock = mockery_1.Mockery.of({ nestedObject: { stringFunction: function () { return 'hi'; } } });
expect(mock.nestedObject.stringFunction()).toEqual('hi');
});
it('mocks multiple level nested functions', function () {
var mock = mockery_1.Mockery.of({ nestedObject: { anotherNestedObject: { function: function () { return false; } } } });
mock.nestedObject.anotherNestedObject.function();
expect(mock.nestedObject.anotherNestedObject.function).toHaveBeenCalled();
});
it('mocks multiple level nested functions with arrays', function () {
var mock = mockery_1.Mockery.of({ array: [{ anotherNestedObject: { function: function () { return false; } } }] });
mock.array[0].anotherNestedObject.function();
expect(mock.array[0].anotherNestedObject.function).toHaveBeenCalled();
});
it('recursively mocks function return array objects', function () {
var obj = {
ect: function () { return ({ stuff: [{ foo: 1, bar: 'hi' }, { foo: 2, bar: '' }, { foo: 3, bar: '' }] }); }
};
mockery_1.Mockery.of({
ect: function () { return ({ stuff: [{}] }); }
});
});
it('mocks partials function return types', function () {
var mock = mockery_1.Mockery.of({ objectFunction: function () { return ({ string: 'bah' }); } });
expect(mock.objectFunction().string).toBe('bah');
});
it('does not blow up when a value is null', function () {
expect(function () { mockery_1.Mockery.of({ any: null }); }).not.toThrow();
});
it('noops have independent spies', function () {
var mock = mockery_1.Mockery.of({ nestedObject: { stringFunction: mockery_1.Mockery.noop }, objectFunction: mockery_1.Mockery.noop });
mock.objectFunction();
expect(mock.objectFunction).toHaveBeenCalled();
expect(mock.nestedObject.stringFunction).not.toHaveBeenCalled();
});
it('Throws error when trying to mock circular reference', function () {
var myObject = {};
myObject.foo = myObject;
var errorMessage = "Return value of foo has a circular reference.\nConsider using Mock.from instead.";
expect(function () { return mockery_1.Mockery.of(myObject); }).toThrow(new Error(errorMessage));
});
});
describe('extend', function () {
it('can extend an empty mock object', function () {
var mock = mockery_1.Mockery.of();
mockery_1.Mockery.extend(mock).with({ stringFunction: function () { return 'foooo'; } });
expect(mock.stringFunction('bye')).toEqual('foooo');
});
it('extends the mock object with the supplied property', function () {
var mock = mockery_1.Mockery.of();
mockery_1.Mockery.extend(mock).with({ string: ':-)' });
expect(mock.string).toEqual(':-)');
});
it('extends the mock object with spies for the supplied function', function () {
var mock = mockery_1.Mockery.of();
mockery_1.Mockery.extend(mock).with({ booleanFunction: function () { return false; } });
mock.booleanFunction();
expect(mock.booleanFunction).toHaveBeenCalled();
});
it('overrides preexisting mock', function () {
var mock = mockery_1.Mockery.of({ stringFunction: function () { return 'hi'; } });
mockery_1.Mockery.extend(mock).with({ stringFunction: function () { return 'foooo'; } });
expect(mock.stringFunction('bye')).toEqual('foooo');
});
it('mocks an already mocked method multiple times', function () {
var mock = mockery_1.Mockery.of({ booleanFunction: function () { return false; } });
expect(mock.booleanFunction()).toBeFalsy();
mockery_1.Mockery.extend(mock).with({ booleanFunction: function () { return true; } });
expect(mock.booleanFunction()).toBeTruthy();
mockery_1.Mockery.extend(mock).with({ booleanFunction: function () { return false; } });
expect(mock.booleanFunction()).toBeFalsy();
expect(mock.booleanFunction).toHaveBeenCalled();
});
it('mocks an already mocked property multiple times', function () {
var mock = mockery_1.Mockery.of({ string: 'first' });
expect(mock.string).toEqual('first');
mockery_1.Mockery.extend(mock).with({ string: 'second' });
expect(mock.string).toEqual('second');
mockery_1.Mockery.extend(mock).with({ string: 'third' });
expect(mock.string).toEqual('third');
});
it('mocks partials function return types', function () {
var mock = mockery_1.Mockery.of({ objectFunction: function () { return ({ string: 'bah' }); } });
expect(mock.objectFunction().string).toBe('bah');
});
it('Throws error when trying to extend a mock with circular reference', function () {
var myObject = {};
myObject.foo = myObject;
var errorMessage = "Return value of foo has a circular reference.\nConsider using Mock.from instead.";
expect(function () { return mockery_1.Mockery.extend({}).with(myObject); }).toThrow(new Error(errorMessage));
});
});
describe('staticMethod', function () {
it('does not call the underlying implememtation', function () {
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
expect(function () { Foo.static(); }).not.toThrow();
});
it('calls fake', function () {
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
expect(Foo.static()).toEqual('hi');
});
it('is a spy', function () {
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
Foo.static();
expect(Foo.static).toHaveBeenCalled();
});
it('resets the call count', function () {
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
Foo.static();
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
expect(Foo.static).not.toHaveBeenCalled();
});
it('overwrites spy fake', function () {
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hi'; });
mockery_1.Mockery.staticMethod(Foo, 'static', function () { return 'hello'; });
expect(Foo.static()).toEqual('hello');
});
it('calls the underlying implememtation', function () {
expect(function () { Foo.static(); }).toThrow();
});
});
describe('noop', function () {
it('returns a spy', function () {
var anyFunc = mockery_1.Mockery.noop;
anyFunc();
expect(anyFunc).toHaveBeenCalled();
});
});
describe('all', function () {
it('returns a spy for uncalled methods', function () {
var mock = mockery_1.Mockery.all();
expect(mock.booleanFunction).not.toHaveBeenCalled();
});
it('returns a spy for called methods', function () {
var mock = mockery_1.Mockery.all();
mock.objectFunction();
expect(mock.objectFunction).toHaveBeenCalled();
});
it('returns undefined for called methods', function () {
var mock = mockery_1.Mockery.all();
var result = mock.objectFunction();
expect(result).toBeUndefined();
});
it('you can extend an all object', function () {
var mock = mockery_1.Mockery.all();
mockery_1.Mockery.extend(mock).with({ stringFunction: function () { return 'hola'; } });
expect(mock.stringFunction('whatevs')).toBe('hola');
});
});
});
//# sourceMappingURL=mockery.spec.js.map