Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Last active February 23, 2018 23:03
Show Gist options
  • Save crunchie84/82dc64060dcb429704d8beb03e3a1d9b to your computer and use it in GitHub Desktop.
Save crunchie84/82dc64060dcb429704d8beb03e3a1d9b to your computer and use it in GitHub Desktop.
Rxjs5 test compare
const assert = require('chai').assert;
/**
* parts of code taken from https://github.com/Reactive-Extensions/RxJS/blob/master/tests/helpers/reactiveassert.js#L32
*
* and modified to work with the rxjs5 testing shim which has no differentiation between values and predicates
*/
exports.assertEqual = function assertEqual(actual, expected) {
const comparer = require('./is-equal'); // copied and adapted from https://raw.githubusercontent.com/Reactive-Extensions/RxJS/master/src/core/internal/isequal.js
let isOk = true;
if (expected.length !== actual.length) {
return assert.fail(expected.length, actual.length, `Not equal length. Expected: ${expected.length} actual:${actual.length}`);
}
for (let i = 0, len = expected.length; i < len; i++) {
const e = expected[i], a = actual[i];
// if the value is a function then use it as assertion instead of equality comparison
let compareDelegate = null;
if (e.notification.value && typeof e.notification.value === 'function') {
compareDelegate = e.notification.value;
} else if (e.notification.error && typeof e.notification.error === 'function') {
compareDelegate = e.notification.error;
}
if (compareDelegate !== null) {
isOk = e.frame === a.frame && e.notification.kind === a.notification.kind;
if (!isOk) {
return assert.fail(expected[i], actual[i], `Emission ${1 + i}@${a.time} did not match timing or kind of expected emission. Expected ${e.notification.kind}@${e.frame} \n\n Emission not matching: \n\n ${JSON.stringify(a.notification.value, jsonSerialiserRemoveSchedulers, ' ')}`);
}
isOk = compareDelegate(a.notification);
if (!isOk) {
return assert.fail(expected[i], actual[i], `Emission ${1 + i}@${a.time} did not match predicate for emission @${e.time}. \n Predicate: \n\n${e.notification.value} \n\n Emission not matching: \n\n ${JSON.stringify(a.notification.value, jsonSerialiserRemoveSchedulers, ' ')}`);
}
} else {
isOk = comparer(e, a);
if (!isOk) {
return assert.fail(e, a, `Emission ${1 + i} not as expected. \n Expected: \n\n${JSON.stringify(e, jsonSerialiserRemoveSchedulers, ' ')} \n\n But got: \n\n ${JSON.stringify(a, jsonSerialiserRemoveSchedulers, ' ')}`);
}
}
}
};
function jsonSerialiserRemoveSchedulers(key, value) {
// Filtering out properties of schedulers to make the assertions more readable
if (key != null && key.startsWith('_') || key === 'scheduler') {
return undefined;
}
return value;
}
const rxStreamCompareUtils = require('./rx-collection-compare');
const TestScheduler = require('@kwonoj/rxjs-testscheduler-compat').TestScheduler;
const next = require('@kwonoj/rxjs-testscheduler-compat').next;
describe('example-test', () => {
it('compares using a predicate', () => {
const scheduler = new TestScheduler();
const results = scheduler.startScheduler(
() => Rx.Observable.just({foo: { bar: 1234 }}).delay(100, scheduler)
);
rxStreamCompareUtils.assertEquals(results.messages, [
next(200, evt => evt.value && evt.value.foo.bar === 1234)
]);
});
});
@crunchie84
Copy link
Author

note; the ./is-equal.js file is absent in this gist; it is a 99.9% copy from https://raw.githubusercontent.com/Reactive-Extensions/RxJS/master/src/core/internal/isequal.js only fixing references and exporting the complete file as module.exports = isEqual;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment