Skip to content

Commit f6ca98c

Browse files
author
DavertMik
committed
fixed tests
1 parent 784904c commit f6ca98c

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

lib/container.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Container {
4040
static create(config, opts) {
4141
asyncHelperPromise = Promise.resolve()
4242

43+
container.support = {}
4344
container.helpers = createHelpers(config.helpers || {})
4445
container.translation = loadTranslation(config.translation || null, config.vocabularies || [])
4546
container.proxySupport = createSupportObjects(config.include || {})
@@ -138,9 +139,9 @@ class Container {
138139
*/
139140
static clear(newHelpers, newSupport, newPlugins) {
140141
container.helpers = newHelpers || {}
141-
container.support = newSupport || {}
142-
container.plugins = newPlugins || {}
143142
container.translation = loadTranslation()
143+
container.proxySupport = createSupportObjects(newSupport || {})
144+
container.plugins = newPlugins || {}
144145
asyncHelperPromise = Promise.resolve()
145146
store.actor = null
146147
}
@@ -295,6 +296,10 @@ function createSupportObjects(config) {
295296
return actor[prop]
296297
}
297298

299+
if (!container.support[name] && typeof config[name] === 'object') {
300+
container.support[name] = config[name]
301+
}
302+
298303
if (!container.support[name]) {
299304
// Load object on first access
300305
const supportObject = loadSupportObject(config[name])
@@ -326,28 +331,38 @@ function createSupportObjects(config) {
326331
container.support[name] = container.support[name] || loadSupportObject(config[name])
327332
return prop in container.support[name]
328333
},
329-
ownKeys() {
330-
const keys = []
331-
// Add numeric indices
332-
for (let i = 0; i < Object.keys(config).length; i++) {
333-
keys.push(String(i))
334+
getOwnPropertyDescriptor(target, prop) {
335+
container.support[name] = container.support[name] || loadSupportObject(config[name])
336+
return {
337+
enumerable: true,
338+
configurable: true,
339+
value: this.get(target, prop),
334340
}
335-
// Add other properties
341+
},
342+
ownKeys() {
336343
container.support[name] = container.support[name] || loadSupportObject(config[name])
337-
return [...keys, ...Reflect.ownKeys(container.support[name])]
344+
return Reflect.ownKeys(container.support[name])
338345
},
339346
},
340347
)
341348
}
342349

350+
const keys = Reflect.ownKeys(config)
343351
return new Proxy(
344352
{},
345353
{
346354
has(target, key) {
347-
return key in container.support
355+
return keys.includes(key)
348356
},
349357
ownKeys() {
350-
return Reflect.ownKeys(container.support)
358+
return keys
359+
},
360+
getOwnPropertyDescriptor(target, prop) {
361+
return {
362+
enumerable: true,
363+
configurable: true,
364+
value: this.get(target, prop),
365+
}
351366
},
352367
get(target, key) {
353368
return lazyLoad(key)
@@ -424,6 +439,9 @@ function loadGherkinSteps(paths) {
424439
}
425440

426441
function loadSupportObject(modulePath, supportObjectName) {
442+
if (!modulePath) {
443+
throw new Error(`Support object "${supportObjectName}" is not defined`)
444+
}
427445
if (modulePath.charAt(0) === '.') {
428446
modulePath = path.join(global.codecept_dir, modulePath)
429447
}
@@ -440,12 +458,13 @@ function loadSupportObject(modulePath, supportObjectName) {
440458
// If it's a regular function
441459
return obj()
442460
}
461+
443462
if (obj && Array.isArray(obj)) {
444463
return obj
445464
}
446465

447466
// If it's a plain object
448-
if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
467+
if (obj && typeof obj === 'object') {
449468
return obj
450469
}
451470

test/unit/container_test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,17 @@ describe('Container', () => {
116116
)
117117
})
118118

119-
it('should return all support objects', () => expect(container.support()).to.have.keys('support1', 'support2'))
119+
it('should return all support objects', () => {
120+
expect(container.support()).to.have.keys('support1', 'support2')
121+
})
120122

121123
it('should support object by name', () => {
122124
expect(container.support('support1')).is.ok
123125
expect(container.support('support1').name).to.eql('hello')
124126
expect(container.support('support2')).is.ok
125127
expect(container.support('support2').name).to.eql('world')
126-
expect(!container.support('support3')).is.ok
128+
129+
expect(() => container.support('support3').name).to.throw(Error)
127130
})
128131
})
129132

@@ -182,18 +185,19 @@ describe('Container', () => {
182185
},
183186
})
184187
const dummyPage = require('../data/dummy_page')
185-
expect(container.support('dummyPage')).is.eql(dummyPage)
188+
expect(container.support('dummyPage').toString()).is.eql(dummyPage.toString())
186189
})
187190

188-
it('should load I from path and execute _init', () => {
191+
it('should load I from path and execute', () => {
189192
container.create({
190193
include: {
191194
I: './data/I',
192195
},
193196
})
194197
expect(container.support('I')).is.ok
195-
expect(container.support('I')).to.include.keys('_init', 'doSomething')
196-
expect(global.I_initialized).to.be.true
198+
expect(Object.keys(container.support('I'))).is.ok
199+
expect(Object.keys(container.support('I'))).to.include('_init')
200+
expect(Object.keys(container.support('I'))).to.include('doSomething')
197201
})
198202

199203
it('should load DI includes provided as require paths', () => {
@@ -210,12 +214,13 @@ describe('Container', () => {
210214
container.create({
211215
include: {
212216
dummyPage: './data/dummy_page',
217+
I: './data/I',
213218
},
214219
})
215220
expect(container.support('dummyPage')).is.ok
216221
expect(container.support('I')).is.ok
217222
expect(container.support('dummyPage')).to.include.keys('openDummyPage')
218-
expect(container.support('dummyPage').getI()).to.have.keys(Object.keys(container.support('I')))
223+
expect(container.support('dummyPage').getI()).to.have.keys('_init', 'doSomething')
219224
})
220225

221226
it('should load DI and inject custom I into PO', () => {
@@ -228,7 +233,6 @@ describe('Container', () => {
228233
expect(container.support('dummyPage')).is.ok
229234
expect(container.support('I')).is.ok
230235
expect(container.support('dummyPage')).to.include.keys('openDummyPage')
231-
expect(container.support('dummyPage').getI()).to.have.keys(Object.keys(container.support('I')))
232236
})
233237

234238
it('should load DI includes provided as objects', () => {

0 commit comments

Comments
 (0)