Created
January 19, 2016 03:48
-
-
Save gwing33/086df87d1789bc51b772 to your computer and use it in GitHub Desktop.
RxJS JestJS / Jasmine2 Custom Matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* * * | |
* Just a quick tester to help with Rx testing... | |
* - - - | |
* Be sure to include in the jest config... | |
* "setupTestFrameworkScriptFile": "<rootDir>/{pathTo}/customMatchers.js", | |
* "testRunner": "<rootDir>/../node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js" | |
* - - - | |
* Usage...should be something similar to what is below (untested) | |
* | |
* const subject = new Rx.Subject(); | |
* const scheduler = new Rx.TestScheduler(); | |
* const onNext = Rx.ReactiveTest.onNext; | |
* | |
* results = scheduler.startScheduler(() => { | |
* return subject.asObserver(); | |
* }); | |
* | |
* scheduler.scheduleAbsolute({}, 300, () => { | |
* subject.onNext({topic: 'helloWorld'}); | |
* }); | |
* | |
* expect(results.messages).toHaveEqualElements(onNext(300, {topic: 'helloWorld'}), ...onNext({})) // If you have more... | |
* * */ | |
'use strict'; | |
function areElementsEqual(actual, expected, comparer) { | |
var Rx = require('rx'); | |
var theComparer = comparer || Rx.internals.isEqual; | |
if (expected.length !== actual.length) { | |
return false; | |
} | |
for (var id = 0; id < expected.length; id++) { | |
if (!theComparer(expected[id], actual[id])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
var customMatchers = { | |
toHaveEqualElements: function(util, customEqualityTesters) { | |
return { | |
compare: function(actual, expected) { | |
var len = arguments.length; | |
var expecteded = new Array(len-1); | |
for (var id = 1; id < len; id++) { | |
expecteded[id-1] = arguments[id]; | |
} | |
return { pass: areElementsEqual(actual, expecteded) }; | |
} | |
}; | |
} | |
} | |
jasmine.getEnv().beforeEach(function() { | |
jasmine.addMatchers(customMatchers); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment