Skip to content

Commit 0f00542

Browse files
author
Nathaniel Dsouza
authored
Merge pull request #83 from jankapunkt/feature-test-coverage
Implement tests + coverage
2 parents 0e0bbcd + 19c550c commit 0f00542

27 files changed

+5925
-1139
lines changed

.babelrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"presets": ["react-native"]
2+
"presets": ["module:metro-react-native-babel-preset"],
3+
"plugins": ["istanbul"]
34
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pids
1111
lib-cov
1212

1313
# Coverage directory used by tools like istanbul
14+
.nyc_output
1415
coverage
1516

1617
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
@@ -29,3 +30,4 @@ node_modules
2930
.vscode
3031
.idea
3132
.DS_Store
33+

.mocharc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// transpile all es6+ code in this project (but not node_modules)
2+
const babelRegister = require('@babel/register');
3+
babelRegister();
4+
5+
// for more options see here https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.yml
6+
module.exports = {
7+
recursive: true,
8+
reporter: "spec",
9+
retries: 0,
10+
slow: 20,
11+
timeout: 2000,
12+
ui: "bdd",
13+
require: ['test/hooks/mockServer.js']
14+
}

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
examples
22
.github
3-
companion-packages
3+
companion-packages
4+
.nyc_output
5+
coverage

.nycrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@istanbuljs/nyc-config-babel"
3+
}

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
package.json
22
example/
3+
.nyc_output
4+
coverage
5+
node_modules

helpers/reactNativeBindings.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const bindings = {}
2+
3+
// TODO:
4+
// we should consider implementing an injection-based pattern for
5+
// unstable_batchedUpdates and runAfterInteractions simply because
6+
// - this allows a much easier test-setup, where we don't need to mock modules
7+
// - we can be independent of any folder structure or refactoring
8+
// that happens in react-native
9+
// - we can provide a default behaviour out-of-the box that gets overwritten
10+
// when devs inject their favourable behaviour
11+
12+
try {
13+
require.resolve('react-native')
14+
bindings.batchedUpdates = require('react-native/Libraries/Renderer/shims/ReactNative').unstable_batchedUpdates;
15+
bindings.runAfterInteractions = require('react-native').InteractionManager.runAfterInteractions;
16+
} catch (e) {
17+
// if the module is not installed (for example when running tests)
18+
// we fall back to some defaults that seem to be close to what
19+
// the original functions implement
20+
bindings.batchedUpdates = cb => cb()
21+
bindings.runAfterInteractions = fn => setTimeout(() => fn(), 50)
22+
}
23+
24+
module.exports = bindings

lib/mongo-id.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import EJSON from 'ejson';
33

44
const MongoID = {};
55

6-
MongoID._looksLikeObjectID = function(str) {
7-
return str.length === 24 && str.match(/^[0-9a-f]*$/);
6+
const objectIdRegex = /^[0-9a-f]*$/
7+
8+
MongoID._looksLikeObjectID = function _looksLikeObjectID(str) {
9+
return str.length === 24 && objectIdRegex.test(str);
810
};
911

10-
MongoID.ObjectID = function(hexString) {
12+
MongoID.ObjectID = function ObjectID(hexString) {
1113
//random-based impl of Mongo ObjectID
1214
var self = this;
1315
if (hexString) {
@@ -18,32 +20,33 @@ MongoID.ObjectID = function(hexString) {
1820
// meant to work with _.isEqual(), which relies on structural equality
1921
self._str = hexString;
2022
} else {
21-
self._str = Random.hexString(24);
23+
// self._str = Random.hexString(24);
24+
throw new Error('Random.hexString not implemented, please pass a hexString');
2225
}
2326
};
2427

25-
MongoID.ObjectID.prototype.toString = function() {
28+
MongoID.ObjectID.prototype.toString = function toString() {
2629
var self = this;
2730
return 'ObjectID("' + self._str + '")';
2831
};
2932

30-
MongoID.ObjectID.prototype.equals = function(other) {
33+
MongoID.ObjectID.prototype.equals = function equals(other) {
3134
var self = this;
3235
return (
3336
other instanceof MongoID.ObjectID && self.valueOf() === other.valueOf()
3437
);
3538
};
3639

37-
MongoID.ObjectID.prototype.clone = function() {
40+
MongoID.ObjectID.prototype.clone = function clone() {
3841
var self = this;
3942
return new MongoID.ObjectID(self._str);
4043
};
4144

42-
MongoID.ObjectID.prototype.typeName = function() {
45+
MongoID.ObjectID.prototype.typeName = function typeName() {
4346
return 'oid';
4447
};
4548

46-
MongoID.ObjectID.prototype.getTimestamp = function() {
49+
MongoID.ObjectID.prototype.getTimestamp = function getTimestamp() {
4750
var self = this;
4851
return parseInt(self._str.substr(0, 8), 16);
4952
};
@@ -56,7 +59,7 @@ EJSON.addType('oid', function(str) {
5659
return new MongoID.ObjectID(str);
5760
});
5861

59-
MongoID.idStringify = function(id) {
62+
MongoID.idStringify = function idStringify(id) {
6063
if (id instanceof MongoID.ObjectID) {
6164
return id.valueOf();
6265
} else if (typeof id === 'string') {
@@ -85,7 +88,7 @@ MongoID.idStringify = function(id) {
8588
}
8689
};
8790

88-
MongoID.idParse = function(id) {
91+
MongoID.idParse = function idParse(id) {
8992
if (id === '') {
9093
return id;
9194
} else if (id === '-') {
@@ -100,3 +103,5 @@ MongoID.idParse = function(id) {
100103
return id;
101104
}
102105
};
106+
107+
module.exports = MongoID;

lib/socket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import EventEmitter from 'wolfy87-eventemitter';
2+
// TODO: 0.60+ supports import { EventEmitter } from 'events';
23
import EJSON from 'ejson';
34
import './mongo-id'; // Register mongo object ids */
45

lib/utils.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ export function uniqueId() {
66
return (i++).toString();
77
}
88

9+
// XXX: we should extract this function and let clients inject
10+
// it, so they can leverage react-native crypto packages that
11+
// implement a secure hashing algorithm like bcrypt
912
export function hashPassword(password) {
1013
return {
1114
digest: SHA256(password).toString(), // lgtm [js/insufficient-password-hash]
@@ -78,8 +81,7 @@ export function isPlainObject(obj) {
7881

7982
// Own properties are enumerated firstly, so to speed up,
8083
// if last one is own, then all properties are own.
81-
for (key in obj) {
82-
}
84+
for (key in obj) {}
8385

8486
return key === undefined || hasOwn.call(obj, key);
8587
}

0 commit comments

Comments
 (0)