@@ -4,6 +4,10 @@ import { awaitDisconnected, stub, restoreAll } from '../testHelpers';
44import DDP from '../../lib/ddp' ;
55
66describe ( '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