Skip to content

Instantly share code, notes, and snippets.

@stdavis
Last active August 29, 2015 13:57
Show Gist options
  • Save stdavis/9764418 to your computer and use it in GitHub Desktop.
Save stdavis/9764418 to your computer and use it in GitHub Desktop.
Custom dojo/topic Jasmine Matchers
require([
'matchers/Topics',
'dojo/topic'
], function (
Topics,
dojoTopic
) {
describe('matchers/Topics', function () {
var topicName = 'custom';
beforeEach(function () {
jasmine.addMatchers(Topics.matchers);
});
describe('toHaveBeenPublished', function () {
it('is available as a matcher', function () {
expect('test').not.toHaveBeenPublished();
});
it('subscribes to topics', function () {
Topics.listen(topicName);
dojoTopic.publish(topicName);
expect(topicName).toHaveBeenPublished();
});
it('can be used with not', function () {
expect(topicName).not.toHaveBeenPublished();
});
});
describe('toHaveBeenPublishedWith', function () {
it('checks arguments', function () {
var a1 = 'blah';
var a2 = ['hello', 'world'];
Topics.listen(topicName);
dojoTopic.publish(topicName, a1, a2);
expect(topicName).toHaveBeenPublishedWith(a1, a2);
});
it('can be used with not', function () {
Topics.listen(topicName);
dojoTopic.publish(topicName, 'blah');
expect(topicName).not.toHaveBeenPublishedWith('blah2');
});
it('returns the most recently published arguments', function () {
var arg = 'blah3';
Topics.listen(topicName);
dojoTopic.publish(topicName, 'blah');
dojoTopic.publish(topicName, arg);
expect(topicName).toHaveBeenPublishedWith(arg);
dojoTopic.publish(topicName, 'hello');
expect(topicName).not.toHaveBeenPublishedWith(arg);
});
});
describe('toHaveBeenPublishedThisManyTimes', function () {
beforeEach(function () {
Topics.listen(topicName);
dojoTopic.publish(topicName);
dojoTopic.publish(topicName);
dojoTopic.publish(topicName);
});
it('counts the number of times a topic was published', function () {
expect(topicName).toHaveBeenPublishedThisManyTimes(3);
});
it('can be used with not', function () {
expect(topicName).not.toHaveBeenPublishedThisManyTimes(2);
});
});
});
});
define([
'dojo/_base/array',
'dojo/topic'
], function (
array,
topic
) {
var publishes;
var handles;
beforeEach(function () {
publishes = {};
handles = [];
});
afterEach(function () {
array.forEach(handles, function (hand) {
hand.remove();
});
});
return {
listen: function (topicName) {
handles.push(topic.subscribe(topicName, function () {
if (!publishes[topicName]) {
publishes[topicName] = [arguments];
} else {
publishes[topicName].push(arguments);
}
}));
},
matchers: {
toHaveBeenPublished: function () {
return {
compare: function (topicName) {
var result = {
pass: publishes[topicName] !== undefined
};
// build message
var msg = 'Expected topic: "' + topicName + '"';
if (result.pass) {
msg += ' not';
}
msg += ' to have been published';
result.message = msg;
return result;
}
};
},
toHaveBeenPublishedWith: function (util, customEqualityTesters) {
return {
compare: function () {
// need to get from arguments keyword because we don't know
// how many arguments will be passed
// this means that I'm a grown up JS dev :)
var topicName = arguments[0];
// convert to true array
var expectedArgs = [].slice.call(arguments);
// remove topicName
expectedArgs.splice(0, 1);
var actualArgs = publishes[topicName][publishes[topicName].length - 1];
var result = {};
result.pass = array.every(expectedArgs, function (a, i) {
return util.equals(a, actualArgs[i], customEqualityTesters);
});
// build message
var msg = 'Expected topic: "' + topicName + '"';
if (result.pass) {
msg += ' not';
}
msg += ' to have been called with ' + JSON.stringify(expectedArgs) +
' but it was actually called with ' + JSON.stringify(actualArgs);
result.message = msg;
return result;
}
};
},
toHaveBeenPublishedThisManyTimes: function () {
return {
compare: function (topicName, expectedNumCalls) {
var result = {
pass: publishes[topicName].length === expectedNumCalls
};
var msg = 'Expected topic: "' + topicName + '"';
if (result.pass) {
msg += ' not';
}
msg += ' to have been published ' + expectedNumCalls + ' times.';
result.message = msg;
return result;
}
};
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment