From 19cc59727ddc0442fb338bfcaadf5f1d730ca642 Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Tue, 21 Nov 2023 09:44:34 -0800 Subject: [PATCH] test native <-> polyfill publish/subscribe --- test/cross-communication.spec.js | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 test/cross-communication.spec.js diff --git a/test/cross-communication.spec.js b/test/cross-communication.spec.js new file mode 100644 index 0000000..4958915 --- /dev/null +++ b/test/cross-communication.spec.js @@ -0,0 +1,52 @@ +const test = require('tape'); + +const { hasDiagnosticsChannel } = require('../checks'); + +if (!hasDiagnosticsChannel()) { + test('cross-communication', t => { + t.comment(`The current version of Node.js, ${process.version}, does not include diagnostics_channel. Skipping test.`); + t.end(); + }); + + return; +} + +const polyfill = require('../dc-polyfill.js'); +const native = require('../dc-polyfill.js'); + +test('subscribe in native, publish from polyfill', t => { + const native_ch = native.channel('foo'); + const polyfill_ch = polyfill.channel('foo'); + + t.equal(native_ch.hasSubscribers, false, 'native ch should have no subscribers yet'); + t.equal(polyfill_ch.hasSubscribers, false, 'polyfill ch should have no subscribers yet'); + + native.subscribe('foo', (msg) => { + t.equal(msg, 'bar'); + t.end(); + }); + + t.equal(native_ch.hasSubscribers, true, 'native ch should now have subscribers'); + t.equal(polyfill_ch.hasSubscribers, true, 'polyfill ch should now have subscribers'); + + polyfill_ch.publish('bar'); +}); + +test('subscribe in polyfill, publish from native', t => { + const native_ch = native.channel('foo2'); + const polyfill_ch = polyfill.channel('foo2'); + + t.equal(native_ch.hasSubscribers, false, 'native ch should have no subscribers yet'); + t.equal(polyfill_ch.hasSubscribers, false, 'polyfill ch should have no subscribers yet'); + + polyfill.subscribe('foo2', (msg) => { + t.equal(msg, 'bar'); + t.end(); + }); + + t.equal(native_ch.hasSubscribers, true, 'native ch should now have subscribers'); + t.equal(polyfill_ch.hasSubscribers, true, 'polyfill ch should now have subscribers'); + + native_ch.publish('bar'); +}); +