-
-
Notifications
You must be signed in to change notification settings - Fork 719
/
container_test.js
257 lines (226 loc) · 8.45 KB
/
container_test.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const { expect } = require('chai');
const path = require('path');
const FileSystem = require('../../lib/helper/FileSystem');
const actor = require('../../lib/actor');
const container = require('../../lib/container');
describe('Container', () => {
before(() => {
global.codecept_dir = path.join(__dirname, '/..');
global.inject = container.support;
global.actor = actor;
});
afterEach(() => {
container.clear();
['I', 'dummy_page'].forEach((po) => {
const name = require.resolve(path.join(__dirname, `../data/${po}`));
delete require.cache[name];
});
});
describe('#translation', () => {
const Translation = require('../../lib/translation');
it('should create empty translation', () => {
container.create({});
expect(container.translation()).to.be.instanceOf(Translation);
expect(container.translation().loaded).to.be.false;
expect(container.translation().actionAliasFor('see')).to.eql('see');
});
it('should create Russian translation', () => {
container.create({ translation: 'ru-RU' });
expect(container.translation()).to.be.instanceOf(Translation);
expect(container.translation().loaded).to.be.true;
expect(container.translation().I).to.eql('Я');
expect(container.translation().actionAliasFor('see')).to.eql('вижу');
});
it('should create Italian translation', () => {
container.create({ translation: 'it-IT' });
expect(container.translation()).to.be.instanceOf(Translation);
expect(container.translation().loaded).to.be.true;
expect(container.translation().I).to.eql('io');
expect(container.translation().value('contexts').Feature).to.eql('Caratteristica');
});
it('should create French translation', () => {
container.create({ translation: 'fr-FR' });
expect(container.translation()).to.be.instanceOf(Translation);
expect(container.translation().loaded).to.be.true;
expect(container.translation().I).to.eql('Je');
expect(container.translation().value('contexts').Feature).to.eql('Fonctionnalité');
});
it('should create Portuguese translation', () => {
container.create({ translation: 'pt-BR' });
expect(container.translation()).to.be.instanceOf(Translation);
expect(container.translation().loaded).to.be.true;
expect(container.translation().I).to.eql('Eu');
expect(container.translation().value('contexts').Feature).to.eql('Funcionalidade');
});
});
describe('#helpers', () => {
beforeEach(() => {
container.clear({
helper1: { name: 'hello' },
helper2: { name: 'world' },
});
});
it('should return all helper with no args', () => expect(container.helpers()).to.have.keys('helper1', 'helper2'));
it('should return helper by name', () => {
expect(container.helpers('helper1')).is.ok;
expect(container.helpers('helper1').name).to.eql('hello');
expect(container.helpers('helper2')).is.ok;
expect(container.helpers('helper2').name).to.eql('world');
expect(!container.helpers('helper3')).is.ok;
});
});
describe('#support', () => {
beforeEach(() => {
container.clear({}, {
support1: { name: 'hello' },
support2: { name: 'world' },
});
});
it('should return all support objects', () => expect(container.support()).to.have.keys('support1', 'support2'));
it('should support object by name', () => {
expect(container.support('support1')).is.ok;
expect(container.support('support1').name).to.eql('hello');
expect(container.support('support2')).is.ok;
expect(container.support('support2').name).to.eql('world');
expect(!container.support('support3')).is.ok;
});
});
describe('#plugins', () => {
beforeEach(() => {
container.clear({}, {}, {
plugin1: { name: 'hello' },
plugin2: { name: 'world' },
});
});
it('should return all plugins', () => expect(container.plugins()).to.have.keys('plugin1', 'plugin2'));
it('should get plugin by name', () => {
expect(container.plugins('plugin1')).is.ok;
expect(container.plugins('plugin1').name).is.eql('hello');
expect(container.plugins('plugin2')).is.ok;
expect(container.plugins('plugin2').name).is.eql('world');
expect(!container.plugins('plugin3')).is.ok;
});
});
describe('#create', () => {
it('should create container with helpers', () => {
const config = {
helpers: {
MyHelper: {
require: './data/helper',
},
FileSystem: {},
},
};
container.create(config);
// custom helpers
expect(container.helpers('MyHelper')).is.ok;
expect(container.helpers('MyHelper').method()).to.eql('hello world');
// built-in helpers
expect(container.helpers('FileSystem')).is.ok;
expect(container.helpers('FileSystem')).to.be.instanceOf(FileSystem);
});
it('should always create I', () => {
container.create({});
expect(container.support('I')).is.ok;
});
it('should load DI and return a reference to the module', () => {
container.create({
include: {
dummyPage: './data/dummy_page',
},
});
const dummyPage = require('../data/dummy_page');
expect(container.support('dummyPage')).is.eql(dummyPage);
});
it('should load I from path and execute _init', () => {
container.create({
include: {
I: './data/I',
},
});
expect(container.support('I')).is.ok;
expect(container.support('I')).to.include.keys('_init', 'doSomething');
expect(global.I_initialized).to.be.true;
});
it('should load DI includes provided as require paths', () => {
container.create({
include: {
dummyPage: './data/dummy_page',
},
});
expect(container.support('dummyPage')).is.ok;
expect(container.support('dummyPage')).to.include.keys('openDummyPage');
});
it('should load DI and inject I into PO', () => {
container.create({
include: {
dummyPage: './data/dummy_page',
},
});
expect(container.support('dummyPage')).is.ok;
expect(container.support('I')).is.ok;
expect(container.support('dummyPage')).to.include.keys('openDummyPage');
expect(container.support('dummyPage').getI()).to.have.keys(Object.keys(container.support('I')));
});
it('should load DI and inject custom I into PO', () => {
container.create({
include: {
dummyPage: './data/dummy_page',
I: './data/I',
},
});
expect(container.support('dummyPage')).is.ok;
expect(container.support('I')).is.ok;
expect(container.support('dummyPage')).to.include.keys('openDummyPage');
expect(container.support('dummyPage').getI()).to.have.keys(Object.keys(container.support('I')));
});
it('should load DI includes provided as objects', () => {
container.create({
include: {
dummyPage: {
openDummyPage: () => 'dummy page opened',
},
},
});
expect(container.support('dummyPage')).is.ok;
expect(container.support('dummyPage')).to.include.keys('openDummyPage');
});
it('should load DI includes provided as objects', () => {
container.create({
include: {
dummyPage: {
openDummyPage: () => 'dummy page opened',
},
},
});
expect(container.support('dummyPage')).is.ok;
expect(container.support('dummyPage')).to.include.keys('openDummyPage');
});
});
describe('#append', () => {
it('should be able to add new helper', () => {
const config = {
helpers: {
FileSystem: {},
},
};
container.create(config);
container.append({
helpers: {
AnotherHelper: { method: () => 'executed' },
},
});
expect(container.helpers('FileSystem')).is.ok;
expect(container.helpers('FileSystem')).is.instanceOf(FileSystem);
expect(container.helpers('AnotherHelper')).is.ok;
expect(container.helpers('AnotherHelper').method()).is.eql('executed');
});
it('should be able to add new support object', () => {
container.create({});
container.append({ support: { userPage: { login: '#login' } } });
expect(container.support('I')).is.ok;
expect(container.support('userPage')).is.ok;
expect(container.support('userPage').login).is.eql('#login');
});
});
});