File tree Expand file tree Collapse file tree 5 files changed +64
-10
lines changed
Expand file tree Collapse file tree 5 files changed +64
-10
lines changed Original file line number Diff line number Diff line change 1-
21import { NativeModules , NativeAppEventEmitter } from 'react-native' ;
2+
3+ console . log ( 'NativeModules ->' , Object . keys ( NativeModules ) . sort ( ) ) ;
34const FirestackAnalytics = NativeModules . FirestackAnalytics ;
45
56import promisify from '../utils/promisify'
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import Log from '../utils/log'
55
66import { NativeModules , NativeEventEmitter , AsyncStorage } from 'react-native' ;
77const FirestackModule = NativeModules . Firestack ;
8- const FirestackModuleEvt = new NativeEventEmitter ( FirestackModule ) ;
8+ let FirestackModuleEvt ;
99
1010import 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 ) ;
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 11'use strict' ;
22
33const Symbol = require ( 'es6-symbol' ) ;
4- const singleton = Symbol ( 'singleton' ) ;
4+ let singleton ;
55
66class 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
Original file line number Diff line number Diff line change 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" : [
You can’t perform that action at this time.
0 commit comments