Skip to content

Commit 11f475e

Browse files
committed
move Registry tests, plots_test.js -> register_test.js
1 parent 910e17c commit 11f475e

File tree

2 files changed

+150
-147
lines changed

2 files changed

+150
-147
lines changed

test/jasmine/tests/plots_test.js

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -245,140 +245,6 @@ describe('Test Plots', function() {
245245
});
246246
});
247247

248-
describe('Plots.register, getModule, and traceIs', function() {
249-
beforeEach(function() {
250-
this.modulesKeys = Object.keys(Plots.modules);
251-
this.allTypesKeys = Object.keys(Plots.allTypes);
252-
this.allCategoriesKeys = Object.keys(Plots.allCategories);
253-
254-
this.fakeModule = {
255-
calc: function() { return 42; },
256-
plot: function() { return 1000000; }
257-
};
258-
this.fakeModule2 = {
259-
plot: function() { throw new Error('nope!'); }
260-
};
261-
262-
Plots.register(this.fakeModule, 'newtype', ['red', 'green']);
263-
264-
spyOn(console, 'warn');
265-
});
266-
267-
afterEach(function() {
268-
function revertObj(obj, initialKeys) {
269-
Object.keys(obj).forEach(function(k) {
270-
if(initialKeys.indexOf(k) === -1) delete obj[k];
271-
});
272-
}
273-
274-
revertObj(Plots.modules, this.modulesKeys);
275-
revertObj(Plots.allTypes, this.allTypesKeys);
276-
revertObj(Plots.allCategories, this.allCategoriesKeys);
277-
});
278-
279-
it('should not reregister a type', function() {
280-
Plots.register(this.fakeModule2, 'newtype', ['yellow', 'blue']);
281-
expect(Plots.allCategories.yellow).toBeUndefined();
282-
});
283-
284-
it('should find the module for a type', function() {
285-
expect(Plots.getModule('newtype')).toBe(this.fakeModule);
286-
expect(Plots.getModule({type: 'newtype'})).toBe(this.fakeModule);
287-
});
288-
289-
it('should return false for types it doesn\'t know', function() {
290-
expect(Plots.getModule('notatype')).toBe(false);
291-
expect(Plots.getModule({type: 'notatype'})).toBe(false);
292-
expect(Plots.getModule({type: 'newtype', r: 'this is polar'})).toBe(false);
293-
});
294-
295-
it('should find the categories for this type', function() {
296-
expect(Plots.traceIs('newtype', 'red')).toBe(true);
297-
expect(Plots.traceIs({type: 'newtype'}, 'red')).toBe(true);
298-
});
299-
300-
it('should not find other real categories', function() {
301-
expect(Plots.traceIs('newtype', 'cartesian')).toBe(false);
302-
expect(Plots.traceIs({type: 'newtype'}, 'cartesian')).toBe(false);
303-
expect(console.warn).not.toHaveBeenCalled();
304-
});
305-
});
306-
307-
describe('Plots.registerSubplot', function() {
308-
var fake = {
309-
name: 'fake',
310-
attr: 'abc',
311-
idRoot: 'cba',
312-
attrRegex: /^abc([2-9]|[1-9][0-9]+)?$/,
313-
idRegex: /^cba([2-9]|[1-9][0-9]+)?$/,
314-
attributes: { stuff: { 'more stuff': 102102 } }
315-
};
316-
317-
Plots.registerSubplot(fake);
318-
319-
var subplotsRegistry = Plots.subplotsRegistry;
320-
321-
it('should register attr, idRoot and attributes', function() {
322-
expect(subplotsRegistry.fake.attr).toEqual('abc');
323-
expect(subplotsRegistry.fake.idRoot).toEqual('cba');
324-
expect(subplotsRegistry.fake.attributes)
325-
.toEqual({stuff: { 'more stuff': 102102 }});
326-
});
327-
328-
describe('registered subplot type attribute regex', function() {
329-
it('should compile to correct attribute regex string', function() {
330-
expect(subplotsRegistry.fake.attrRegex.toString())
331-
.toEqual('/^abc([2-9]|[1-9][0-9]+)?$/');
332-
});
333-
334-
var shouldPass = [
335-
'abc', 'abc2', 'abc3', 'abc10', 'abc9', 'abc100', 'abc2002'
336-
];
337-
var shouldFail = [
338-
'0abc', 'abc0', 'abc1', 'abc021321', 'abc00021321'
339-
];
340-
341-
shouldPass.forEach(function(s) {
342-
it('considers ' + JSON.stringify(s) + 'as a correct attribute name', function() {
343-
expect(subplotsRegistry.fake.attrRegex.test(s)).toBe(true);
344-
});
345-
});
346-
347-
shouldFail.forEach(function(s) {
348-
it('considers ' + JSON.stringify(s) + 'as an incorrect attribute name', function() {
349-
expect(subplotsRegistry.fake.attrRegex.test(s)).toBe(false);
350-
});
351-
});
352-
});
353-
354-
describe('registered subplot type id regex', function() {
355-
it('should compile to correct id regular expression', function() {
356-
expect(subplotsRegistry.fake.idRegex.toString())
357-
.toEqual('/^cba([2-9]|[1-9][0-9]+)?$/');
358-
});
359-
360-
var shouldPass = [
361-
'cba', 'cba2', 'cba3', 'cba10', 'cba9', 'cba100', 'cba2002'
362-
];
363-
var shouldFail = [
364-
'0cba', 'cba0', 'cba1', 'cba021321', 'cba00021321'
365-
];
366-
367-
shouldPass.forEach(function(s) {
368-
it('considers ' + JSON.stringify(s) + 'as a correct attribute name', function() {
369-
expect(subplotsRegistry.fake.idRegex.test(s)).toBe(true);
370-
});
371-
});
372-
373-
shouldFail.forEach(function(s) {
374-
it('considers ' + JSON.stringify(s) + 'as an incorrect attribute name', function() {
375-
expect(subplotsRegistry.fake.idRegex.test(s)).toBe(false);
376-
});
377-
});
378-
});
379-
380-
});
381-
382248
describe('Plots.resize', function() {
383249
var gd;
384250

test/jasmine/tests/register_test.js

Lines changed: 150 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,152 @@
11
var Plotly = require('@lib/index');
2+
var Registry = require('@src/registry');
23

3-
describe('the register function', function() {
4+
describe('Test Registry', function() {
45
'use strict';
56

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: /^abc([2-9]|[1-9][0-9]+)?$/,
72+
idRegex: /^cba([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';
7144

8145
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);
13150
});
14151

15152
afterEach(function() {
@@ -19,10 +156,10 @@ describe('the register function', function() {
19156
});
20157
}
21158

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);
26163
});
27164

28165
it('should throw an error when no argument is given', function() {
@@ -44,7 +181,7 @@ describe('the register function', function() {
44181
Plotly.register(mockTrace1);
45182
}).not.toThrow();
46183

47-
expect(Plotly.Plots.getModule('mockTrace1')).toBe(mockTrace1);
184+
expect(Registry.getModule('mockTrace1')).toBe(mockTrace1);
48185
});
49186

50187
it('should work with an array of modules', function() {
@@ -60,7 +197,7 @@ describe('the register function', function() {
60197
Plotly.register([mockTrace2]);
61198
}).not.toThrow();
62199

63-
expect(Plotly.Plots.getModule('mockTrace2')).toBe(mockTrace2);
200+
expect(Registry.getModule('mockTrace2')).toBe(mockTrace2);
64201
});
65202

66203
it('should throw an error when an invalid module is given', function() {
@@ -99,6 +236,6 @@ describe('the register function', function() {
99236
Plotly.register(transformModule);
100237
}).not.toThrow();
101238

102-
expect(Plots.transformsRegistry['mah-transform']).toBeDefined();
239+
expect(Registry.transformsRegistry['mah-transform']).toBeDefined();
103240
});
104241
});

0 commit comments

Comments
 (0)