Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
3
*
4
* This source code is licensed under the BSD-style license found in the
5
* LICENSE file in the root directory of this source tree. An additional grant
6
* of patent rights can be found in the PATENTS file in the same directory.
7
*/
8
'use strict';
9
10
// This was generated with https://babeljs.io/repl/
11
/* jshint ignore:start */
12
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } };
13
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
14
/* jshint ignore:end */
15
/* global _classCallCheck, _createClass */
16
17
jest.autoMockOff();
18
19
describe('moduleMocker', function() {
20
var moduleMocker;
21
22
beforeEach(function() {
23
moduleMocker = require('../moduleMocker');
24
});
25
26
describe('getMetadata', function() {
27
it('returns the function `name` property', function() {
28
function x(){}
29
var metadata = moduleMocker.getMetadata(x);
30
expect(x.name).toBe('x');
31
expect(metadata.name).toBe('x');
32
});
33
});
34
35
describe('generateFromMetadata', function() {
36
it('forwards the function name property', function() {
37
function foo(){}
38
var fooMock = moduleMocker.generateFromMetadata(
39
moduleMocker.getMetadata(foo)
40
);
41
expect(fooMock.name).toBe('foo');
42
});
43
44
it('wont interfere with previous mocks on a shared prototype', function() {
45
var ClassFoo = function() {};
46
ClassFoo.prototype.x = function() {};
47
var ClassFooMock = moduleMocker.generateFromMetadata(
48
moduleMocker.getMetadata(ClassFoo)
49
);
50
var foo = new ClassFooMock();
51
var bar = new ClassFooMock();
52
53
foo.x.mockImplementation(function() {
54
return 'Foo';
55
});
56
bar.x.mockImplementation(function() {
57
return 'Bar';
58
});
59
60
expect(foo.x()).toBe('Foo');
61
expect(bar.x()).toBe('Bar');
62
});
63
64
it('mocks ES6 non-enumerable methods', function() {
65
// ES6: class ClassFoo { foo() { } }
66
// Converted with https://babeljs.io/repl/
67
var ClassFoo = (function () {
68
function ClassFoo() {
69
_classCallCheck(this, ClassFoo);
70
}
71
72
_createClass(ClassFoo, [{
73
key: 'foo',
74
value: function foo() {}
75
}, {
76
key: 'toString',
77
value: function toString() {
78
return 'Foo';
79
}
80
}]);
81
82
return ClassFoo;
83
})();
84
var ClassFooMock = moduleMocker.generateFromMetadata(
85
moduleMocker.getMetadata(ClassFoo)
86
);
87
88
var foo = new ClassFooMock();
89
90
var instanceFoo = new ClassFoo();
91
var instanceFooMock = moduleMocker.generateFromMetadata(
92
moduleMocker.getMetadata(instanceFoo)
93
);
94
95
expect(typeof foo.foo).toBe('function');
96
expect(typeof instanceFooMock.foo).toBe('function');
97
expect(instanceFooMock.foo.mock).not.toBeUndefined();
98
99
expect(instanceFooMock.toString.mock).not.toBeUndefined();
100
});
101
});
102
});
103
104