Skip to content

Commit

Permalink
Extract testMessageEquality to helper
Browse files Browse the repository at this point in the history
I want to use it in a second test file.
  • Loading branch information
lawrence-forooghian committed Nov 6, 2023
1 parent c859b90 commit 5cc2c13
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
30 changes: 29 additions & 1 deletion test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ define([
'test/common/modules/client_module',
'test/common/modules/testapp_manager',
'async',
], function (testAppModule, clientModule, testAppManager, async) {
'chai',
], function (testAppModule, clientModule, testAppManager, async, chai) {
var utils = clientModule.Ably.Realtime.Utils;
var platform = clientModule.Ably.Realtime.Platform;
var BufferUtils = platform.BufferUtils;
var expect = chai.expect;
clientModule.Ably.Realtime.ConnectionManager.initTransports();
var availableTransports = utils.keysArray(clientModule.Ably.Realtime.ConnectionManager.supportedTransports),
bestTransport = availableTransports[0],
Expand Down Expand Up @@ -222,6 +225,30 @@ define([
return Math.random().toString().slice(2);
}

function testMessageEquality(one, two) {
// treat `null` same as `undefined` (using ==, rather than ===)
expect(one.encoding == two.encoding, "Encoding mismatch ('" + one.encoding + "' != '" + two.encoding + "').").to.be
.ok;

if (typeof one.data === 'string' && typeof two.data === 'string') {
expect(one.data === two.data, 'String data contents mismatch.').to.be.ok;
return;
}

if (BufferUtils.isBuffer(one.data) && BufferUtils.isBuffer(two.data)) {
expect(BufferUtils.areBuffersEqual(one.data, two.data), 'Buffer data contents mismatch.').to.equal(true);
return;
}

var json1 = JSON.stringify(one.data);
var json2 = JSON.stringify(two.data);
if (null === json1 || undefined === json1 || null === json2 || undefined === json2) {
expect(false, 'JSON stringify failed.').to.be.ok;
return;
}
expect(json1 === json2, 'JSON data contents mismatch.').to.be.ok;
}

var exports = {
setupApp: testAppModule.setup,
tearDownApp: testAppModule.tearDown,
Expand Down Expand Up @@ -255,6 +282,7 @@ define([
arrFilter: arrFilter,
whenPromiseSettles: whenPromiseSettles,
randomString: randomString,
testMessageEquality: testMessageEquality,
};

if (typeof window !== 'undefined') {
Expand Down
22 changes: 1 addition & 21 deletions test/realtime/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,7 @@ define(['ably', 'shared_helper', 'async', 'chai'], function (Ably, helper, async

function testMessageEquality(done, one, two) {
try {
// treat `null` same as `undefined` (using ==, rather than ===)
expect(one.encoding == two.encoding, "Encoding mismatch ('" + one.encoding + "' != '" + two.encoding + "').").to
.be.ok;

if (typeof one.data === 'string' && typeof two.data === 'string') {
expect(one.data === two.data, 'String data contents mismatch.').to.be.ok;
return;
}

if (BufferUtils.isBuffer(one.data) && BufferUtils.isBuffer(two.data)) {
expect(BufferUtils.areBuffersEqual(one.data, two.data), 'Buffer data contents mismatch.').to.equal(true);
return;
}

var json1 = JSON.stringify(one.data);
var json2 = JSON.stringify(two.data);
if (null === json1 || undefined === json1 || null === json2 || undefined === json2) {
expect(false, 'JSON stringify failed.').to.be.ok;
return;
}
expect(json1 === json2, 'JSON data contents mismatch.').to.be.ok;
helper.testMessageEquality(one, two);
} catch (err) {
done(err);
}
Expand Down

0 comments on commit 5cc2c13

Please sign in to comment.