defineNamespace.test.js
2.52 KB
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
/* eslint-disable no-undef */
'use strict';
var defineNamespace = require('../src/js/defineNamespace');
var type = require('../src/js/type');
describe('defineNamespace', function() {
it('define', function() {
defineNamespace('aaa.bbb.ccc');
expect(aaa).toBeDefined();
expect(aaa.bbb).toBeDefined();
expect(aaa.bbb.ccc).toBeDefined();
});
it('not define', function() {
defineNamespace('aaa.bbb');
expect(aaa.bbb.ddd).not.toBeDefined();
});
it('with props', function() {
var num = 0;
defineNamespace('asdf', {
exec: function() {
num += 10;
}
});
asdf.exec();
expect(num).toBe(10);
});
it('function', function() {
var num = 0;
defineNamespace('fdsa', function() {
num += 100;
}, true);
fdsa();
expect(num).toBe(100);
});
it('for class', function() {
var name = 'nhn';
var mInstance;
defineNamespace('asdf.nhn', function() {
this.name = name;
this.getName = function() {
return this.name;
};
}, true);
mInstance = new asdf.nhn();
expect(mInstance.getName()).toBe(name);
});
it('override', function() {
defineNamespace('asdf.over');
expect(asdf.over).toBeDefined();
expect(asdf.over.exec).not.toBeDefined();
defineNamespace('asdf.over', {
exec: function() {}
}, true);
expect(asdf.over.exec).toBeDefined();
});
it('invalid props type', function() {
defineNamespace('asdf.hello', 'hello world');
expect(asdf.hello).toBeDefined();
expect(type.isString(asdf.hello)).toBeFalsy();
});
it('define double', function() {
var feCom = defineNamespace('fe.component'),
feDouble = defineNamespace('fe.component');
expect(feCom).toBe(feDouble);
});
it('define double other depth', function() {
var feCom = defineNamespace('fe.comp');
defineNamespace('fe.comp.team');
expect(feCom).toBe(fe.comp);
expect(feCom.team).toBe(fe.comp.team);
});
it('should extend if exist', function() {
var tester = {
method: function() {}
};
defineNamespace('foo.bar.baz');
expect(window.foo.bar.baz).toEqual(jasmine.any(Object));
defineNamespace('foo.bar', tester);
expect(window.foo.bar.method).toEqual(tester.method);
});
});