Skip to content

Commit bd78b4d

Browse files
committed
Added singleton test
1 parent 291f6a2 commit bd78b4d

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

Diff for: lib/modules/analytics.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
21
import {NativeModules, NativeAppEventEmitter} from 'react-native';
2+
3+
console.log('NativeModules ->', Object.keys(NativeModules).sort());
34
const FirestackAnalytics = NativeModules.FirestackAnalytics;
45

56
import promisify from '../utils/promisify'

Diff for: lib/modules/base.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Log from '../utils/log'
55

66
import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
77
const FirestackModule = NativeModules.Firestack;
8-
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
8+
let FirestackModuleEvt;
99

1010
import promisify from '../utils/promisify'
1111

@@ -53,6 +53,9 @@ export class Base {
5353
// this.eventHandlers[name] = {};
5454
// }
5555
if (!nativeModule) {
56+
if (!FirestackModuleEvt) {
57+
FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
58+
}
5659
nativeModule = FirestackModuleEvt;
5760
}
5861
const sub = nativeModule.addListener(name, cb);

Diff for: lib/utils/__tests__/singleton-test.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
jest.unmock('../singleton');
2+
3+
import Singleton from '../singleton';
4+
import sinon from 'sinon'
5+
6+
let created = 0;
7+
class TestClass extends Singleton {
8+
constructor() {
9+
super();
10+
created += 1;
11+
}
12+
13+
get namespace() {
14+
return 'firestack:TestClass'
15+
}
16+
}
17+
18+
describe('singleton', () => {
19+
let tc;
20+
21+
beforeEach(() => {
22+
created = 0;
23+
TestClass.reset();
24+
})
25+
26+
it('creates an instance of the class', () => {
27+
expect(created).toBe(0);
28+
TestClass.instance
29+
expect(created).toBe(1);
30+
})
31+
32+
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);
38+
})
39+
40+
});

Diff for: lib/utils/singleton.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
'use strict';
22

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

66
class Singleton {
77
constructor() {
8+
this.singleton = Symbol(this.namespace);
9+
810
let Class = this.constructor;
911

10-
if(!Class[singleton]) {
11-
Class[singleton] = this;
12+
if(!Class[this.singleton]) {
13+
Class[this.singleton] = this;
1214
}
1315

14-
return Class[singleton];
16+
return Class[this.singleton];
1517
}
1618

1719
static get instance() {
18-
if(!this[singleton]) {
19-
this[singleton] = new this;
20+
if(!this[this.singleton]) {
21+
this[this.singleton] = new this;
2022
}
2123

22-
return this[singleton];
24+
return this[this.singleton];
25+
}
26+
27+
static reset() {
28+
delete this[this.singleton]
2329
}
2430
}
2531

Diff for: package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
"url": "https://github.com/fullstackreact/react-native-firestack.git"
1616
},
1717
"jest": {
18-
"preset": "jest-react-native"
18+
"preset": "jest-react-native",
19+
"unmockedModulePathPatterns": [
20+
"react-native",
21+
"firestack"
22+
]
1923
},
2024
"license": "ISC",
2125
"keywords": [

0 commit comments

Comments
 (0)