Skip to content

Commit ac60127

Browse files
committed
Initial test setup
1 parent 7acb21d commit ac60127

File tree

14 files changed

+135
-20
lines changed

14 files changed

+135
-20
lines changed

lib/firestack.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @providesModule Firestack
33
* @flow
44
*/
5-
import Log from './log'
5+
import Log from './utils/log'
66

77
const firebase = require('firebase');
88

@@ -16,8 +16,8 @@ import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
1616
const FirestackModule = NativeModules.Firestack;
1717
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
1818

19-
import promisify from './promisify'
20-
import Singleton from './singleton'
19+
import promisify from './utils/promisify'
20+
import Singleton from './utils/singleton'
2121

2222
import RemoteConfig from './modules/remoteConfig'
2323
import {Authentication} from './modules/authentication'

lib/modules/analytics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {NativeModules, NativeAppEventEmitter} from 'react-native';
33
const FirestackAnalytics = NativeModules.FirestackAnalytics;
44

5-
import promisify from '../promisify'
5+
import promisify from '../utils/promisify'
66
import { Base } from './base'
77

88
export class Analytics extends Base {

lib/modules/authentication.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {NativeModules, NativeEventEmitter} from 'react-native';
33
const FirestackAuth = NativeModules.FirestackAuth
44
const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth);
55

6-
import promisify from '../promisify'
6+
import promisify from '../utils/promisify'
77
import { Base } from './base'
88

99
export class Authentication extends Base {

lib/modules/base.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
2-
* @providesModule Firestack
32
* @flow
43
*/
5-
import Log from '../log'
4+
import Log from '../utils/log'
65

76
import {NativeModules, NativeEventEmitter, AsyncStorage} from 'react-native';
87
const FirestackModule = NativeModules.Firestack;
98
const FirestackModuleEvt = new NativeEventEmitter(FirestackModule);
109

11-
import promisify from '../promisify'
10+
import promisify from '../utils/promisify'
1211

1312
let logs = {};
1413
export class Base {

lib/modules/database.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {NativeModules, NativeEventEmitter} from 'react-native';
55
const FirestackDatabase = NativeModules.FirestackDatabase;
66
const FirestackDatabaseEvt = new NativeEventEmitter(FirestackDatabase);
77

8-
import promisify from '../promisify'
8+
import promisify from '../utils/promisify'
99
import { Base, ReferenceBase } from './base'
1010

1111
let dbSubscriptions = {};

lib/modules/presence.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import promisify from '../promisify'
1+
import promisify from '../utils/promisify'
22
import { Base, ReferenceBase } from './base'
33

44
class PresenceRef extends ReferenceBase {

lib/modules/storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {NativeModules, NativeEventEmitter} from 'react-native';
33
const FirestackStorage = NativeModules.FirestackStorage;
44
const FirestackStorageEvt = new NativeEventEmitter(FirestackStorage);
55

6-
import promisify from '../promisify'
6+
import promisify from '../utils/promisify'
77
import { Base, ReferenceBase } from './base'
88

99
class StorageRef extends ReferenceBase {

lib/utils/__tests__/log-test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
jest.unmock('../log');
2+
jest.unmock('../window-or-global')
3+
import Log from '../log';
4+
import root from '../window-or-global';
5+
6+
describe('Log', () => {
7+
let log;
8+
9+
beforeEach(() => {
10+
root.localStorage = {};
11+
})
12+
13+
it('does not explode on import', () => {
14+
log = new Log('test');
15+
expect(log).toBeDefined();
16+
});
17+
18+
it('can be enabled', () => {
19+
log = new Log('test', true);
20+
expect(log.enabled).toBeTruthy();
21+
});
22+
23+
it('can be disabled', () => {
24+
log = new Log('test', true);
25+
expect(log.enabled).toBeTruthy();
26+
log.enable(false);
27+
expect(log.enabled).toBeFalsy();
28+
});
29+
30+
describe('levels', () => {
31+
beforeEach(() => {
32+
log = new Log('test', true);
33+
});
34+
35+
it('has an info level', () => {
36+
expect(() => {
37+
log.info('Testing')
38+
}).not.toThrow();
39+
});
40+
41+
it('has a debug level', () => {
42+
expect(() => {
43+
log.debug('Testing')
44+
}).not.toThrow();
45+
});
46+
47+
it('has an error level', () => {
48+
expect(() => {
49+
log.error('Testing')
50+
}).not.toThrow();
51+
});
52+
53+
})
54+
55+
})
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
jest.unmock('../promisify');
2+
3+
import promisify from '../promisify';
4+
import sinon from 'sinon'
5+
6+
describe('promisify', () => {
7+
let NativeModule;
8+
9+
beforeEach(() => {
10+
NativeModule = {
11+
nativeFn: sinon.spy(),
12+
nativeRet: (callback) => callback(null, true),
13+
nativeErr: (callback) => callback({type: 'error'})
14+
}
15+
});
16+
17+
it('returns a function to be called', () => {
18+
expect(
19+
typeof promisify('nativeFn', NativeModule)
20+
).toEqual('function')
21+
})
22+
23+
it('returns a function which returns a promise', () => {
24+
const promise = promisify('nativeFn', NativeModule)();
25+
expect(promise instanceof Promise).toBeTruthy();
26+
});
27+
28+
it('calls the native function when called', () => {
29+
const promise = promisify('nativeFn', NativeModule)();
30+
expect(NativeModule.nativeFn.called).toBeTruthy();
31+
});
32+
33+
it('resolves its promise when the native function returns', (done) => {
34+
const promise = promisify('nativeRet', NativeModule)();
35+
promise.then((res) => {
36+
expect(res).toBeTruthy();
37+
done();
38+
});
39+
});
40+
41+
it('rejects when the natie function returns an error', (done) => {
42+
const promise = promisify('nativeErr', NativeModule)();
43+
promise.catch((err) => {
44+
expect(err.type).toBe('error')
45+
done();
46+
})
47+
})
48+
49+
})

lib/log.js renamed to lib/utils/log.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// document hack
2-
if (!global.document) {
3-
global.document = { documentElement: { style: { WebkitAppearance: true } } }
4-
}
5-
if(!window.localStorage) window.localStorage = {};
2+
import root from './window-or-global'
3+
(function (global) {
4+
if (!global.document) {
5+
global.document = { documentElement: { style: { WebkitAppearance: true } } }
6+
}
7+
if(!global.localStorage) global.localStorage = {};
8+
})(root);
69

710
let debug = () => {};
811

@@ -11,14 +14,17 @@ export class Log {
1114
this._namespace = namespace || 'firestack';
1215
this.l = null;
1316
this.loggers = {};
17+
this.enabled = false;
1418
this.enable(enable);
1519
}
1620

1721
enable(booleanOrStringDebug) {
18-
if (booleanOrStringDebug) {
19-
window.localStorage.debug =
20-
typeof booleanOrStringDebug === 'string' ? booleanOrStringDebug : '*';
21-
}
22+
root.localStorage.debug =
23+
typeof booleanOrStringDebug === 'string' ?
24+
booleanOrStringDebug :
25+
(booleanOrStringDebug ? '*' : booleanOrStringDebug);
26+
27+
this.enabled = !!root.localStorage.debug;
2228
}
2329

2430
info(...args) {

0 commit comments

Comments
 (0)