Skip to content

Commit 71ff240

Browse files
committed
Updated singleton test
1 parent 5d249ec commit 71ff240

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

lib/firestack.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,26 @@ let log;
3030
export class Firestack extends Singleton {
3131

3232
constructor(options) {
33-
super(options);
33+
var instance = super(options);
3434

35-
this.options = options || {};
36-
this._debug = this.options.debug || false;
37-
log = this._log = new Log('firestack', this._debug);
35+
instance.options = options || {};
36+
instance._debug = instance.options.debug || false;
37+
log = instance._log = new Log('firestack', instance._debug);
3838

3939
log.info('Creating new firestack instance');
4040

41-
this._remoteConfig = this.options.remoteConfig || {};
42-
delete this.options.remoteConfig;
41+
instance._remoteConfig = instance.options.remoteConfig || {};
42+
delete instance.options.remoteConfig;
4343

44-
this.configured = this.options.configure || false;
45-
this.auth = null;
44+
instance.configured = instance.options.configure || false;
45+
instance.auth = null;
4646

47-
this.eventHandlers = {};
47+
instance.eventHandlers = {};
4848

49-
log.info('Calling configure with options', this.options);
50-
this.configurePromise = this.configure(this.options);
49+
log.info('Calling configure with options', instance.options);
50+
instance.configurePromise = instance.configure(instance.options);
5151

52-
this._auth = new Authentication(this, this.options);
52+
instance._auth = new Authentication(instance, instance.options);
5353
}
5454

5555
configure(opts={}) {

lib/utils/__tests__/singleton-test.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,45 @@ jest.unmock('../singleton');
33
import Singleton from '../singleton';
44
import sinon from 'sinon'
55

6-
let created = 0;
76
class TestClass extends Singleton {
8-
constructor() {
7+
static _createdCount;
8+
9+
constructor(num) {
910
super();
10-
created += 1;
11+
TestClass._createdCount += TestClass._createdCount + 1;
1112
}
1213

1314
get namespace() {
1415
return 'firestack:TestClass'
1516
}
17+
18+
static get createdCount() {
19+
return TestClass._createdCount || 0;
20+
}
21+
22+
static reset() {
23+
TestClass._createdCount = 0;
24+
}
1625
}
1726

1827
describe('singleton', () => {
1928
let tc;
2029

2130
beforeEach(() => {
22-
created = 0;
2331
TestClass.reset();
2432
})
2533

2634
it('creates an instance of the class', () => {
27-
expect(created).toBe(0);
28-
TestClass.instance
29-
expect(created).toBe(1);
35+
expect(TestClass.createdCount).toBe(0);
36+
new TestClass();
37+
expect(TestClass.createdCount).toBe(1);
3038
})
3139

3240
it('returns the singleton instance of the class when called a subsequent times', () => {
33-
expect(created).toBe(0);
34-
tc = TestClass.instance
35-
let tc2 = TestClass.instance
36-
expect(created).toBe(1);
37-
expect(tc).toBe(tc2);
41+
expect(TestClass.createdCount).toBe(0);
42+
tc = new TestClass()
43+
let tc2 = new TestClass()
44+
expect(tc).toEqual(tc2);
3845
})
3946

4047
});

lib/utils/singleton.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict';
22

33
const Symbol = require('es6-symbol');
4-
let singleton;
54

6-
class Singleton {
5+
class Singleton {
76
constructor() {
8-
this.singleton = Symbol(this.namespace);
9-
107
let Class = this.constructor;
118

129
if(!Class[this.singleton]) {
@@ -24,6 +21,15 @@ class Singleton {
2421
return this[this.singleton];
2522
}
2623

24+
static set instance(instance) {
25+
this[this.singleton] = instance;
26+
return this[this.singleton];
27+
}
28+
29+
static get singleton() {
30+
return Symbol(this.namespace);
31+
}
32+
2733
static reset() {
2834
delete this[this.singleton]
2935
}

0 commit comments

Comments
 (0)