Skip to content

Commit a61952f

Browse files
authored
Minor cleanup of require/define tests (#194)
* Remove Test#012 which duplicated Test#008 * Fix/Improve some test descriptions * Reset modules after each require/define test * Be more specific in Test#008 & Test#011 * Cosmetic changes
1 parent c67c6b9 commit a61952f

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

test/test.require.js

+37-40
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@
2222
describe('require + define', function () {
2323
const { require, define } = cordova;
2424

25+
function clearModules () {
26+
Object.keys(define.moduleMap).forEach(m => define.remove(m));
27+
}
28+
29+
// Restore our actual modules (cordova etc.) after all tests have finished
30+
const originalModules = {};
31+
beforeAll(() => Object.assign(originalModules, define.moduleMap));
32+
afterAll(() => {
33+
clearModules();
34+
Object.assign(define.moduleMap, originalModules);
35+
});
36+
37+
// Begin each test on a clean slate
38+
beforeEach(clearModules);
39+
2540
it('exists off of cordova', function () {
2641
expect(require).toBeDefined();
2742
expect(define).toBeDefined();
@@ -30,14 +45,20 @@ describe('require + define', function () {
3045
describe('when defining', function () {
3146
it('Test#001 : can define and remove module', function () {
3247
define('a', jasmine.createSpy());
48+
expect(define.moduleMap.a).toBeDefined();
49+
3350
define.remove('a');
51+
expect(define.moduleMap.a).toBeUndefined();
3452
});
3553

3654
it("Test#002 : can remove a module that doesn't exist", function () {
37-
define.remove("can't touch this");
55+
expect(() => {
56+
define.remove("can't touch this");
57+
}).not.toThrow();
3858
});
3959

40-
it('Test#003 : throws an error the module already exists', function () {
60+
it('Test#003 : throws an error if the module already exists', function () {
61+
define('cordova', function () {});
4162
expect(function () {
4263
define('cordova', function () {});
4364
}).toThrow('module cordova already defined');
@@ -64,11 +85,10 @@ describe('require + define', function () {
6485
define('ModuleB', function (require, exports, module) {
6586
require('ModuleA');
6687
});
88+
6789
expect(function () {
6890
require('ModuleA');
6991
}).toThrow('Cycle in require graph: ModuleA->ModuleB->ModuleA');
70-
define.remove('ModuleA');
71-
define.remove('ModuleB');
7292
});
7393

7494
it('Test#007 : throws an exception when a cycle of requires occurs', function () {
@@ -81,36 +101,30 @@ describe('require + define', function () {
81101
define('ModuleC', function (require, exports, module) {
82102
require('ModuleA');
83103
});
104+
84105
expect(function () {
85106
require('ModuleA');
86107
}).toThrow('Cycle in require graph: ModuleA->ModuleB->ModuleC->ModuleA');
87-
define.remove('ModuleA');
88-
define.remove('ModuleB');
89-
define.remove('ModuleC');
90108
});
91109

92110
it('Test#008 : calls the factory method when requiring', function () {
93111
var factory = jasmine.createSpy();
94112
define('dino', factory);
95113
require('dino');
114+
expect(factory).toHaveBeenCalledTimes(1);
96115

97-
expect(factory).toHaveBeenCalledWith(jasmine.any(Function),
98-
{}, {
99-
id: 'dino',
100-
exports: {}
101-
});
102-
103-
define.remove('dino');
116+
const [req, exports, module] = factory.calls.argsFor(0);
117+
expect(req).toEqual(jasmine.any(Function));
118+
expect(module).toEqual({ id: 'dino', exports: {} });
119+
expect(exports).toBe(module.exports);
104120
});
105121

106122
it('Test#009 : returns the exports object', function () {
107123
define('a', function (require, exports, module) {
108124
exports.stuff = 'asdf';
109125
});
110126

111-
var v = require('a');
112-
expect(v.stuff).toBe('asdf');
113-
define.remove('a');
127+
expect(require('a').stuff).toBe('asdf');
114128
});
115129

116130
it('Test#010 : can use both the exports and module.exports object', function () {
@@ -119,36 +133,19 @@ describe('require + define', function () {
119133
module.exports.b = 'b';
120134
});
121135

122-
var v = require('a');
123-
expect(v.a).toBe('a');
124-
expect(v.b).toBe('b');
125-
define.remove('a');
136+
expect(require('a')).toEqual({ a: 'a', b: 'b' });
126137
});
127138

128-
it('Test#011 : returns was is assigned to module.exports', function () {
129-
var Foo = function () { };
139+
it('Test#011 : returns what is assigned to module.exports', function () {
140+
const Foo = {};
130141
define('a', function (require, exports, module) {
131-
module.exports = new Foo();
142+
module.exports = Foo;
132143
});
133144

134-
var v = require('a');
135-
expect(v instanceof Foo).toBe(true);
136-
define.remove('a');
137-
});
138-
139-
it('Test#012 : has the id and exports values but not the factory on the module object', function () {
140-
var factory = function (require, exports, module) {
141-
expect(module.id).toBe('a');
142-
expect(module.exports).toBeDefined();
143-
expect(module.factory).not.toBeDefined();
144-
};
145-
146-
define('a', factory);
147-
require('a');
148-
define.remove('a');
145+
expect(require('a')).toBe(Foo);
149146
});
150147

151-
it("can handle multiple defined modules that use cordova's unique handling of relative require paths", function () {
148+
it('Test#012 : supports a unique, namespace-based flavor of relative require paths', function () {
152149
define('plugin.ios.foo', function (require, exports, module) {
153150
module.exports = require('./bar') * 2;
154151
});

0 commit comments

Comments
 (0)