Skip to content

Commit

Permalink
test: implement tests for jestjs#1214
Browse files Browse the repository at this point in the history
  • Loading branch information
phra committed Dec 25, 2017
1 parent 65d884d commit a4a6313
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
92 changes: 92 additions & 0 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,96 @@ describe('moduleMocker', () => {
expect(spy2.mock.calls.length).toBe(1);
});
});

describe('spyOnProperty', () => {
it('should work', () => {
let isOriginalCalled = false;
let originalCallThis;
let originalCallArguments;
const obj = {
get method() {
return function () {
isOriginalCalled = true;
originalCallThis = this;
originalCallArguments = arguments;
}
},
};

const spy = moduleMocker.spyOnProperty(obj, 'method');

const thisArg = {this: true};
const firstArg = {first: true};
const secondArg = {second: true};
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).toHaveBeenCalled();

isOriginalCalled = false;
originalCallThis = null;
originalCallArguments = null;
spy.mockReset();
spy.mockRestore();
console.log('porcoddddddiooo', obj, obj.method)
obj.method.call(thisArg, firstArg, secondArg);
expect(isOriginalCalled).toBe(true);
expect(originalCallThis).toBe(thisArg);
expect(originalCallArguments.length).toBe(2);
expect(originalCallArguments[0]).toBe(firstArg);
expect(originalCallArguments[1]).toBe(secondArg);
expect(spy).not.toHaveBeenCalled();
});

it('should throw on invalid input', () => {
expect(() => {
moduleMocker.spyOnProperty(null, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOnProperty({}, 'method');
}).toThrow();
expect(() => {
moduleMocker.spyOnProperty({method: 10}, 'method');
}).toThrow();
});

it('supports restoring all spies', () => {
let methodOneCalls = 0;
let methodTwoCalls = 0;
const obj = {
get methodOne() {
return function () {methodOneCalls++};
},
get methodTwo() {
return function () {methodTwoCalls++};
},
};

const spy1 = moduleMocker.spyOnProperty(obj, 'methodOne');
const spy2 = moduleMocker.spyOnProperty(obj, 'methodTwo');

// First, we call with the spies: both spies and both original functions
// should be called.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(1);
expect(methodTwoCalls).toBe(1);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);

moduleMocker.restoreAllMocks();

// Then, after resetting all mocks, we call methods again. Only the real
// methods should bump their count, not the spies.
obj.methodOne();
obj.methodTwo();
expect(methodOneCalls).toBe(2);
expect(methodTwoCalls).toBe(2);
expect(spy1.mock.calls.length).toBe(1);
expect(spy2.mock.calls.length).toBe(1);
});
});
});
2 changes: 2 additions & 0 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,15 @@ class ModuleMockerClass {

descriptor[accessType] = this._makeComponent({type: 'function'}, () => {
descriptor[accessType] = original;
Object.defineProperty(obj, propertyName, descriptor)
});

descriptor[accessType].mockImplementation(function() {
return original.apply(this, arguments);
});
}

Object.defineProperty(obj, propertyName, descriptor)
return descriptor[accessType];
}

Expand Down
21 changes: 21 additions & 0 deletions packages/jest-runtime/src/__tests__/runtime_jest_spy_on.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ describe('Runtime', () => {
expect(spy).toHaveBeenCalled();
}));
});

describe('jest.spyOnProperty', () => {
it('calls the original function', () =>
createRuntime(__filename).then(runtime => {
const root = runtime.requireModule(runtime.__mockRootPath);

let isOriginalCalled = false;
const obj = {
get method() {
return () => isOriginalCalled = true;
},
};

const spy = root.jest.spyOnProperty(obj, 'method');

obj.method();

expect(isOriginalCalled).toBe(true);
expect(spy).toHaveBeenCalled();
}));
});
});

0 comments on commit a4a6313

Please sign in to comment.