1
1
var Plotly = require ( '@lib/index' ) ;
2
+ var Registry = require ( '@src/registry' ) ;
2
3
3
- describe ( 'the register function ' , function ( ) {
4
+ describe ( 'Test Registry ' , function ( ) {
4
5
'use strict' ;
5
6
6
- var Plots = Plotly . Plots ;
7
+ describe ( 'register, getModule, and traceIs' , function ( ) {
8
+ beforeEach ( function ( ) {
9
+ this . modulesKeys = Object . keys ( Registry . modules ) ;
10
+ this . allTypesKeys = Object . keys ( Registry . allTypes ) ;
11
+ this . allCategoriesKeys = Object . keys ( Registry . allCategories ) ;
12
+
13
+ this . fakeModule = {
14
+ calc : function ( ) { return 42 ; } ,
15
+ plot : function ( ) { return 1000000 ; }
16
+ } ;
17
+ this . fakeModule2 = {
18
+ plot : function ( ) { throw new Error ( 'nope!' ) ; }
19
+ } ;
20
+
21
+ Registry . register ( this . fakeModule , 'newtype' , [ 'red' , 'green' ] ) ;
22
+
23
+ spyOn ( console , 'warn' ) ;
24
+ } ) ;
25
+
26
+ afterEach ( function ( ) {
27
+ function revertObj ( obj , initialKeys ) {
28
+ Object . keys ( obj ) . forEach ( function ( k ) {
29
+ if ( initialKeys . indexOf ( k ) === - 1 ) delete obj [ k ] ;
30
+ } ) ;
31
+ }
32
+
33
+ revertObj ( Registry . modules , this . modulesKeys ) ;
34
+ revertObj ( Registry . allTypes , this . allTypesKeys ) ;
35
+ revertObj ( Registry . allCategories , this . allCategoriesKeys ) ;
36
+ } ) ;
37
+
38
+ it ( 'should not reregister a type' , function ( ) {
39
+ Registry . register ( this . fakeModule2 , 'newtype' , [ 'yellow' , 'blue' ] ) ;
40
+ expect ( Registry . allCategories . yellow ) . toBeUndefined ( ) ;
41
+ } ) ;
42
+
43
+ it ( 'should find the module for a type' , function ( ) {
44
+ expect ( Registry . getModule ( 'newtype' ) ) . toBe ( this . fakeModule ) ;
45
+ expect ( Registry . getModule ( { type : 'newtype' } ) ) . toBe ( this . fakeModule ) ;
46
+ } ) ;
47
+
48
+ it ( 'should return false for types it doesn\'t know' , function ( ) {
49
+ expect ( Registry . getModule ( 'notatype' ) ) . toBe ( false ) ;
50
+ expect ( Registry . getModule ( { type : 'notatype' } ) ) . toBe ( false ) ;
51
+ expect ( Registry . getModule ( { type : 'newtype' , r : 'this is polar' } ) ) . toBe ( false ) ;
52
+ } ) ;
53
+
54
+ it ( 'should find the categories for this type' , function ( ) {
55
+ expect ( Registry . traceIs ( 'newtype' , 'red' ) ) . toBe ( true ) ;
56
+ expect ( Registry . traceIs ( { type : 'newtype' } , 'red' ) ) . toBe ( true ) ;
57
+ } ) ;
58
+
59
+ it ( 'should not find other real categories' , function ( ) {
60
+ expect ( Registry . traceIs ( 'newtype' , 'cartesian' ) ) . toBe ( false ) ;
61
+ expect ( Registry . traceIs ( { type : 'newtype' } , 'cartesian' ) ) . toBe ( false ) ;
62
+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ describe ( 'Registry.registerSubplot' , function ( ) {
67
+ var fake = {
68
+ name : 'fake' ,
69
+ attr : 'abc' ,
70
+ idRoot : 'cba' ,
71
+ attrRegex : / ^ a b c ( [ 2 - 9 ] | [ 1 - 9 ] [ 0 - 9 ] + ) ? $ / ,
72
+ idRegex : / ^ c b a ( [ 2 - 9 ] | [ 1 - 9 ] [ 0 - 9 ] + ) ? $ / ,
73
+ attributes : { stuff : { 'more stuff' : 102102 } }
74
+ } ;
75
+
76
+ Registry . registerSubplot ( fake ) ;
77
+
78
+ var subplotsRegistry = Registry . subplotsRegistry ;
79
+
80
+ it ( 'should register attr, idRoot and attributes' , function ( ) {
81
+ expect ( subplotsRegistry . fake . attr ) . toEqual ( 'abc' ) ;
82
+ expect ( subplotsRegistry . fake . idRoot ) . toEqual ( 'cba' ) ;
83
+ expect ( subplotsRegistry . fake . attributes )
84
+ . toEqual ( { stuff : { 'more stuff' : 102102 } } ) ;
85
+ } ) ;
86
+
87
+ describe ( 'registered subplot type attribute regex' , function ( ) {
88
+ it ( 'should compile to correct attribute regex string' , function ( ) {
89
+ expect ( subplotsRegistry . fake . attrRegex . toString ( ) )
90
+ . toEqual ( '/^abc([2-9]|[1-9][0-9]+)?$/' ) ;
91
+ } ) ;
92
+
93
+ var shouldPass = [
94
+ 'abc' , 'abc2' , 'abc3' , 'abc10' , 'abc9' , 'abc100' , 'abc2002'
95
+ ] ;
96
+ var shouldFail = [
97
+ '0abc' , 'abc0' , 'abc1' , 'abc021321' , 'abc00021321'
98
+ ] ;
99
+
100
+ shouldPass . forEach ( function ( s ) {
101
+ it ( 'considers ' + JSON . stringify ( s ) + 'as a correct attribute name' , function ( ) {
102
+ expect ( subplotsRegistry . fake . attrRegex . test ( s ) ) . toBe ( true ) ;
103
+ } ) ;
104
+ } ) ;
105
+
106
+ shouldFail . forEach ( function ( s ) {
107
+ it ( 'considers ' + JSON . stringify ( s ) + 'as an incorrect attribute name' , function ( ) {
108
+ expect ( subplotsRegistry . fake . attrRegex . test ( s ) ) . toBe ( false ) ;
109
+ } ) ;
110
+ } ) ;
111
+ } ) ;
112
+
113
+ describe ( 'registered subplot type id regex' , function ( ) {
114
+ it ( 'should compile to correct id regular expression' , function ( ) {
115
+ expect ( subplotsRegistry . fake . idRegex . toString ( ) )
116
+ . toEqual ( '/^cba([2-9]|[1-9][0-9]+)?$/' ) ;
117
+ } ) ;
118
+
119
+ var shouldPass = [
120
+ 'cba' , 'cba2' , 'cba3' , 'cba10' , 'cba9' , 'cba100' , 'cba2002'
121
+ ] ;
122
+ var shouldFail = [
123
+ '0cba' , 'cba0' , 'cba1' , 'cba021321' , 'cba00021321'
124
+ ] ;
125
+
126
+ shouldPass . forEach ( function ( s ) {
127
+ it ( 'considers ' + JSON . stringify ( s ) + 'as a correct attribute name' , function ( ) {
128
+ expect ( subplotsRegistry . fake . idRegex . test ( s ) ) . toBe ( true ) ;
129
+ } ) ;
130
+ } ) ;
131
+
132
+ shouldFail . forEach ( function ( s ) {
133
+ it ( 'considers ' + JSON . stringify ( s ) + 'as an incorrect attribute name' , function ( ) {
134
+ expect ( subplotsRegistry . fake . idRegex . test ( s ) ) . toBe ( false ) ;
135
+ } ) ;
136
+ } ) ;
137
+ } ) ;
138
+
139
+ } ) ;
140
+ } ) ;
141
+
142
+ describe ( 'the register function' , function ( ) {
143
+ 'use strict' ;
7
144
8
145
beforeEach ( function ( ) {
9
- this . modulesKeys = Object . keys ( Plots . modules ) ;
10
- this . allTypesKeys = Object . keys ( Plots . allTypes ) ;
11
- this . allCategoriesKeys = Object . keys ( Plots . allCategories ) ;
12
- this . allTransformsKeys = Object . keys ( Plots . transformsRegistry ) ;
146
+ this . modulesKeys = Object . keys ( Registry . modules ) ;
147
+ this . allTypesKeys = Object . keys ( Registry . allTypes ) ;
148
+ this . allCategoriesKeys = Object . keys ( Registry . allCategories ) ;
149
+ this . allTransformsKeys = Object . keys ( Registry . transformsRegistry ) ;
13
150
} ) ;
14
151
15
152
afterEach ( function ( ) {
@@ -19,10 +156,10 @@ describe('the register function', function() {
19
156
} ) ;
20
157
}
21
158
22
- revertObj ( Plots . modules , this . modulesKeys ) ;
23
- revertObj ( Plots . allTypes , this . allTypesKeys ) ;
24
- revertObj ( Plots . allCategories , this . allCategoriesKeys ) ;
25
- revertObj ( Plots . transformsRegistry , this . allTransformsKeys ) ;
159
+ revertObj ( Registry . modules , this . modulesKeys ) ;
160
+ revertObj ( Registry . allTypes , this . allTypesKeys ) ;
161
+ revertObj ( Registry . allCategories , this . allCategoriesKeys ) ;
162
+ revertObj ( Registry . transformsRegistry , this . allTransformsKeys ) ;
26
163
} ) ;
27
164
28
165
it ( 'should throw an error when no argument is given' , function ( ) {
@@ -44,7 +181,7 @@ describe('the register function', function() {
44
181
Plotly . register ( mockTrace1 ) ;
45
182
} ) . not . toThrow ( ) ;
46
183
47
- expect ( Plotly . Plots . getModule ( 'mockTrace1' ) ) . toBe ( mockTrace1 ) ;
184
+ expect ( Registry . getModule ( 'mockTrace1' ) ) . toBe ( mockTrace1 ) ;
48
185
} ) ;
49
186
50
187
it ( 'should work with an array of modules' , function ( ) {
@@ -60,7 +197,7 @@ describe('the register function', function() {
60
197
Plotly . register ( [ mockTrace2 ] ) ;
61
198
} ) . not . toThrow ( ) ;
62
199
63
- expect ( Plotly . Plots . getModule ( 'mockTrace2' ) ) . toBe ( mockTrace2 ) ;
200
+ expect ( Registry . getModule ( 'mockTrace2' ) ) . toBe ( mockTrace2 ) ;
64
201
} ) ;
65
202
66
203
it ( 'should throw an error when an invalid module is given' , function ( ) {
@@ -99,6 +236,6 @@ describe('the register function', function() {
99
236
Plotly . register ( transformModule ) ;
100
237
} ) . not . toThrow ( ) ;
101
238
102
- expect ( Plots . transformsRegistry [ 'mah-transform' ] ) . toBeDefined ( ) ;
239
+ expect ( Registry . transformsRegistry [ 'mah-transform' ] ) . toBeDefined ( ) ;
103
240
} ) ;
104
241
} ) ;
0 commit comments