Skip to content

Commit

Permalink
Merge pull request #153 from meteorrn/fix/autoConnnect
Browse files Browse the repository at this point in the history
fix: strictly respect autoConnect flag
  • Loading branch information
jankapunkt authored Apr 17, 2024
2 parents 2e269a5 + bc490cc commit 768b9d0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
2 changes: 1 addition & 1 deletion companion-packages/meteorrn-local/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"author": "",
"license": "ISC",
"peerDependencies": {
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.0"
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.1"
}
}
2 changes: 1 addition & 1 deletion companion-packages/meteorrn-ndev-mfa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"author": "",
"license": "ISC",
"peerDependencies": {
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.0"
"@meteorrn/core":">=2.0.7 || ^2.8.1-rc.1"
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meteorrn/core",
"version": "2.8.1-rc.0",
"version": "2.8.1-rc.1",
"description": "Full Meteor Client for React Native",
"main": "src/index.js",
"repository": {
Expand Down
4 changes: 0 additions & 4 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ const Meteor = {
}

// Reconnect if we lose internet

NetInfo.addEventListener(
({ type, isConnected, isInternetReachable, isWifiEnabled }) => {
if (isConnected && Data.ddp.autoReconnect) {
Expand All @@ -324,10 +323,7 @@ const Meteor = {
console.warn(
'Warning: NetInfo not installed, so DDP will not automatically reconnect'
);
Data.ddp.connect();
}
} else {
Data.ddp.connect();
}
},
subscribe(name) {
Expand Down
53 changes: 47 additions & 6 deletions test/src/Meteor.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { awaitDisconnected, stub, restoreAll } from '../testHelpers';
import DDP from '../../lib/ddp';

describe('Meteor - integration', function () {
afterEach(() => {
restoreAll();
});

it('uses the default async storage if none is defined', function () {
const fallback =
require('@react-native-async-storage/async-storage').default;
Expand All @@ -13,9 +17,37 @@ describe('Meteor - integration', function () {

describe(Meteor.connect.name, () => {
before(awaitDisconnected);
afterEach(() => {
restoreAll();

it('requires manual connect if autoConnect is set to false', function (done) {
this.timeout(3000);
expect(Meteor.getData().ddp.status).to.equal('disconnected');
stub(DDP.prototype, 'on', () => {});
let connectCalled = 0;
stub(DDP.prototype, 'connect', () => {
done(new Error('should not automatically call connect'));
});

const AsyncStorage = {
getItem: async () => {},
setItem: async () => {},
removeItem: async () => {},
};

const endpoint = `ws://localhost:3000/websocket`;
Meteor.connect(endpoint, {
AsyncStorage,
NetInfo: null,
autoConnect: false,
});

// let's wait some time to make sure no internals
// unintentionally call ddp.connect before we do
setTimeout(() => {
expect(Meteor.getData().ddp.status).to.equal('disconnected');
done();
}, 2900);
});

it('allows to bypass NetInfo', (done) => {
stub(DDP.prototype, 'on', () => {});
stub(DDP.prototype, 'connect', done);
Expand All @@ -30,12 +62,20 @@ describe('Meteor - integration', function () {
Meteor.connect(endpoint, {
AsyncStorage,
NetInfo: null,
autoConnect: false,
});
});
it('allows to pass a custom configured NetInfo', (done) => {
stub(DDP.prototype, 'on', () => {});
stub(DDP.prototype, 'connect', done);

let connectCalled = 0;
stub(DDP.prototype, 'connect', () => {
connectCalled++;
if (connectCalled > 1) {
done(new Error('should not call more than once!'));
} else {
done();
}
});

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

const NetInfo = {
addEventListener: (cb) => {
setTimeout(() => cb({ isConnected: true }), 300);
setTimeout(() => {
cb({ isConnected: true });
}, 0);
},
};

const endpoint = `ws://localhost:3000/websocket`;
Meteor.connect(endpoint, {
AsyncStorage,
NetInfo,
autoConnect: false,
autoReconnect: true,
});
});
Expand Down

0 comments on commit 768b9d0

Please sign in to comment.