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-11-14 19:37:11 +00:00
|
|
|
var isUserInCondition = function () {
|
|
|
|
return false;
|
|
|
|
},
|
2016-11-30 13:40:08 +00:00
|
|
|
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,
|
|
|
|
wgUserEditCount: 3
|
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(
|
|
|
|
isUserInCondition,
|
|
|
|
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',
|
|
|
|
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: {
|
|
|
|
isInCondition: false,
|
|
|
|
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-11-16 19:45:10 +00:00
|
|
|
QUnit.module( 'ext.popups/actions#linkDwell @integration', {
|
|
|
|
setup: function () {
|
|
|
|
var that = this;
|
|
|
|
|
2016-11-30 12:01:45 +00:00
|
|
|
this.el = $( '<a>' )
|
2016-11-16 19:45:10 +00:00
|
|
|
.data( 'page-previews-title', 'Foo' )
|
|
|
|
.eq( 0 );
|
|
|
|
|
2016-11-30 12:01:45 +00:00
|
|
|
this.state = {};
|
|
|
|
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-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-11-16 19:45:10 +00:00
|
|
|
dispatch = this.sandbox.spy(),
|
|
|
|
gatewayDeferred = $.Deferred(),
|
|
|
|
gateway = function () {
|
|
|
|
return gatewayDeferred;
|
2016-11-25 10:40:33 +00:00
|
|
|
},
|
2016-11-25 12:42:02 +00:00
|
|
|
el = this.el,
|
2016-11-25 10:40:33 +00:00
|
|
|
fetchThunk,
|
|
|
|
result = {};
|
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-06 14:02:13 +00:00
|
|
|
mw.popups.actions.linkDwell( el, event, gateway, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-12-06 14:02:13 +00:00
|
|
|
assert.deepEqual( dispatch.getCall( 0 ).args[0], {
|
2016-11-16 19:45:10 +00:00
|
|
|
type: 'LINK_DWELL',
|
2016-11-25 12:42:02 +00:00
|
|
|
el: el,
|
2016-11-21 11:08:06 +00:00
|
|
|
event: event,
|
2016-12-06 14:02:13 +00:00
|
|
|
interactionToken: '9876543210',
|
|
|
|
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,
|
2016-11-25 12:42:02 +00:00
|
|
|
activeLink: el
|
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,
|
|
|
|
'The fetch action is dispatched after 500 ms'
|
|
|
|
);
|
|
|
|
|
|
|
|
fetchThunk = dispatch.secondCall.args[0];
|
|
|
|
fetchThunk( dispatch );
|
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'FETCH_START',
|
2016-11-25 12:42:02 +00:00
|
|
|
el: el,
|
2016-11-16 19:45:10 +00:00
|
|
|
title: 'Foo'
|
|
|
|
} ) );
|
|
|
|
|
2016-11-25 12:42:02 +00:00
|
|
|
// ---
|
|
|
|
|
2016-11-16 19:45:10 +00:00
|
|
|
gatewayDeferred.resolve( result );
|
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'FETCH_END',
|
2016-11-25 12:42:02 +00:00
|
|
|
el: el,
|
2016-11-16 19:45:10 +00:00
|
|
|
result: result
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
done();
|
2016-11-25 10:40:33 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// After 500 ms...
|
|
|
|
this.waitDeferred.resolve();
|
2016-11-16 19:45:10 +00:00
|
|
|
} );
|
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
QUnit.test( '#linkDwell doesn\'t dispatch under certain conditions', function ( assert ) {
|
|
|
|
var cases,
|
|
|
|
done,
|
2016-12-06 14:02:13 +00:00
|
|
|
that = this,
|
|
|
|
event = {};
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
enabled: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled: true,
|
|
|
|
activeLink: undefined // Any value other than this.el.
|
|
|
|
}
|
|
|
|
];
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
done = assert.async( cases.length );
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
$.each( cases, function ( testCase ) {
|
|
|
|
var dispatch = that.sandbox.spy();
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-12-06 14:02:13 +00:00
|
|
|
mw.popups.actions.linkDwell( that.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
that.getState
|
|
|
|
);
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
that.state.preview = testCase;
|
2016-11-25 10:40:33 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
that.waitPromise.then( function () {
|
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
2016-11-16 19:45:10 +00:00
|
|
|
|
2016-11-25 21:22:09 +00:00
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// After 500 ms...
|
|
|
|
that.waitDeferred.resolve();
|
|
|
|
} );
|
|
|
|
} );
|
2016-11-28 12:00:07 +00:00
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#linkAbandon', {
|
|
|
|
setup: function () {
|
|
|
|
setupWait( this );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should dispatch start and end actions', function ( assert ) {
|
|
|
|
var that = this,
|
|
|
|
dispatch = that.sandbox.spy(),
|
|
|
|
done = assert.async();
|
|
|
|
|
2016-12-06 14:02:13 +00:00
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
|
|
|
|
2016-11-28 12:00:07 +00:00
|
|
|
mw.popups.actions.linkAbandon( that.el )( dispatch );
|
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'LINK_ABANDON_START',
|
2016-12-06 14:02:13 +00:00
|
|
|
el: that.el,
|
|
|
|
timestamp: mw.now()
|
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 () {
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'LINK_ABANDON_END',
|
|
|
|
el: that.el
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
|
|
|
// After 300 ms...
|
|
|
|
that.waitDeferred.resolve();
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#previewAbandon', function ( assert ) {
|
|
|
|
var that = this,
|
|
|
|
dispatch = that.sandbox.spy(),
|
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
mw.popups.actions.previewAbandon()( dispatch );
|
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'PREVIEW_ABANDON_START'
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
mw.popups.wait.calledWith( 300 ),
|
|
|
|
'Have you spoken with #Design about changing this value?'
|
|
|
|
);
|
|
|
|
|
|
|
|
that.waitPromise.then( function () {
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'PREVIEW_ABANDON_END'
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
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 ) );
|