Skip to content

Commit

Permalink
Update tests so that each has its own helper instance
Browse files Browse the repository at this point in the history
This removes the per-file SharedHelper instance and instead makes sure
that each context which will call SharedHelper instance methods (e.g.
test cases, test hooks) has a unique SharedHelper instance that is aware
of the context from which it is being called. (Motivation for this
change already explained in 1fa5e13.)
  • Loading branch information
lawrence-forooghian committed Jul 24, 2024
1 parent 1fa5e13 commit 4f0dbbe
Show file tree
Hide file tree
Showing 39 changed files with 1,020 additions and 608 deletions.
45 changes: 30 additions & 15 deletions test/browser/connection.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

define(['shared_helper', 'chai'], function (Helper, chai) {
const helper = new Helper();

var { expect, assert } = chai;
var transportPreferenceName = 'ably-transport-preference';

Expand All @@ -29,6 +27,7 @@ define(['shared_helper', 'chai'], function (Helper, chai) {
this.timeout(60 * 1000);

before(function (done) {
const helper = Helper.forHook(this);
helper.setupApp(function (err) {
if (err) {
done(err);
Expand All @@ -43,6 +42,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @spec RTN20a */
it('device_going_offline_causes_disconnected_state', function (done) {
const helper = this.test.helper;

var realtime = helper.AblyRealtime(),
connection = realtime.connection,
offlineEvent = new Event('offline', { bubbles: true });
Expand Down Expand Up @@ -86,6 +87,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @spec RTN20b */
it('device_going_online_causes_disconnected_connection_to_reconnect_immediately', function (done) {
const helper = this.test.helper;

/* Give up trying to connect fairly quickly */
var realtime = helper.AblyRealtime({ realtimeRequestTimeout: 1000 }),
connection = realtime.connection,
Expand Down Expand Up @@ -132,6 +135,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @spec RTN20b */
it('device_going_online_causes_suspended_connection_to_reconnect_immediately', function (done) {
const helper = this.test.helper;

/* move to suspended state after 2s of being disconnected */
var realtime = helper.AblyRealtime({
disconnectedRetryTimeout: 500,
Expand Down Expand Up @@ -178,6 +183,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @spec RTN20c */
it('device_going_online_causes_connecting_connection_to_retry_attempt', function (done) {
const helper = this.test.helper;

var realtime = helper.AblyRealtime({}),
connection = realtime.connection,
onlineEvent = new Event('online', { bubbles: true }),
Expand Down Expand Up @@ -217,6 +224,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {
* @specpartial RTN16d
*/
it('page_refresh_with_recovery', function (done) {
const helper = this.test.helper;

var realtimeOpts = {
recover: function (lastConnectionDetails, cb) {
cb(true);
Expand Down Expand Up @@ -259,6 +268,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @nospec */
it('page_refresh_persist_with_denied_recovery', function (done) {
const helper = this.test.helper;

var realtimeOpts = {
recover: function (lastConnectionDetails, cb) {
cb(false);
Expand Down Expand Up @@ -302,10 +313,10 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @nospec */
it('page_refresh_with_close_on_unload', function (done) {
var realtime = helper.AblyRealtime({ closeOnUnload: true }),
var realtime = this.test.helper.AblyRealtime({ closeOnUnload: true }),
refreshEvent = new Event('beforeunload', { bubbles: true });

helper.monitorConnection(done, realtime);
this.test.helper.monitorConnection(done, realtime);

realtime.connection.once('connected', function () {
try {
Expand All @@ -326,6 +337,8 @@ define(['shared_helper', 'chai'], function (Helper, chai) {
* @specpartial RTN16d
*/
it('page_refresh_with_manual_recovery', function (done) {
const helper = this.test.helper;

var realtime = helper.AblyRealtime({ closeOnUnload: false }),
refreshEvent = new Event('beforeunload', { bubbles: true });

Expand Down Expand Up @@ -364,12 +377,12 @@ define(['shared_helper', 'chai'], function (Helper, chai) {
});

/** @nospec */
it('page_refresh_with_multiple_recovery_scopes', async () => {
it('page_refresh_with_multiple_recovery_scopes', async function () {
const realtimeOpts = { recover: (_, cb) => cb(true) },
opts1 = Object.assign({ recoveryKeyStorageName: 'recovery-1' }, realtimeOpts),
opts2 = Object.assign({ recoveryKeyStorageName: 'recovery-2' }, realtimeOpts),
realtime1 = helper.AblyRealtime(opts1),
realtime2 = helper.AblyRealtime(opts2),
realtime1 = this.test.helper.AblyRealtime(opts1),
realtime2 = this.test.helper.AblyRealtime(opts2),
refreshEvent = new Event('beforeunload', { bubbles: true });

await Promise.all([realtime1.connection.once('connected'), realtime2.connection.once('connected')]);
Expand All @@ -378,24 +391,26 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

document.dispatchEvent(refreshEvent);

helper.simulateDroppedConnection(realtime1);
helper.simulateDroppedConnection(realtime2);
this.test.helper.simulateDroppedConnection(realtime1);
this.test.helper.simulateDroppedConnection(realtime2);

await new Promise((res) => setTimeout(res, 1000));

const newRealtime1 = helper.AblyRealtime(opts1);
const newRealtime2 = helper.AblyRealtime(opts2);
const newRealtime1 = this.test.helper.AblyRealtime(opts1);
const newRealtime2 = this.test.helper.AblyRealtime(opts2);
await Promise.all([newRealtime1.connection.once('connected'), newRealtime2.connection.once('connected')]);
assert.equal(connId1, newRealtime1.connection.id);
assert.equal(connId2, newRealtime2.connection.id);

await Promise.all(
[realtime1, realtime2, newRealtime1, newRealtime2].map((rt) => helper.closeAndFinishAsync(rt)),
[realtime1, realtime2, newRealtime1, newRealtime2].map((rt) => this.test.helper.closeAndFinishAsync(rt)),
);
});

/** @nospec */
it('persist_preferred_transport', function (done) {
const helper = this.test.helper;

var realtime = helper.AblyRealtime();

realtime.connection.connectionManager.on(function (transport) {
Expand All @@ -416,15 +431,15 @@ define(['shared_helper', 'chai'], function (Helper, chai) {

/** @nospec */
it('browser_transports', function (done) {
var realtime = helper.AblyRealtime();
var realtime = this.test.helper.AblyRealtime();
try {
expect(realtime.connection.connectionManager.baseTransport).to.equal('xhr_polling');
expect(realtime.connection.connectionManager.webSocketTransportAvailable).to.be.ok;
} catch (err) {
helper.closeAndFinish(done, realtime, err);
this.test.helper.closeAndFinish(done, realtime, err);
return;
}
helper.closeAndFinish(done, realtime);
this.test.helper.closeAndFinish(done, realtime);
});
});
}
Expand Down
3 changes: 1 addition & 2 deletions test/browser/http.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';

define(['ably', 'shared_helper', 'chai'], function (Ably, Helper, chai) {
const helper = new Helper();

var rest;
var expect = chai.expect;

describe('rest/http/fetch', function () {
this.timeout(60 * 1000);
let initialXhrSupported;
before(function (done) {
const helper = Helper.forHook(this);
initialXhrSupported = Ably.Rest.Platform.Config.xhrSupported;
Ably.Rest.Platform.Config.xhrSupported = false;
helper.setupApp(function () {
Expand Down
Loading

0 comments on commit 4f0dbbe

Please sign in to comment.