Skip to content

Commit

Permalink
Fix mocking for boolean values.
Browse files Browse the repository at this point in the history
Summary:I cannot believe this was never an issue before? wtf.?
Closes #713

Reviewed By: kentaromiura

Differential Revision: D2943462

fb-gh-sync-id: 55ed5d3f21a9b35e2fbf5221397a95e7244dc781
shipit-source-id: 55ed5d3f21a9b35e2fbf5221397a95e7244dc781
  • Loading branch information
cpojer authored and facebook-github-bot-5 committed Feb 18, 2016
1 parent fc90c9b commit 68c2162
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
74 changes: 43 additions & 31 deletions src/lib/__tests__/moduleMocker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,67 @@

jest.autoMockOff();

describe('moduleMocker', function() {
var moduleMocker;
describe('moduleMocker', () => {
let moduleMocker;

beforeEach(function() {
beforeEach(() => {
moduleMocker = require('../moduleMocker');
});

describe('getMetadata', function() {
it('returns the function `name` property', function() {
describe('getMetadata', () => {
it('returns the function `name` property', () => {
function x() {}
var metadata = moduleMocker.getMetadata(x);
const metadata = moduleMocker.getMetadata(x);
expect(x.name).toBe('x');
expect(metadata.name).toBe('x');
});

it('mocks constant values', () => {
expect(moduleMocker.getMetadata('banana').value).toEqual('banana');
expect(moduleMocker.getMetadata(27).value).toEqual(27);
expect(moduleMocker.getMetadata(false).value).toEqual(false);
});
});

describe('generateFromMetadata', function() {
it('forwards the function name property', function() {
describe('generateFromMetadata', () => {
it('forwards the function name property', () => {
function foo() {}
var fooMock = moduleMocker.generateFromMetadata(
const fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo)
);
expect(fooMock.name).toBe('foo');
});

it('wont interfere with previous mocks on a shared prototype', function() {
var ClassFoo = function() {};
ClassFoo.prototype.x = function() {};
var ClassFooMock = moduleMocker.generateFromMetadata(
it('wont interfere with previous mocks on a shared prototype', () => {
const ClassFoo = function() {};
ClassFoo.prototype.x = () => {};
const ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo)
);
var foo = new ClassFooMock();
var bar = new ClassFooMock();
const foo = new ClassFooMock();
const bar = new ClassFooMock();

foo.x.mockImplementation(function() {
foo.x.mockImplementation(() => {
return 'Foo';
});
bar.x.mockImplementation(function() {
bar.x.mockImplementation(() => {
return 'Bar';
});

expect(foo.x()).toBe('Foo');
expect(bar.x()).toBe('Bar');
});

it('does not mock non-enumerable getters', function() {
var foo = Object.defineProperties({}, {
it('does not mock non-enumerable getters', () => {
const foo = Object.defineProperties({}, {
nonEnumMethod: {
value: function() {},
value: () => {},
},
nonEnumGetter: {
get: function() { throw new Error(); },
get: () => { throw new Error(); },
},
});
var fooMock = moduleMocker.generateFromMetadata(
const fooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(foo)
);

Expand All @@ -75,21 +81,21 @@ describe('moduleMocker', function() {
expect(fooMock.nonEnumGetter).toBeUndefined();
});

it('mocks ES2015 non-enumerable methods', function() {
it('mocks ES2015 non-enumerable methods', () => {
class ClassFoo {
foo() {}
toString() {
return 'Foo';
}
}

var ClassFooMock = moduleMocker.generateFromMetadata(
const ClassFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(ClassFoo)
);
var foo = new ClassFooMock();
const foo = new ClassFooMock();

var instanceFoo = new ClassFoo();
var instanceFooMock = moduleMocker.generateFromMetadata(
const instanceFoo = new ClassFoo();
const instanceFooMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(instanceFoo)
);

Expand All @@ -100,15 +106,21 @@ describe('moduleMocker', function() {
expect(instanceFooMock.toString.mock).not.toBeUndefined();
});

it('mocks methods that are bound multiple times', function() {
var func = function func() {};
var multipleBoundFunc = func.bind(null).bind(null);
it('mocks methods that are bound multiple times', () => {
const func = function func() {};
const multipleBoundFunc = func.bind(null).bind(null);

var multipleBoundFuncMock = moduleMocker.generateFromMetadata(
const multipleBoundFuncMock = moduleMocker.generateFromMetadata(
moduleMocker.getMetadata(multipleBoundFunc)
);

expect(typeof multipleBoundFuncMock).toBe('function');
});

it('mocks regexp instances', () => {
expect(
() => moduleMocker.generateFromMetadata(moduleMocker.getMetadata(/a/))
).not.toThrow();
});
});
});
12 changes: 10 additions & 2 deletions src/lib/moduleMocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getType(ref) {
return 'array';
} else if (isA('Object', ref)) {
return 'object';
} else if (isA('Number', ref) || isA('String', ref)) {
} else if (isA('Number', ref) || isA('String', ref) || isA('Boolean', ref)) {
return 'constant';
} else if (isA('Map', ref) || isA('WeakMap', ref) || isA('Set', ref)) {
return 'collection';
Expand All @@ -97,7 +97,15 @@ function isReadonlyProp(object, prop) {
) &&
isA('Function', object)
) ||
(prop === 'source' && isA('RegExp', object))
(
(
prop === 'source' ||
prop === 'global' ||
prop === 'ignoreCase' ||
prop === 'multiline'
) &&
isA('RegExp', object)
)
);
}

Expand Down

0 comments on commit 68c2162

Please sign in to comment.