From 5cc2c13f7e5dd8eee1fc23ca64d9c4539336732e Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Tue, 15 Aug 2023 17:06:21 -0300 Subject: [PATCH] Extract testMessageEquality to helper I want to use it in a second test file. --- test/common/modules/shared_helper.js | 30 +++++++++++++++++++++++++++- test/realtime/crypto.test.js | 22 +------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/test/common/modules/shared_helper.js b/test/common/modules/shared_helper.js index c3265b1b26..4d8581ab22 100644 --- a/test/common/modules/shared_helper.js +++ b/test/common/modules/shared_helper.js @@ -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], @@ -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, @@ -255,6 +282,7 @@ define([ arrFilter: arrFilter, whenPromiseSettles: whenPromiseSettles, randomString: randomString, + testMessageEquality: testMessageEquality, }; if (typeof window !== 'undefined') { diff --git a/test/realtime/crypto.test.js b/test/realtime/crypto.test.js index 8952bcf71e..407dbc6e25 100644 --- a/test/realtime/crypto.test.js +++ b/test/realtime/crypto.test.js @@ -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); }