Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

Commit 258a5f0

Browse files
author
Dr. Safi
committed
Added testing to ReusableNestedUnit & fixed bugs.
1 parent 253dfd8 commit 258a5f0

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

module/reusableNestedUnit/Controller.class.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = ({
3939
}
4040

4141
static initializeStaticClass() {
42-
if(methodInstanceName) {
42+
if(methodInstanceName && superclass && superclass.eventEmitter) {
4343
superclass.eventEmitter.on('initializationEnd', () => {
4444
let ClassObject = {}
4545
ClassObject[`${methodInstanceName}`] = self

module/reusableNestedUnit/entrypoint.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ function createStaticInstanceClasses({
1515
cacheName = false /* {Boolean || String} */
1616
}) {
1717
// Used as caching key.
18-
if(cacheName) cacheName = (typeof cacheName == 'boolean') ? implementationType : cacheName;
18+
let automaticCacheNaming;
19+
if(cacheName && typeof cacheName == 'boolean') {
20+
automaticCacheNaming = true
21+
cacheName = implementationType
22+
}
1923

2024
// load specific implementation functions (class producers).
2125
let implementationConfig;
@@ -36,7 +40,7 @@ function createStaticInstanceClasses({
3640
}
3741

3842
// Choose to create a cached context or anonymous garbage collected one.
39-
const MC = ModuleContext({ referenceName: cacheName })
43+
const MC = ModuleContext({ referenceName: implementationType /* used to combine all related contexts under same object */ })
4044

4145
// Create new context for the modules using proxy.
4246
let ControllerFunc = new MC({ target: ControllerFunction })
@@ -45,15 +49,23 @@ function createStaticInstanceClasses({
4549
const SpecificNestedUnitFunc = new MC({ target: implementationConfig.NestedUnitFunction })
4650
const SpecificUnitFunc = new MC({ target: implementationConfig.UnitFunction })
4751

48-
// Choose unique names to cache the related classes with.
4952
if(cacheName) {
50-
counter[cacheName] = counter[cacheName] || 0
51-
ControllerFunc.moduleContext.cacheName = `${cacheName}ReusableController${counter[cacheName]}`
52-
NestedUnitFunc.moduleContext.cacheName = `${cacheName}ReusableNestedUnit${counter[cacheName]}`
53-
UnitFunc.moduleContext.cacheName = `${cacheName}ReusableUnit${counter[cacheName]}`
54-
SpecificNestedUnitFunc.moduleContext.cacheName = `${cacheName}SpecificNestedUnit${counter[cacheName]}`
55-
SpecificUnitFunc.moduleContext.cacheName = `${cacheName}SpecificUnit${counter[cacheName]}`
56-
counter[cacheName] ++
53+
// Choose unique names to cache the related classes with.
54+
ControllerFunc.moduleContext.cacheName = `${cacheName}ReusableController`
55+
NestedUnitFunc.moduleContext.cacheName = `${cacheName}ReusableNestedUnit`
56+
UnitFunc.moduleContext.cacheName = `${cacheName}ReusableUnit`
57+
SpecificNestedUnitFunc.moduleContext.cacheName = `${cacheName}SpecificNestedUnit`
58+
SpecificUnitFunc.moduleContext.cacheName = `${cacheName}SpecificUnit`
59+
60+
if(automaticCacheNaming) {
61+
counter[cacheName] = counter[cacheName] || 0
62+
ControllerFunc.moduleContext.cacheName += `${counter[cacheName]}`
63+
NestedUnitFunc.moduleContext.cacheName += `${counter[cacheName]}`
64+
UnitFunc.moduleContext.cacheName += `${counter[cacheName]}`
65+
SpecificNestedUnitFunc.moduleContext.cacheName += `${counter[cacheName]}`
66+
SpecificUnitFunc.moduleContext.cacheName += `${counter[cacheName]}`
67+
counter[cacheName] ++
68+
}
5769
}
5870

5971
// Call class producer functions to return a new class with the specific connections.

module/reusableNestedUnit/entrypoint.test.js

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,86 @@
11
import assert from 'assert'
2-
import ReusableModule from './entrypoint.js'
2+
import createStaticInstanceClasses from './entrypoint.js'
33

44
describe('Connecting class inhiritance dynamically', () => {
5+
6+
describe('Creating static classes without cache', () => {
7+
8+
let Controller1 = createStaticInstanceClasses({
9+
superclass: null,
10+
implementationType: 'Middleware',
11+
cacheName: false
12+
})
13+
let Controller2 = createStaticInstanceClasses({
14+
superclass: null,
15+
implementationType: 'Middleware',
16+
cacheName: false
17+
})
18+
19+
it('Should return different class references', () => {
20+
assert.notStrictEqual(Controller1, Controller2)
21+
})
22+
23+
})
24+
25+
describe('Caching static classes using automatic naming', () => {
26+
27+
let Controller1 = createStaticInstanceClasses({
28+
superclass: null,
29+
implementationType: 'Middleware',
30+
cacheName: true
31+
})
32+
let Controller2 = createStaticInstanceClasses({
33+
superclass: null,
34+
implementationType: 'Middleware',
35+
cacheName: true
36+
})
37+
38+
it('Should return different class references', () => {
39+
assert.notStrictEqual(Controller1, Controller2)
40+
})
541

6-
describe('', () => {
42+
})
43+
44+
describe('Caching static classes using different passed cacheName', () => {
45+
46+
let Controller1 = createStaticInstanceClasses({
47+
superclass: null,
48+
implementationType: 'Middleware',
49+
cacheName: 'X'
50+
})
51+
let Controller2 = createStaticInstanceClasses({
52+
superclass: null,
53+
implementationType: 'Middleware',
54+
cacheName: 'Y'
55+
})
56+
57+
it('Should return different class references', () => {
58+
assert.notStrictEqual(Controller1, Controller2)
59+
})
60+
61+
})
62+
63+
describe('Caching static classes using different same passed cacheName', () => {
64+
65+
let Controller1 = createStaticInstanceClasses({
66+
superclass: null,
67+
implementationType: 'Middleware',
68+
cacheName: 'X'
69+
})
70+
let Controller2 = createStaticInstanceClasses({
71+
superclass: null,
72+
implementationType: 'Middleware',
73+
cacheName: 'X'
74+
})
75+
76+
it('Should return same class references', () => {
77+
assert.strictEqual(Controller1, Controller2)
78+
})
79+
80+
it('Should return same subclass references', () => {
81+
assert.strictEqual(Controller1.extendedSubclass.static['NestedUnit'], Controller1.extendedSubclass.static['NestedUnit'])
82+
assert.strictEqual(Controller1.extendedSubclass.static['UnitImplementation'], Controller1.extendedSubclass.static['UnitImplementation'])
83+
})
784

885
})
986

0 commit comments

Comments
 (0)