lang.spec.js
4.75 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { expect } from 'chai';
import {
extend,
noop,
getNow,
selector,
getClientHeightByDom,
namespace,
} from '../src/util/lang';
describe('extend方法', () => {
let obj1;
let obj2;
let obj3;
before(() => {
// 在本区块的所有测试用例之前执行
obj1 = {
company: 'epoint',
product: {
ejs: 'ejs混合开发方案',
},
};
obj2 = {
company: 'epoint2',
city: 'suzhou',
product: {
m7: 'm7移动框架',
},
};
obj3 = {
name: 'zhangsan',
product: {
group: 'mobile',
m7: 'm7移动框架2',
},
};
});
it('拓展单个(浅层拓展)', () => {
const result = extend({}, obj1, obj2);
expect(result).to.have.deep.property('company', obj2.company);
expect(result).to.have.deep.property('product', obj2.product);
});
it('拓展多个(浅层拓展)', () => {
const result = extend({}, obj1, obj2, obj3);
expect(result).to.have.deep.property('company', obj2.company);
expect(result).to.have.deep.property('product', obj3.product);
expect(result).to.have.deep.property('name', obj3.name);
});
it('拓展多个(递归拓展)', () => {
const result = extend(true, {}, obj1, obj2, obj3);
expect(result).to.have.deep.property('company', obj2.company);
expect(result).to.have.deep.nested.property('product.ejs', obj1.product.ejs);
expect(result).to.have.deep.nested.property('product.m7', obj3.product.m7);
expect(result).to.have.deep.nested.property('product.group', obj3.product.group);
});
});
describe('noop', () => {
it('是一个空函数', () => {
noop();
expect(noop).to.be.an('function');
});
});
describe('getNow', () => {
it('当前时间', () => {
expect(getNow()).to.be.an('number');
});
it('两个now不允许相等', () => {
expect(getNow()).to.not.equal(getNow());
});
it('没有performance时', () => {
const oldPerformance = window.performance;
window.performance = undefined;
getNow();
expect(1).to.be.equal(1);
window.performance = oldPerformance;
});
});
describe('selector', () => {
const ID = 'testdiv';
const CLASS = 'testclass';
const TAG = 'DIV';
const CONTENT = 'testcontent';
function expectDom(dom) {
expect(dom).to.be.an.instanceof(HTMLElement);
expect(dom.id).to.be.equal(ID);
expect(dom.className).to.be.equal(CLASS);
expect(dom.className).to.be.equal(CLASS);
expect(dom.tagName).to.be.equal(TAG);
expect(dom.innerHTML).to.be.equal(CONTENT);
}
before(() => {
const dom = document.createElement(TAG);
dom.id = ID;
dom.className = CLASS;
dom.innerHTML = CONTENT;
document.body.appendChild(dom);
});
it('id选择', () => {
const dom = selector(`#${ID}`);
expectDom(dom);
});
it('class选择', () => {
const dom = selector(`.${CLASS}`);
expectDom(dom);
});
});
describe('getClientHeightByDom', () => {
// 确保高度会大于这个阈值
const INSURANCE_HEIGHT = 10;
let dom;
before(() => {
dom = document.createElement('div');
dom.innerHTML = '纯属测试';
document.body.appendChild(dom);
});
it('body的高度', () => {
expect(getClientHeightByDom(document.body)).to.be.an('number');
expect(getClientHeightByDom(dom)).to.be.above(INSURANCE_HEIGHT);
});
it('元素的高度', () => {
expect(getClientHeightByDom(dom)).to.be.an('number');
expect(getClientHeightByDom(dom)).to.be.above(INSURANCE_HEIGHT);
});
});
describe('namespace', () => {
const NAMESPACE_STR = 'test';
const NAMESPACE_STR_MORE = 'test.child';
const NAMESPACE_EMPTY = '';
let parent;
let target;
beforeEach(() => {
parent = {};
target = {};
});
it('empty命名空间挂载', () => {
const res = namespace(parent, NAMESPACE_EMPTY, target);
expect(res).to.be.equal(parent);
});
it('正常单重命名空间挂载', () => {
const res = namespace(parent, NAMESPACE_STR, target);
expect(res).to.be.equal(target);
expect(parent[NAMESPACE_STR]).to.be.equal(target);
});
it('正常多重命名空间挂载', () => {
const res = namespace(parent, NAMESPACE_STR_MORE, target);
expect(res).to.be.equal(target);
});
});