mediawiki-extensions-Popups/tests/qunit/ext.popups/actions.test.js
Sam Smith 0d300867ab Make API request after 500 ms
If the user has abandoned the link or dwelled on a different link, then
don't make the API request.

Changes:
* Fix a bug in the linkDwell reducer where the activeLink was always
  being set to undefined.
* Add the private fetch action creator.
* Make the linkDwell action dispatch the result of the fetch action
  creator after 500ms.

Supporting changes:
* Make the link* action creators take DOM elements rather than jQuery
  instances so that:
  1. Testing whether the state tree has changed is an identity
     comparison.
  2. jQuery instances are constructed only when required.
* Document the ext.popups.Gateway type.
* Document the Redux.Store type.
* Rename the "previews-page-title" data attribute to
  "page-previews-title" to avoid confusion.

Change-Id: I0b1cf3337a6f8d6450ad2bd127cb292ebb73af4f
2016-11-17 13:20:11 +00:00

115 lines
2.2 KiB
JavaScript

( function ( mw, $ ) {
QUnit.module( 'ext.popups/actions' );
QUnit.test( '#boot', function ( assert ) {
var isUserInCondition = function () {
return false;
},
sessionID = '0123456789',
generateToken = function () {
return '9876543210';
};
assert.expect( 1 );
assert.deepEqual(
mw.popups.actions.boot( isUserInCondition, sessionID, generateToken ),
{
type: 'BOOT',
isUserInCondition: false,
sessionToken: '0123456789',
pageToken: '9876543210'
}
);
} );
QUnit.module( 'ext.popups/actions#linkDwell @integration', {
setup: function () {
var that = this;
this.el = $( '<a>' )
.data( 'page-previews-title', 'Foo' )
.eq( 0 );
that.state = {};
that.getState = function () {
return that.state;
};
}
} );
QUnit.test( '#linkDwell', function ( assert ) {
var done,
dispatch = this.sandbox.spy(),
gatewayDeferred = $.Deferred(),
gateway = function () {
return gatewayDeferred;
};
done = assert.async();
mw.popups.actions.linkDwell( this.el, gateway )( dispatch, this.getState );
assert.ok( dispatch.calledWith( {
type: 'LINK_DWELL',
el: this.el
} ) );
// Stub the state tree being updated.
this.state.preview = {
activeLink: this.el
};
// ---
setTimeout( function () {
var fetchThunk,
result = {};
assert.strictEqual(
dispatch.callCount,
2,
'The fetch action is dispatched after 500 ms'
);
fetchThunk = dispatch.secondCall.args[0];
fetchThunk( dispatch );
assert.ok( dispatch.calledWith( {
type: 'FETCH_START',
title: 'Foo'
} ) );
gatewayDeferred.resolve( result );
assert.ok( dispatch.calledWith( {
type: 'FETCH_END',
result: result
} ) );
done();
}, 500 );
} );
QUnit.test( '#linkDwell doesn\'t dispatch the fetch action if the active link has changed', function ( assert ) {
var done,
dispatch = this.sandbox.spy();
done = assert.async();
mw.popups.actions.linkDwell( this.el /*, gateway */ )( dispatch, this.getState );
this.state.preview = {
activeLink: undefined // Any value other than this.el.
};
setTimeout( function () {
assert.strictEqual( dispatch.callCount, 1 );
done();
}, 500 );
} );
}( mediaWiki, jQuery ) );