-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
157 lines (130 loc) · 5.3 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {assert} from 'chai';
import sinon from 'sinon';
import sisyphos from './index.js';
/* eslint-env mocha */
var sandbox = sinon.sandbox.create();
describe('sisyphos', function(){
afterEach(function(){
sandbox.restore();
return sisyphos.reset();
});
describe('stub method', function(){
it('should not accept the same name twice', function(){
var name = 'some/arbitrary/name';
sisyphos.stub(name, {});
assert.throws(function(){
sisyphos.stub(name, {});
});
});
it('can stub modules that have a single default export', function(){
sisyphos.stub('mocks/default_a.js', 'something');
return sisyphos.require('mocks/c.js').then(function(c){
assert.equal(c.getA(), 'something');
});
});
it('can stub modules that have multiple named exports', function(){
sisyphos.stub('mocks/foo_and_bar.js', {
foo: 'newfoo',
bar: 'newbar'
});
return sisyphos.require('mocks/d.js').then(function(c){
assert.equal(c.getFoo(), 'newfoo');
assert.equal(c.getBar(), 'newbar');
});
});
it('can stub modules with default exports and named exports at the same time', function() {
sisyphos.stub('mocks/default_a.js', 'something');
sisyphos.stub('mocks/foo_and_bar.js', {
foo: 'newfoo'
});
return sisyphos.require('mocks/d.js').then(function(c){
assert.equal(c.getFoo(), 'newfoo');
assert.equal(c.getA(), 'something');
});
});
it('should accept a map of stub names to their default exports', function(){
sisyphos.stub({
'mocks/default_a.js': 'wow',
'mocks/default_b.js': 'such stub'
});
return sisyphos.require('mocks/c.js').then(function(c){
assert.equal(c.getA(), 'wow');
assert.equal(c.getB(), 'such stub');
});
});
it('can stub modules that export functions', function () {
var someFunc = function(){};
someFunc.a = 'newA';
someFunc.b = 'newB';
sisyphos.stub({
'mocks/common_js_function_with_properties.js': someFunc
});
return sisyphos.require('mocks/imports_common_js_function_with_properties.js')
.then(function (module) {
assert.strictEqual(module.default, someFunc);
return System.normalize('mocks/imports_common_js_function_with_properties.js');
})
.then(function (normalizedName) {
var module = System.get(normalizedName);
assert.equal(module.exportedA, 'newA');
assert.equal(module.exportedB, 'newB');
});
});
});
describe('requireWithStubs method', function(){
it('should be a function', function(){
assert(typeof sisyphos.requireWithStubs === 'function');
});
});
describe('require method', function(){
it('should be an alias of requireWithStubs', function(){
assert.equal(sisyphos.require, sisyphos.requireWithStubs);
});
});
describe('reset method', function(){
describe('if the module was in the registry before being required', function() {
beforeEach(function() {
var name = 'mocks/c.js';
return System.import(name).then(function(){
sisyphos.stub({
'mocks/default_a.js': 'wow',
'mocks/default_b.js': 'such stub'
});
return sisyphos.require(name);
});
});
it('should return all original implementations to their names', function(){
return sisyphos.reset()
.then(System.import.bind(System, 'mocks/c.js'))
.then(function(c){
assert.equal(c.getA(), 'a');
assert.equal(c.getB(), 'b');
});
});
});
describe('if the module was not in the registry before being required', function() {
beforeEach(function() {
var name = 'mocks/d.js';
return System.normalize(name)
.then(function(normalizedName) {
System.delete(normalizedName);
})
.then(function() {
sisyphos.stub('mocks/foo_and_bar.js', {
foo: 'newfoo',
bar: 'newbar'
});
return sisyphos.require(name);
});
});
it('should return all original implementations to their names', function(){
return sisyphos.reset()
.then(System.import.bind(System, 'mocks/d.js'))
.then(function(d){
assert.equal(d.getFoo(), 'foo');
assert.equal(d.getBar(), 'bar');
});
});
});
});
});