Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Mar 26, 2020
1 parent 10adf5e commit 6a21134
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions test/core/utils/respondable.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ describe('axe.utils.respondable', function() {
});
event.source = window;

// this sets up the callback function but the post event from it
// is asynchronous so is will be ignored as the dispatch event
// that follows is synchronous and will trigger the callback
// (thats why there's no `done()` call)
axe.utils.respondable(window, 'Death star', null, true, function(data) {
success = true;
assert.equal(data, 'Help us Obi-Wan');
Expand Down Expand Up @@ -247,9 +251,9 @@ describe('axe.utils.respondable', function() {
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = '{ "_respondable": true, "topic": "batman" }';
event.data = JSON.stringify({
_respondable: true,
topic: 'batman',
_axeuuid: 'otherAxe'
});
event.source = window;
Expand All @@ -266,9 +270,9 @@ describe('axe.utils.respondable', function() {
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = '{ "_respondable": true, "topic": "batman", "uuid": "12" }';
event.data = JSON.stringify({
_respondable: true,
topic: 'batman',
uuid: 'not-' + mockUUID,
_axeuuid: 'otherAxe'
});
Expand All @@ -286,9 +290,9 @@ describe('axe.utils.respondable', function() {
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = '{ "uuid": "48", "topic": "batman" }';
event.data = JSON.stringify({
uuid: mockUUID,
topic: 'batman',
_axeuuid: 'otherAxe'
});
event.source = window;
Expand All @@ -300,6 +304,45 @@ describe('axe.utils.respondable', function() {
assert.isTrue(success);
});

it('should reject messages that do not have `_axeuuid`', function() {
var success = true;
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = JSON.stringify({
_respondable: true,
topic: 'batman',
uuid: mockUUID
});
event.source = window;

axe.utils.respondable(window, 'batman', 'nananana', true, function() {
success = false;
});
document.dispatchEvent(event);
assert.isTrue(success);
});

it('should reject messages from the same axe instance (`_axeuuid`)', function() {
var success = true;
var event = document.createEvent('Event');
// Define that the event name is 'build'.
event.initEvent('message', true, true);
event.data = JSON.stringify({
_respondable: true,
topic: 'batman',
_axeuuid: axe._uuid,
uuid: mockUUID
});
event.source = window;

axe.utils.respondable(window, 'batman', 'nananana', true, function() {
success = false;
});
document.dispatchEvent(event);
assert.isTrue(success);
});

it('should throw if an error message was send', function() {
var success = false;
var event = document.createEvent('Event');
Expand Down

0 comments on commit 6a21134

Please sign in to comment.