2017-02-27 16:10:50 +00:00
|
|
|
var mock = require( 'mock-require' ),
|
|
|
|
createStubUser = require( './stubs' ).createStubUser,
|
|
|
|
actions = require( '../../src/actions' ),
|
|
|
|
mw = mediaWiki;
|
|
|
|
|
|
|
|
function generateToken() {
|
|
|
|
return '9876543210';
|
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions' );
|
|
|
|
|
|
|
|
QUnit.test( '#boot', function ( assert ) {
|
|
|
|
var config = new Map(), /* global Map */
|
|
|
|
stubUser = createStubUser( /* isAnon = */ true ),
|
|
|
|
stubUserSettings,
|
|
|
|
action;
|
|
|
|
|
|
|
|
config.set( 'wgTitle', 'Foo' );
|
|
|
|
config.set( 'wgNamespaceNumber', 1 );
|
|
|
|
config.set( 'wgArticleId', 2 );
|
|
|
|
config.set( 'wgUserEditCount', 3 );
|
|
|
|
config.set( 'wgPopupsConflictsWithNavPopupGadget', true );
|
|
|
|
|
|
|
|
stubUserSettings = {
|
|
|
|
getPreviewCount: function () {
|
|
|
|
return 22;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.expect( 1 );
|
|
|
|
|
|
|
|
action = actions.boot(
|
|
|
|
false,
|
|
|
|
stubUser,
|
|
|
|
stubUserSettings,
|
|
|
|
generateToken,
|
|
|
|
config
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
action,
|
|
|
|
{
|
|
|
|
type: 'BOOT',
|
|
|
|
isEnabled: false,
|
|
|
|
isNavPopupsEnabled: true,
|
|
|
|
sessionToken: '0123456789',
|
|
|
|
pageToken: '9876543210',
|
|
|
|
page: {
|
|
|
|
title: 'Foo',
|
|
|
|
namespaceID: 1,
|
|
|
|
id: 2
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
isAnon: true,
|
|
|
|
editCount: 3,
|
|
|
|
previewCount: 22
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stubs `wait.js` and adds the deferred and its promise as properties
|
|
|
|
* of the module.
|
|
|
|
*
|
|
|
|
* @param {Object} module
|
|
|
|
*/
|
|
|
|
function setupWait( module ) {
|
2017-03-26 21:05:32 +00:00
|
|
|
module.waitDeferreds = [];
|
|
|
|
module.waitPromises = [];
|
|
|
|
|
|
|
|
module.wait = module.sandbox.spy( function () {
|
|
|
|
var deferred = $.Deferred(),
|
|
|
|
promise = deferred.promise();
|
|
|
|
|
|
|
|
module.waitDeferreds.push( deferred );
|
|
|
|
module.waitPromises.push( promise );
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
} );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
mock( '../../src/wait', module.wait );
|
|
|
|
// Re-require actions so that it uses the mocked wait module
|
|
|
|
actions = mock.reRequire( '../../src/actions' );
|
|
|
|
}
|
|
|
|
|
|
|
|
function teardownWait() {
|
|
|
|
mock.stop( '../../src/wait' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#linkDwell @integration', {
|
|
|
|
setup: function () {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
preview: {}
|
|
|
|
};
|
|
|
|
this.getState = function () {
|
|
|
|
return that.state;
|
|
|
|
};
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// The worst-case implementation of mw.now.
|
2017-02-27 16:10:50 +00:00
|
|
|
mw.now = function () { return Date.now(); };
|
|
|
|
|
|
|
|
setupWait( this );
|
|
|
|
setupEl( this );
|
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
teardownWait();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#linkDwell', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
|
|
|
this.sandbox.stub( actions, 'fetch' );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// Stub the state tree being updated by the LINK_DWELL action.
|
2017-02-27 16:10:50 +00:00
|
|
|
this.state.preview = {
|
|
|
|
activeToken: generateToken()
|
|
|
|
};
|
|
|
|
|
|
|
|
actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepEqual( dispatch.getCall( 0 ).args[ 0 ], {
|
|
|
|
type: 'LINK_DWELL',
|
|
|
|
el: this.el,
|
|
|
|
event: event,
|
|
|
|
token: '9876543210',
|
|
|
|
timestamp: mw.now()
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Stub the state tree being updated.
|
|
|
|
this.state.preview = {
|
|
|
|
enabled: true,
|
|
|
|
activeLink: this.el,
|
|
|
|
activeToken: generateToken()
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
this.waitPromises[ 0 ].then( function () {
|
2017-02-27 16:10:50 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
dispatch.callCount,
|
|
|
|
2,
|
2017-03-26 21:05:32 +00:00
|
|
|
'The fetch action is dispatched after FETCH_COMPLETE milliseconds.'
|
2017-02-27 16:10:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// After FETCH_START_DELAY milliseconds...
|
|
|
|
this.waitDeferreds[ 0 ].resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#linkDwell doesn\'t continue when previews are disabled', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// Stub the state tree being updated by the LINK_DWELL action.
|
|
|
|
this.state.preview = {
|
|
|
|
enabled: false,
|
|
|
|
activeLink: this.el,
|
|
|
|
activeToken: generateToken()
|
|
|
|
};
|
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
assert.strictEqual( this.wait.callCount, 1 );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
this.waitPromises[ 0 ].then( function () {
|
2017-02-27 16:10:50 +00:00
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// After FETCH_START_DELAY milliseconds...
|
|
|
|
this.waitDeferreds[ 0 ].resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#linkDwell doesn\'t continue if the token has changed', function ( assert ) {
|
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// Stub the state tree being updated by a LINK_DWELL action.
|
|
|
|
this.state.preview = {
|
|
|
|
enabled: true,
|
|
|
|
activeLink: this.el,
|
|
|
|
activeToken: generateToken()
|
|
|
|
};
|
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// Stub the state tree being updated by another LINK_DWELL action.
|
2017-02-27 16:10:50 +00:00
|
|
|
this.state.preview = {
|
|
|
|
enabled: true,
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
activeToken: 'banana'
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
this.waitPromises[ 0 ].then( function () {
|
2017-02-27 16:10:50 +00:00
|
|
|
assert.strictEqual( dispatch.callCount, 1 );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// After FETCH_START_DELAY milliseconds...
|
|
|
|
this.waitDeferreds[ 0 ].resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
QUnit.test( '#linkDwell dispatches the fetch action', function ( assert ) {
|
2017-02-27 16:10:50 +00:00
|
|
|
var done = assert.async(),
|
|
|
|
event = {},
|
|
|
|
dispatch = this.sandbox.spy();
|
|
|
|
|
|
|
|
this.state.preview = {
|
2017-03-26 21:05:32 +00:00
|
|
|
enabled: true,
|
|
|
|
activeToken: generateToken()
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
actions.linkDwell( this.el, event, /* gateway = */ null, generateToken )(
|
|
|
|
dispatch,
|
|
|
|
this.getState
|
|
|
|
);
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
this.waitPromises[ 0 ].then( function () {
|
|
|
|
assert.strictEqual( dispatch.callCount, 2 );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// After FETCH_START_DELAY milliseconds...
|
|
|
|
this.waitDeferreds[ 0 ].resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#fetch', {
|
|
|
|
setup: function () {
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
// Setup the mw.now stub before actions is re-required in setupWait
|
2017-04-07 11:34:21 +00:00
|
|
|
this.now = 0;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
this.sandbox.stub( mw, 'now', function () {
|
|
|
|
return that.now;
|
|
|
|
} );
|
|
|
|
|
|
|
|
setupWait( this );
|
|
|
|
setupEl( this );
|
|
|
|
|
|
|
|
this.gatewayDeferred = $.Deferred(),
|
|
|
|
this.gatewayPromise = this.gatewayDeferred.promise();
|
|
|
|
this.gateway = {
|
|
|
|
getPageSummary: this.sandbox.stub().returns( this.gatewayPromise )
|
|
|
|
};
|
|
|
|
|
|
|
|
this.dispatch = this.sandbox.spy();
|
|
|
|
|
2017-04-07 11:34:21 +00:00
|
|
|
this.token = '1234567890';
|
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
// Sugar.
|
|
|
|
this.fetch = function () {
|
2017-04-07 11:34:21 +00:00
|
|
|
actions.fetch( that.gateway, that.el, that.token )( that.dispatch );
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
teardownWait();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should fetch data from the gateway immediately', function ( assert ) {
|
|
|
|
this.fetch();
|
|
|
|
|
|
|
|
assert.ok( this.gateway.getPageSummary.calledWith( 'Foo' ) );
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.dispatch.calledWith( {
|
|
|
|
type: 'FETCH_START',
|
|
|
|
el: this.el,
|
2017-03-07 00:27:38 +00:00
|
|
|
title: 'Foo',
|
|
|
|
timestamp: this.now
|
2017-02-27 16:10:50 +00:00
|
|
|
} ),
|
|
|
|
'It dispatches the FETCH_START action immediately.'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
QUnit.test( 'it should dispatch the FETCH_END action when the API request ends', function ( assert ) {
|
2017-02-27 16:10:50 +00:00
|
|
|
var that = this,
|
2017-03-26 23:18:29 +00:00
|
|
|
done = assert.async();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
this.fetch();
|
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
this.now += 115;
|
|
|
|
this.gatewayDeferred.resolve( {} );
|
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
this.gatewayPromise.then( function () {
|
2017-03-26 21:05:32 +00:00
|
|
|
assert.deepEqual(
|
2017-04-25 12:12:36 +00:00
|
|
|
that.dispatch.getCall( 1 ).args[ 0 ],
|
2017-03-26 21:05:32 +00:00
|
|
|
{
|
|
|
|
type: 'FETCH_END',
|
|
|
|
el: that.el,
|
2017-03-26 23:18:29 +00:00
|
|
|
timestamp: 115
|
|
|
|
}
|
2017-02-27 16:10:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
2017-03-26 23:18:29 +00:00
|
|
|
} );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
QUnit.test( 'it should delay dispatching the FETCH_COMPLETE action', function ( assert ) {
|
|
|
|
var whenDeferred = $.Deferred(),
|
|
|
|
whenSpy,
|
|
|
|
args,
|
|
|
|
result = {},
|
2017-04-07 11:34:21 +00:00
|
|
|
that = this,
|
|
|
|
done = assert.async();
|
2017-04-25 12:12:36 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
whenSpy = this.sandbox.stub( $, 'when' )
|
|
|
|
.returns( whenDeferred.promise() );
|
2017-03-26 19:13:56 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
this.fetch();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
this.wait.getCall( 0 ).args[ 0 ],
|
2017-04-06 20:12:19 +00:00
|
|
|
350,
|
|
|
|
'It waits for FETCH_COMPLETE_TARGET_DELAY - FETCH_START_DELAY milliseconds.'
|
2017-03-26 23:18:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
args = whenSpy.getCall( 0 ).args;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
// This assertion is disabled due to $.Promise#then and #fail returning a new
|
|
|
|
// instance of $.Promise.
|
2017-04-25 12:12:36 +00:00
|
|
|
// assert.strictEqual( args[ 0 ], this.gatewayPromise );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
assert.strictEqual( args[ 1 ], this.waitPromises[ 0 ] );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
// ---
|
|
|
|
this.now += 500;
|
2017-04-07 11:34:21 +00:00
|
|
|
whenDeferred.resolve( result );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2017-03-26 23:18:29 +00:00
|
|
|
whenDeferred.then( function () {
|
2017-04-07 11:34:21 +00:00
|
|
|
|
|
|
|
// Ensure the following assertions are made after all callbacks have been
|
|
|
|
// executed. Use setTimeout( _, 0 ) since it's not critical that these
|
|
|
|
// assertions are run before I/O is processed, i.e. we don't require
|
|
|
|
// process.nextTick.
|
|
|
|
setTimeout( function () {
|
|
|
|
assert.deepEqual(
|
|
|
|
that.dispatch.getCall( 1 ).args[ 0 ],
|
|
|
|
{
|
|
|
|
type: 'FETCH_COMPLETE',
|
|
|
|
el: that.el,
|
|
|
|
result: result,
|
|
|
|
timestamp: 500,
|
|
|
|
token: that.token
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}, 0 );
|
2017-03-26 23:18:29 +00:00
|
|
|
} );
|
|
|
|
} );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#abandon', {
|
|
|
|
setup: function () {
|
|
|
|
setupWait( this );
|
|
|
|
},
|
|
|
|
teardown: function () {
|
|
|
|
teardownWait();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it should dispatch start and end actions', function ( assert ) {
|
|
|
|
var dispatch = this.sandbox.spy(),
|
|
|
|
token = '0123456789',
|
|
|
|
getState = function () {
|
|
|
|
return {
|
|
|
|
preview: {
|
|
|
|
activeToken: token
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
|
|
|
|
|
|
|
actions.abandon( this.el )( dispatch, getState );
|
|
|
|
|
|
|
|
assert.ok( dispatch.calledWith( {
|
|
|
|
type: 'ABANDON_START',
|
|
|
|
timestamp: mw.now(),
|
|
|
|
token: token
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.wait.calledWith( 300 ),
|
|
|
|
'Have you spoken with #Design about changing this value?'
|
|
|
|
);
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
this.waitPromises[ 0 ].then( function () {
|
2017-02-27 16:10:50 +00:00
|
|
|
assert.ok(
|
|
|
|
dispatch.calledWith( {
|
|
|
|
type: 'ABANDON_END',
|
|
|
|
token: token
|
|
|
|
} ),
|
|
|
|
'ABANDON_* share the same token.'
|
|
|
|
);
|
|
|
|
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
|
2017-03-26 21:05:32 +00:00
|
|
|
// After ABANDON_END_DELAY milliseconds...
|
|
|
|
this.waitDeferreds[ 0 ].resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
2017-04-18 18:43:16 +00:00
|
|
|
QUnit.test( 'it shouldn\'t dispatch under certain conditions', function ( assert ) {
|
|
|
|
var dispatch = this.sandbox.spy(),
|
|
|
|
getState = function () {
|
|
|
|
return {
|
|
|
|
preview: {
|
|
|
|
activeToken: undefined
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
actions.abandon( this.el )( dispatch, getState );
|
|
|
|
|
|
|
|
assert.ok( dispatch.notCalled );
|
|
|
|
} );
|
|
|
|
|
2017-02-27 16:10:50 +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
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
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' );
|
|
|
|
} );
|
2017-04-10 17:28:24 +00:00
|
|
|
|
|
|
|
QUnit.module( 'ext.popups/actions#previewShow' );
|
|
|
|
|
|
|
|
QUnit.test( 'it should dispatch the PREVIEW_SHOW action', function ( assert ) {
|
|
|
|
var token = '1234567890';
|
|
|
|
|
|
|
|
this.sandbox.stub( mw, 'now' ).returns( new Date() );
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
actions.previewShow( token ),
|
|
|
|
{
|
|
|
|
type: 'PREVIEW_SHOW',
|
|
|
|
token: token,
|
|
|
|
timestamp: mw.now()
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|