Skip to content

Commit 768b9d0

Browse files
authored
Merge pull request #153 from meteorrn/fix/autoConnnect
fix: strictly respect autoConnect flag
2 parents 2e269a5 + bc490cc commit 768b9d0

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

companion-packages/meteorrn-local/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"author": "",
1010
"license": "ISC",
1111
"peerDependencies": {
12-
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.0"
12+
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.1"
1313
}
1414
}

companion-packages/meteorrn-ndev-mfa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"author": "",
1010
"license": "ISC",
1111
"peerDependencies": {
12-
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.0"
12+
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.1"
1313
}
1414
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meteorrn/core",
3-
"version": "2.8.1-rc.0",
3+
"version": "2.8.1-rc.1",
44
"description": "Full Meteor Client for React Native",
55
"main": "src/index.js",
66
"repository": {

src/Meteor.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ const Meteor = {
312312
}
313313

314314
// Reconnect if we lose internet
315-
316315
NetInfo.addEventListener(
317316
({ type, isConnected, isInternetReachable, isWifiEnabled }) => {
318317
if (isConnected && Data.ddp.autoReconnect) {
@@ -324,10 +323,7 @@ const Meteor = {
324323
console.warn(
325324
'Warning: NetInfo not installed, so DDP will not automatically reconnect'
326325
);
327-
Data.ddp.connect();
328326
}
329-
} else {
330-
Data.ddp.connect();
331327
}
332328
},
333329
subscribe(name) {

test/src/Meteor.tests.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { awaitDisconnected, stub, restoreAll } from '../testHelpers';
44
import DDP from '../../lib/ddp';
55

66
describe('Meteor - integration', function () {
7+
afterEach(() => {
8+
restoreAll();
9+
});
10+
711
it('uses the default async storage if none is defined', function () {
812
const fallback =
913
require('@react-native-async-storage/async-storage').default;
@@ -13,9 +17,37 @@ describe('Meteor - integration', function () {
1317

1418
describe(Meteor.connect.name, () => {
1519
before(awaitDisconnected);
16-
afterEach(() => {
17-
restoreAll();
20+
21+
it('requires manual connect if autoConnect is set to false', function (done) {
22+
this.timeout(3000);
23+
expect(Meteor.getData().ddp.status).to.equal('disconnected');
24+
stub(DDP.prototype, 'on', () => {});
25+
let connectCalled = 0;
26+
stub(DDP.prototype, 'connect', () => {
27+
done(new Error('should not automatically call connect'));
28+
});
29+
30+
const AsyncStorage = {
31+
getItem: async () => {},
32+
setItem: async () => {},
33+
removeItem: async () => {},
34+
};
35+
36+
const endpoint = `ws://localhost:3000/websocket`;
37+
Meteor.connect(endpoint, {
38+
AsyncStorage,
39+
NetInfo: null,
40+
autoConnect: false,
41+
});
42+
43+
// let's wait some time to make sure no internals
44+
// unintentionally call ddp.connect before we do
45+
setTimeout(() => {
46+
expect(Meteor.getData().ddp.status).to.equal('disconnected');
47+
done();
48+
}, 2900);
1849
});
50+
1951
it('allows to bypass NetInfo', (done) => {
2052
stub(DDP.prototype, 'on', () => {});
2153
stub(DDP.prototype, 'connect', done);
@@ -30,12 +62,20 @@ describe('Meteor - integration', function () {
3062
Meteor.connect(endpoint, {
3163
AsyncStorage,
3264
NetInfo: null,
33-
autoConnect: false,
3465
});
3566
});
3667
it('allows to pass a custom configured NetInfo', (done) => {
3768
stub(DDP.prototype, 'on', () => {});
38-
stub(DDP.prototype, 'connect', done);
69+
70+
let connectCalled = 0;
71+
stub(DDP.prototype, 'connect', () => {
72+
connectCalled++;
73+
if (connectCalled > 1) {
74+
done(new Error('should not call more than once!'));
75+
} else {
76+
done();
77+
}
78+
});
3979

4080
const AsyncStorage = {
4181
getItem: async () => {},
@@ -45,15 +85,16 @@ describe('Meteor - integration', function () {
4585

4686
const NetInfo = {
4787
addEventListener: (cb) => {
48-
setTimeout(() => cb({ isConnected: true }), 300);
88+
setTimeout(() => {
89+
cb({ isConnected: true });
90+
}, 0);
4991
},
5092
};
5193

5294
const endpoint = `ws://localhost:3000/websocket`;
5395
Meteor.connect(endpoint, {
5496
AsyncStorage,
5597
NetInfo,
56-
autoConnect: false,
5798
autoReconnect: true,
5899
});
59100
});

0 commit comments

Comments
 (0)