2016-11-16 19:45:10 +00:00
|
|
|
( function ( mw, $ ) {
|
2016-11-14 19:37:11 +00:00
|
|
|
|
2016-12-06 14:02:13 +00:00
|
|
|
function generateToken() {
|
|
|
|
return '9876543210';
|
|
|
|
}
|
|
|
|
|
2016-11-14 19:37:11 +00:00
|
|
|
QUnit.module( 'ext.popups/actions' );
|
|
|
|
|
2016-11-16 19:45:10 +00:00
|
|
|
QUnit.test( '#boot', function ( assert ) {
|
2016-12-14 14:24:21 +00:00
|
|
|
var config = new mw.Map(),
|
2016-12-01 14:56:46 +00:00
|
|
|
stubUser = mw.popups.tests.stubs.createStubUser( /* isAnon = */ true ),
|
|
|
|
stubUserSettings,
|
|
|
|
action;
|
2016-11-30 13:40:08 +00:00
|
|
|
|
|
|
|
config.set( {
|
|
|
|
wgTitle: 'Foo',
|
|
|
|
wgNamespaceNumber: 1,
|
2016-12-01 13:12:29 +00:00
|
|
|
wgArticleId: 2,
|
2017-01-25 14:09:08 +00:00
|
|
|
wgUserEditCount: 3,
|
|
|
|
wgPopupsConflictsWithNavPopupGadget: true
|
2016-11-30 13:40:08 +00:00
|
|
|
} );
|
|
|
|
|
2016-12-01 14:56:46 +00:00
|
|
|
stubUserSettings = {
|
|
|
|
getPreviewCount: function () {
|
|
|
|
return 22;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-16 19:45:10 +00:00
|
|
|
assert.expect( 1 );
|
|
|
|
|
2016-12-01 14:56:46 +00:00
|
|
|
action = mw.popups.actions.boot(
|
2016-12-14 14:24:21 +00:00
|
|
|
false,
|
2016-12-01 14:56:46 +00:00
|
|
|
stubUser,
|
|
|
|
stubUserSettings,
|
|
|
|
generateToken,
|
|
|
|
config
|
|
|
|
);
|
|
|
|
|
2016-11-14 19:37:11 +00:00
|
|
|
assert.deepEqual(
|
2016-12-01 14:56:46 +00:00
|
|
|
action,
|
2016-11-14 19:37:11 +00:00
|
|
|
{
|
|
|
|
type: 'BOOT',
|
2016-12-14 14:24:21 +00:00
|
|
|
isEnabled: false,
|
2017-01-25 14:09:08 +00:00
|
|
|
isNavPopupsEnabled: true,
|
2016-11-14 19:37:11 +00:00
|
|
|
sessionToken: '0123456789',
|
2016-11-30 13:40:08 +00:00
|
|
|
pageToken: '9876543210',
|
|
|
|
page: {
|
|
|
|
title: 'Foo',
|
|
|
|
namespaceID: 1,
|
|
|
|
id: 2
|
|
|
|
},
|
2016-12-01 13:12:29 +00:00
|
|
|
user: {
|
|
|
|
isAnon: true,
|
2016-12-01 14:56:46 +00:00
|
|
|
editCount: 3,
|
|
|
|
previewCount: 22
|
2016-12-01 13:12:29 +00:00
|
|
|
}
|
2016-11-14 19:37:11 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2016-11-28 12:00:07 +00:00
|
|
|
/**
|
|
|
|
* Stubs `mw.popups.wait` and adds the deferred and its promise as properties
|
|
|
|
* of the module.
|
|
|
|
*
|
|
|
|
* @param {Object} module
|
|
|
|
*/
|
|
|
|
function setupWait( module ) {
|
|
|
|
module.waitDeferred = $.Deferred();
|
|
|
|
module.waitPromise = module.waitDeferred.promise();
|
|
|
|
|
|
|
|
module.sandbox.stub( mw.popups, 'wait', function () {
|
|
|
|
return module.waitPromise;
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2016-12-19 12:53:11 +00:00
|
|
|
/**
|
|
|
|
* Sets up an `a` element that can be passed to action creators.
|
|
|
|
*
|
|
|
|
* @param {Object} module
|
|
|
|
*/
|
|
|
|
function setupEl( module ) {
|
|
|
|
module.el = $( '<a>' )
|
|
|
|
.data( 'page-previews-title', 'Foo' )
|
|
|
|
.eq( 0 );
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:45:10 +00:00
|
|
|
QUnit.module( 'ext.popups/actions#linkDwell @integration', {
|
|
|
|
setup: function () {
|
|
|
|
var that = this;
|
|
|
|
|
2017-01-10 23:23:40 +00:00
|
|
|
this.state = {
|
|
|
|
preview: {}
|
|
|
|
};
|
2016-11-30 12:01:45 +00:00
|
|
|
this.getState = function () {
|
2016-11-16 19:45:10 +00:00
|
|
|
return that.state;
|
|
|
|
};
|
2016-11-25 10:40:33 +00:00
|
|
|
|
2016-11-28 12:00:07 +00:00
|
|
|
setupWait( this );
|
2016-12-19 12:53:11 +00:00
|
|
|
setupEl( this );
|
2016-11-16 19:45:10 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#linkDwell', function ( assert ) {
|
2016-11-25 10:40:33 +00:00
|
|
|
var done = assert.async(),
|
2016-11-21 11:08:06 +00:00
|
|
|
event = {},
|
2016-12-19 12:53:11 +00:00
|
|
|
dispatch = this.sandbox.spy();
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-21 11:08:06 +00:00
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
2016-12-19 12:53:11 +00:00
|
|
|
this.sandbox.stub( mw.popups.actions, 'fetch' );
|
2016-11-21 11:08:06 +00:00
|
|
|
|
2017-01-10 23:23:40 +00:00
|
|
|
// Set the state for when dispatch is called. New token is accepted
|
|
|
|
this.state.preview = {
|
|
|
|
activeToken: generateToken()
|
|
|
|
};
|
|
|
|
|
2016-12-19 12:53:11 +00:00
|
|
|
mw.popups.actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
2016-12-06 14:02:13 +00:00
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-10 23:23:40 +00:00
|
|
|
assert.deepEqual( dispatch.getCall( 0 ).args[ 0 ], {
|
2016-11-16 19:45:10 +00:00
|
|
|
type: 'LINK_DWELL',
|
2016-12-19 12:53:11 +00:00
|
|
|
el: this.el,
|
2016-11-21 11:08:06 +00:00
|
|
|
event: event,
|
2017-01-08 13:25:17 +00:00
|
|
|
token: '9876543210',
|
2016-12-06 14:02:13 +00:00
|
|
|
timestamp: mw.now()
|
|
|
|
} );
|
2016-11-16 19:45:10 +00:00
|
|
|
|
|
|
|
// Stub the state tree being updated.
|
|
|
|
this.state.preview = {
|
2016-11-25 21:22:09 +00:00
|
|
|
enabled: true,
|
2017-01-09 16:45:34 +00:00
|
|
|
activeLink: this.el,
|
|
|
|
activeToken: generateToken()
|
2016-11-16 19:45:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2016-11-25 10:40:33 +00:00
|
|
|
this.waitPromise.then( function () {
|
2016-11-16 19:45:10 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
dispatch.callCount,
|
|
|
|
2,
|
2016-12-19 12:53:11 +00:00
|
|
|
'The fetch action is dispatched after 50 ms'
|
2016-11-16 19:45:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
2016-11-25 10:40:33 +00:00
|
|
|
} );
|
|
|
|
|
2016-12-19 12:53:11 +00:00
|
|
|
// After 50 ms...
|
2016-11-25 10:40:33 +00:00
|
|
|
this.waitDeferred.resolve();
|
2016-11-16 19:45:10 +00:00
|
|
|
} );
|
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
QUnit.test( '#linkDwell doesn\'t continue when previews are disabled', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
mw.popups.actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
this.state.preview = {
|
|
|
|
enabled: false
|
|
|
|
};
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
this.waitPromise.then( function () {
|
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// After 500 ms...
|
|
|
|
this.waitDeferred.resolve();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#linkDwell doesn\'t continue if the token has changed', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
|
|
|
mw.popups.actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
this.state.preview = {
|
|
|
|
enabled: true,
|
2016-11-25 10:40:33 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
// Consider the user tabbing back and forth between two links in the time
|
|
|
|
// it takes to start fetching data via the gateway: the active link hasn't
|
|
|
|
// changed, but the active token has.
|
|
|
|
activeLink: this.el,
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
activeToken: '0123456789'
|
|
|
|
};
|
2016-11-25 21:22:09 +00:00
|
|
|
|
2017-01-09 16:45:34 +00:00
|
|
|
this.waitPromise.then( function () {
|
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
|
|
|
|
|
|
|
done();
|
2016-12-19 12:53:11 +00:00
|
|
|
} );
|
2017-01-09 16:45:34 +00:00
|
|
|
|
|
|
|
// After 500 ms...
|
|
|
|
this.waitDeferred.resolve();
|
2016-12-19 12:53:11 +00:00
|
|
|
} );
|
|
|
|
|
2017-01-10 23:23:40 +00:00
|
|
|
QUnit.test( '#linkDwell doesn\'t continue if the interaction is the same one', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
|
|
|
this.state.preview = {
|
|
|
|
activeToken: '0123456789'
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.popups.actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
|
|
|
|
|
|
|
this.waitPromise.then( function () {
|
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// After 500 ms...
|
|
|
|
this.waitDeferred.resolve();
|
|
|
|
} );
|
|
|
|
|
2016-12-19 12:53:11 +00:00
|
|
|
QUnit.module( 'ext.popups/actions#fetch', {
|
|
|
|
setup: function () {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.gatewayDeferred = $.Deferred(),
|
|
|
|
this.gatewayPromise = this.gatewayDeferred.promise();
|
2017-01-31 22:34:27 +00:00
|
|
|
this.gateway = {
|
|
|
|
getPageSummary: this.sandbox.stub().returns( this.gatewayPromise )
|
|
|
|
};
|
2016-12-19 12:53:11 +00:00
|
|
|
|
|
|
|
// Setup the mw.now stub.
|
|
|
|
that.now = 0;
|
|
|
|
|
|
|
|
this.sandbox.stub( mw, 'now', function () {
|
|
|
|
return that.now;
|
|
|
|
} );
|
|
|
|
|
|
|
|
setupWait( this );
|
|
|
|
setupEl( this );
|
|
|
|
|
|
|
|
this.dispatch = this.sandbox.spy();
|
|
|
|
|
|
|
|
// Sugar.
|
|
|
|
this.fetch = function () {
|
|
|
|
mw.popups.actions.fetch( that.gateway, that.el, that.now )( that.dispatch );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should fetch data from the gateway immediately', function ( assert ) {
|
|
|
|
this.fetch();
|
|
|
|
|
2017-01-31 22:34:27 +00:00
|
|
|
assert.ok( this.gateway.getPageSummary.calledWith( 'Foo' ) );
|
2016-12-19 12:53:11 +00:00
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.dispatch.calledWith( {
|
|
|
|
type: 'FETCH_START',
|
|
|
|
el: this.el,
|
|
|
|
title: 'Foo'
|
|
|
|
} ),
|
|
|
|
'It dispatches the FETCH_START action immediately.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should delay dispatching the FETCH_END action', function ( assert ) {
|
|
|
|
var that = this,
|
|
|
|
result = {};
|
|
|
|
|
|
|
|
this.fetch();
|
|
|
|
|
|
|
|
this.gatewayPromise.then( function () {
|
|
|
|
assert.ok(
|
|
|
|
mw.popups.wait.calledWith( 250 ),
|
|
|
|
'FETCH_END is delayed by 250 (500 - 250) ms. ' +
|
|
|
|
'If you\'ve changed FETCH_END_TARGET_DELAY, then have you spoken with #Design about changing this value?'
|
|
|
|
);
|
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
that.waitDeferred.resolve();
|
2016-12-19 12:53:11 +00:00
|
|
|
|
|
|
|
assert.ok( that.dispatch.calledWith( {
|
|
|
|
type: 'FETCH_END',
|
|
|
|
el: that.el,
|
|
|
|
result: result
|
|
|
|
} ) );
|
2016-11-25 21:22:09 +00:00
|
|
|
} );
|
2016-12-19 12:53:11 +00:00
|
|
|
|
|
|
|
// The API request took 250 ms.
|
|
|
|
this.now += 250;
|
|
|
|
this.gatewayDeferred.resolve( result );
|
2016-11-25 21:22:09 +00:00
|
|
|
} );
|
2016-11-28 12:00:07 +00:00
|
|
|
|
2016-12-19 12:53:11 +00:00
|
|
|
QUnit.test(
|
|
|
|
'it shouldn\'t delay dispatching the FETCH_END action if the API request is over the target',
|
|
|
|
function ( assert ) {
|
|
|
|
|
|
|
|
this.fetch();
|
|
|
|
|
|
|
|
this.gatewayPromise.then( function () {
|
|
|
|
assert.ok(
|
|
|
|
mw.popups.wait.calledWith( 0 ),
|
|
|
|
'FETCH_END isn\'t delayed.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
// The API request took 301 ms.
|
|
|
|
this.now += 501;
|
|
|
|
this.gatewayDeferred.resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-01-11 23:33:27 +00:00
|
|
|
QUnit.module( 'ext.popups/actions#abandon', {
|
2016-11-28 12:00:07 +00:00
|
|
|
setup: function () {
|
|
|
|
setupWait( this );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should dispatch start and end actions', function ( assert ) {
|
|
|
|
var that = this,
|
|
|
|
dispatch = that.sandbox.spy(),
|
2017-01-08 13:32:03 +00:00
|
|
|
token = '0123456789',
|
|
|
|
getState = function () {
|
|
|
|
return {
|
|
|
|
preview: {
|
|
|
|
activeToken: token
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
2016-11-28 12:00:07 +00:00
|
|
|
done = assert.async();
|
|
|
|
|
2016-12-06 14:02:13 +00:00
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
|
|
|
|
2017-01-11 23:33:27 +00:00
|
|
|
mw.popups.actions.abandon( that.el )( dispatch, getState );
|
2016-11-28 12:00:07 +00:00
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
2017-01-11 23:33:27 +00:00
|
|
|
type: 'ABANDON_START',
|
2017-01-08 13:32:03 +00:00
|
|
|
timestamp: mw.now(),
|
|
|
|
token: token
|
2016-11-28 12:00:07 +00:00
|
|
|
} ) );
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
mw.popups.wait.calledWith( 300 ),
|
|
|
|
'Have you spoken with #Design about changing this value?'
|
|
|
|
);
|
|
|
|
|
|
|
|
that.waitPromise.then( function () {
|
2017-01-08 13:32:03 +00:00
|
|
|
assert.ok(
|
|
|
|
dispatch.calledWith( {
|
2017-02-07 11:06:16 +00:00
|
|
|
type: 'ABANDON_END',
|
|
|
|
token: token
|
2017-01-08 13:32:03 +00:00
|
|
|
} ),
|
2017-01-11 23:33:27 +00:00
|
|
|
'ABANDON_* share the same token.'
|
2017-01-08 13:32:03 +00:00
|
|
|
);
|
2016-11-28 12:00:07 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// After 300 ms...
|
|
|
|
that.waitDeferred.resolve();
|
|
|
|
} );
|
|
|
|
|
2016-12-13 18:04:03 +00:00
|
|
|
QUnit.module( 'ext.popups/actions#saveSettings' );
|
|
|
|
|
|
|
|
QUnit.test( 'it should dispatch an action with previous and current enabled state', function ( assert ) {
|
|
|
|
var dispatch = this.sandbox.spy(),
|
|
|
|
getState = this.sandbox.stub().returns( {
|
|
|
|
preview: {
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
mw.popups.actions.saveSettings( /* enabled = */ true )( dispatch, getState );
|
|
|
|
|
|
|
|
assert.ok( getState.calledOnce, 'it should query the global state for the current state' );
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'SETTINGS_CHANGE',
|
|
|
|
wasEnabled: false,
|
|
|
|
enabled: true
|
|
|
|
} ), 'it should dispatch the action with the previous and next enabled state' );
|
|
|
|
} );
|
2016-11-16 19:45:10 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|