File tree 5 files changed +64
-10
lines changed
5 files changed +64
-10
lines changed Original file line number Diff line number Diff line change 1
-
2
1
import { NativeModules , NativeAppEventEmitter } from 'react-native' ;
2
+
3
+ console . log ( 'NativeModules ->' , Object . keys ( NativeModules ) . sort ( ) ) ;
3
4
const FirestackAnalytics = NativeModules . FirestackAnalytics ;
4
5
5
6
import promisify from '../utils/promisify'
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import Log from '../utils/log'
5
5
6
6
import { NativeModules , NativeEventEmitter , AsyncStorage } from 'react-native' ;
7
7
const FirestackModule = NativeModules . Firestack ;
8
- const FirestackModuleEvt = new NativeEventEmitter ( FirestackModule ) ;
8
+ let FirestackModuleEvt ;
9
9
10
10
import promisify from '../utils/promisify'
11
11
@@ -53,6 +53,9 @@ export class Base {
53
53
// this.eventHandlers[name] = {};
54
54
// }
55
55
if ( ! nativeModule ) {
56
+ if ( ! FirestackModuleEvt ) {
57
+ FirestackModuleEvt = new NativeEventEmitter ( FirestackModule ) ;
58
+ }
56
59
nativeModule = FirestackModuleEvt ;
57
60
}
58
61
const sub = nativeModule . addListener ( name , cb ) ;
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
3
const Symbol = require ( 'es6-symbol' ) ;
4
- const singleton = Symbol ( 'singleton' ) ;
4
+ let singleton ;
5
5
6
6
class Singleton {
7
7
constructor ( ) {
8
+ this . singleton = Symbol ( this . namespace ) ;
9
+
8
10
let Class = this . constructor ;
9
11
10
- if ( ! Class [ singleton ] ) {
11
- Class [ singleton ] = this ;
12
+ if ( ! Class [ this . singleton ] ) {
13
+ Class [ this . singleton ] = this ;
12
14
}
13
15
14
- return Class [ singleton ] ;
16
+ return Class [ this . singleton ] ;
15
17
}
16
18
17
19
static get instance ( ) {
18
- if ( ! this [ singleton ] ) {
19
- this [ singleton ] = new this ;
20
+ if ( ! this [ this . singleton ] ) {
21
+ this [ this . singleton ] = new this ;
20
22
}
21
23
22
- return this [ singleton ] ;
24
+ return this [ this . singleton ] ;
25
+ }
26
+
27
+ static reset ( ) {
28
+ delete this [ this . singleton ]
23
29
}
24
30
}
25
31
Original file line number Diff line number Diff line change 15
15
"url" : " https://github.com/fullstackreact/react-native-firestack.git"
16
16
},
17
17
"jest" : {
18
- "preset" : " jest-react-native"
18
+ "preset" : " jest-react-native" ,
19
+ "unmockedModulePathPatterns" : [
20
+ " react-native" ,
21
+ " firestack"
22
+ ]
19
23
},
20
24
"license" : " ISC" ,
21
25
"keywords" : [
You can’t perform that action at this time.
0 commit comments