Skip to content

Commit 0fcbadd

Browse files
committed
Merge branch 'master' into feature/messaging
* master: Ignore .idea Updated singleton test Added test-helper Removed debugging comments Added singleton test
2 parents f75ecd9 + 5fcaf80 commit 0fcbadd

File tree

7 files changed

+98
-24
lines changed

7 files changed

+98
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ Thumbs.dbandroid/gradle
5353
android/gradlew
5454
android/gradlew.bat
5555
android/gradle/
56+
.idea

lib/firestack.js

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

3333
constructor(options) {
34-
super(options);
34+
var instance = super(options);
3535

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

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

42-
this._remoteConfig = this.options.remoteConfig || {};
43-
delete this.options.remoteConfig;
42+
instance._remoteConfig = instance.options.remoteConfig || {};
43+
delete instance.options.remoteConfig;
4444

45-
this.configured = this.options.configure || false;
46-
this.auth = null;
45+
instance.configured = instance.options.configure || false;
46+
instance.auth = null;
4747

48-
this.eventHandlers = {};
48+
instance.eventHandlers = {};
4949

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

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

5656
configure(opts = {}) {

lib/modules/analytics.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import {NativeModules, NativeAppEventEmitter} from 'react-native';
32
const FirestackAnalytics = NativeModules.FirestackAnalytics;
43

lib/utils/__tests__/singleton-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
jest.unmock('../singleton');
2+
3+
import Singleton from '../singleton';
4+
import sinon from 'sinon'
5+
6+
class TestClass extends Singleton {
7+
static _createdCount;
8+
9+
constructor(num) {
10+
super();
11+
TestClass._createdCount += TestClass._createdCount + 1;
12+
}
13+
14+
get namespace() {
15+
return 'firestack:TestClass'
16+
}
17+
18+
static get createdCount() {
19+
return TestClass._createdCount || 0;
20+
}
21+
22+
static reset() {
23+
TestClass._createdCount = 0;
24+
}
25+
}
26+
27+
describe('singleton', () => {
28+
let tc;
29+
30+
beforeEach(() => {
31+
TestClass.reset();
32+
})
33+
34+
it('creates an instance of the class', () => {
35+
expect(TestClass.createdCount).toBe(0);
36+
new TestClass();
37+
expect(TestClass.createdCount).toBe(1);
38+
})
39+
40+
it('returns the singleton instance of the class when called a subsequent times', () => {
41+
expect(TestClass.createdCount).toBe(0);
42+
tc = new TestClass()
43+
let tc2 = new TestClass()
44+
expect(tc).toEqual(tc2);
45+
})
46+
47+
});

lib/utils/singleton.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
'use strict';
22

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

6-
class Singleton {
5+
class Singleton {
76
constructor() {
87
let Class = this.constructor;
98

10-
if(!Class[singleton]) {
11-
Class[singleton] = this;
9+
if(!Class[this.singleton]) {
10+
Class[this.singleton] = this;
1211
}
1312

14-
return Class[singleton];
13+
return Class[this.singleton];
1514
}
1615

1716
static get instance() {
18-
if(!this[singleton]) {
19-
this[singleton] = new this;
17+
if(!this[this.singleton]) {
18+
this[this.singleton] = new this;
2019
}
2120

22-
return this[singleton];
21+
return this[this.singleton];
22+
}
23+
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+
33+
static reset() {
34+
delete this[this.singleton]
2335
}
2436
}
2537

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
"url": "https://github.com/fullstackreact/react-native-firestack.git"
1616
},
1717
"jest": {
18-
"preset": "jest-react-native"
18+
"preset": "jest-react-native",
19+
"setupFiles": [
20+
],
21+
"unmockedModulePathPatterns": [
22+
"./node_modules/react",
23+
"./node_modules/react-native",
24+
"./node_modules/react-native-mock",
25+
"./node_modules/react-addons-test-utils"
26+
]
1927
},
2028
"license": "ISC",
2129
"keywords": [
@@ -48,10 +56,10 @@
4856
"mocha": "^3.0.2",
4957
"react": "^15.3.0",
5058
"react-dom": "^15.3.0",
51-
"react-native-mock": "^0.2.5",
59+
"react-native-mock": "^0.2.6",
5260
"react-test-renderer": "^15.3.0",
5361
"should": "^11.1.0",
54-
"sinon": "^1.17.5"
62+
"sinon": "^2.0.0-pre.2"
5563
},
5664
"dependencies": {
5765
"es6-symbol": "^3.1.0"

test/test-helper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require('react-native-mock/mock');
2+
3+
var { NativeEventEmitter, NativeModules } = require('react-native');
4+
5+
NativeModules.Firestack = {
6+
7+
}

0 commit comments

Comments
 (0)