22
22
describe ( 'require + define' , function ( ) {
23
23
const { require, define } = cordova ;
24
24
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
+
25
40
it ( 'exists off of cordova' , function ( ) {
26
41
expect ( require ) . toBeDefined ( ) ;
27
42
expect ( define ) . toBeDefined ( ) ;
@@ -30,14 +45,20 @@ describe('require + define', function () {
30
45
describe ( 'when defining' , function ( ) {
31
46
it ( 'Test#001 : can define and remove module' , function ( ) {
32
47
define ( 'a' , jasmine . createSpy ( ) ) ;
48
+ expect ( define . moduleMap . a ) . toBeDefined ( ) ;
49
+
33
50
define . remove ( 'a' ) ;
51
+ expect ( define . moduleMap . a ) . toBeUndefined ( ) ;
34
52
} ) ;
35
53
36
54
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 ( ) ;
38
58
} ) ;
39
59
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 ( ) { } ) ;
41
62
expect ( function ( ) {
42
63
define ( 'cordova' , function ( ) { } ) ;
43
64
} ) . toThrow ( 'module cordova already defined' ) ;
@@ -64,11 +85,10 @@ describe('require + define', function () {
64
85
define ( 'ModuleB' , function ( require , exports , module ) {
65
86
require ( 'ModuleA' ) ;
66
87
} ) ;
88
+
67
89
expect ( function ( ) {
68
90
require ( 'ModuleA' ) ;
69
91
} ) . toThrow ( 'Cycle in require graph: ModuleA->ModuleB->ModuleA' ) ;
70
- define . remove ( 'ModuleA' ) ;
71
- define . remove ( 'ModuleB' ) ;
72
92
} ) ;
73
93
74
94
it ( 'Test#007 : throws an exception when a cycle of requires occurs' , function ( ) {
@@ -81,36 +101,30 @@ describe('require + define', function () {
81
101
define ( 'ModuleC' , function ( require , exports , module ) {
82
102
require ( 'ModuleA' ) ;
83
103
} ) ;
104
+
84
105
expect ( function ( ) {
85
106
require ( 'ModuleA' ) ;
86
107
} ) . toThrow ( 'Cycle in require graph: ModuleA->ModuleB->ModuleC->ModuleA' ) ;
87
- define . remove ( 'ModuleA' ) ;
88
- define . remove ( 'ModuleB' ) ;
89
- define . remove ( 'ModuleC' ) ;
90
108
} ) ;
91
109
92
110
it ( 'Test#008 : calls the factory method when requiring' , function ( ) {
93
111
var factory = jasmine . createSpy ( ) ;
94
112
define ( 'dino' , factory ) ;
95
113
require ( 'dino' ) ;
114
+ expect ( factory ) . toHaveBeenCalledTimes ( 1 ) ;
96
115
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 ) ;
104
120
} ) ;
105
121
106
122
it ( 'Test#009 : returns the exports object' , function ( ) {
107
123
define ( 'a' , function ( require , exports , module ) {
108
124
exports . stuff = 'asdf' ;
109
125
} ) ;
110
126
111
- var v = require ( 'a' ) ;
112
- expect ( v . stuff ) . toBe ( 'asdf' ) ;
113
- define . remove ( 'a' ) ;
127
+ expect ( require ( 'a' ) . stuff ) . toBe ( 'asdf' ) ;
114
128
} ) ;
115
129
116
130
it ( 'Test#010 : can use both the exports and module.exports object' , function ( ) {
@@ -119,36 +133,19 @@ describe('require + define', function () {
119
133
module . exports . b = 'b' ;
120
134
} ) ;
121
135
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' } ) ;
126
137
} ) ;
127
138
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 = { } ;
130
141
define ( 'a' , function ( require , exports , module ) {
131
- module . exports = new Foo ( ) ;
142
+ module . exports = Foo ;
132
143
} ) ;
133
144
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 ) ;
149
146
} ) ;
150
147
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 ( ) {
152
149
define ( 'plugin.ios.foo' , function ( require , exports , module ) {
153
150
module . exports = require ( './bar' ) * 2 ;
154
151
} ) ;
0 commit comments