2017-07-28 17:32:46 +00:00
|
|
|
import * as Redux from 'redux';
|
|
|
|
import * as ReduxThunk from 'redux-thunk';
|
|
|
|
import * as WaitModule from '../../src/wait';
|
|
|
|
import * as stubs from './stubs';
|
|
|
|
import * as actions from '../../src/actions';
|
|
|
|
import reducers from '../../src/reducers';
|
|
|
|
import registerChangeListener from '../../src/changeListener';
|
2019-03-11 13:14:17 +00:00
|
|
|
import { previewTypes } from '../../src/preview/model';
|
2017-07-28 17:32:46 +00:00
|
|
|
|
2018-03-19 19:39:41 +00:00
|
|
|
const mw = mediaWiki,
|
2017-07-28 17:32:46 +00:00
|
|
|
$ = jQuery,
|
2018-01-11 15:44:26 +00:00
|
|
|
/**
|
2019-01-23 16:50:19 +00:00
|
|
|
* Whether Gateway#fetchPreviewForTitle is resolved or rejected.
|
2018-01-11 15:44:26 +00:00
|
|
|
* @enum {number}
|
|
|
|
*/
|
|
|
|
FETCH_RESOLUTION = { RESOLVE: 0, REJECT: 1 };
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-12-13 03:09:26 +00:00
|
|
|
function identity( x ) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
function constant( x ) {
|
|
|
|
return () => x;
|
|
|
|
}
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Integration tests for actions and state of the preview part of the system.
|
|
|
|
* Follows a diagram of the interactions considered valid, which will be
|
|
|
|
* used as a basis for the following tests:
|
|
|
|
*
|
|
|
|
|
|
|
|
+--------+
|
|
|
|
|INACTIVE+-----------------------+
|
|
|
|
+---+----+ |
|
|
|
|
^ |
|
|
|
|
| link or preview |
|
|
|
|
| abandon end |
|
|
|
|
| link_dwell|
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
| |
|
|
|
|
+---|----------------------------|---+
|
|
|
|
| | ACTIVE | |
|
|
|
|
+---|----------------------------|---+
|
|
|
|
| + v |
|
|
|
|
| OFF_LINK ON_LINK | Inside ACTIVE, or out
|
|
|
|
| + ^ link or preview | ^ | of it, only actions with
|
|
|
|
| | | abandon start | | | that same active link are
|
|
|
|
| | +----------------------+ | | valid. Others are ignored.
|
|
|
|
| | | |
|
|
|
|
| +----------------------------+ |
|
|
|
|
| preview or same link dwell |
|
|
|
|
| |
|
|
|
|
| NO_DATA +-------> DATA |
|
|
|
|
| fetch end |
|
|
|
|
+------------------------------------+
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups preview @integration', {
|
2018-03-14 22:04:59 +00:00
|
|
|
beforeEach() {
|
2017-03-26 21:05:32 +00:00
|
|
|
// The worst-case implementation of mw.now.
|
2018-03-14 23:50:09 +00:00
|
|
|
mw.now = () => Date.now();
|
2017-03-26 21:05:32 +00:00
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.resetWait = () => {
|
|
|
|
this.waitDeferred = $.Deferred();
|
|
|
|
this.waitPromise = this.waitDeferred.promise();
|
|
|
|
this.wait.returns( this.waitPromise );
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2017-07-28 17:32:46 +00:00
|
|
|
this.wait = this.sandbox.stub( WaitModule, 'default' );
|
2017-02-27 16:10:50 +00:00
|
|
|
this.resetWait();
|
|
|
|
|
|
|
|
this.store = Redux.createStore(
|
|
|
|
Redux.combineReducers( reducers ),
|
|
|
|
Redux.compose( Redux.applyMiddleware( ReduxThunk.default ) )
|
|
|
|
);
|
|
|
|
|
|
|
|
this.actions = Redux.bindActionCreators(
|
|
|
|
actions,
|
|
|
|
this.store.dispatch
|
|
|
|
);
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.registerChangeListener = ( fn ) => {
|
|
|
|
return registerChangeListener( this.store, fn );
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2017-06-08 15:23:44 +00:00
|
|
|
this.title = stubs.createStubTitle( 1, 'Foo' );
|
2017-05-09 18:41:23 +00:00
|
|
|
|
2019-01-17 22:17:27 +00:00
|
|
|
this.el = $( '<a>' ).attr( 'href', '/wiki/Foo' );
|
2017-05-09 18:41:23 +00:00
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
this.actions.boot(
|
|
|
|
/* isEnabled: */
|
|
|
|
constant( true ),
|
|
|
|
/* user */
|
2018-08-21 23:43:33 +00:00
|
|
|
stubs.createStubUser( true ),
|
2017-02-27 16:10:50 +00:00
|
|
|
/* userSettings: */
|
|
|
|
{ getPreviewCount: constant( 1 ) },
|
|
|
|
/* config: */
|
|
|
|
{ get: identity }
|
|
|
|
);
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.dwell = (
|
2018-01-18 18:48:16 +00:00
|
|
|
title, el, ev, fetchResponse, resolution = FETCH_RESOLUTION.RESOLVE
|
2018-02-16 12:52:56 +00:00
|
|
|
) => {
|
|
|
|
this.resetWait();
|
|
|
|
return this.actions.linkDwell( title, el, ev, {
|
2019-01-23 16:50:19 +00:00
|
|
|
fetchPreviewForTitle() {
|
2018-03-19 19:39:41 +00:00
|
|
|
const method = resolution === FETCH_RESOLUTION.RESOLVE ?
|
2018-01-18 18:48:16 +00:00
|
|
|
'resolve' : 'reject';
|
Update: cancel unused HTTP requests in flight
Whenever an HTTP request sequence is started, i.e. wait for the fetch
start time, issue a network request, and return the result, abort the
process if the results are known to no longer be needed. This occurs
when a user has dwelt upon one link and then abandoned it either during
the fetch start wait time or during the fetch network request itself.
This change is accomplished by preserving the pending promises in two
actions, LINK_DWELL and FETCH_START, and whenever the ABANDON_START
action is issued, it now aborts any previously pending XHR-like promise,
called a "AbortPromise" which is just a thenable with an abort() method.
There is a similar concept in Core:
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/ecc812f06e7dff587b3f31dc18189adbf4616351/resources/src/mediawiki.api/index.js.
Aborting pending requests has big implications for client and server
logging as requests are quickly canceled, especially on slower
connections. These differences can be observed on the network tab of
DevTools and the log in Redux DevTools.
Consider, for instance, the scenario of dwelling upon and quickly
abandoning a single link prior to this patch:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_END STATSV_LOGGED ABANDON_END EVENT_LOGGED FETCH_COMPLETE
And after this patch when the fetch timer is canceled (prior to an
actual network request):
BOOT EVENT_LOGGED LINK_DWELL ABANDON_START ABANDON_END EVENT_LOGGED
In the above sequence, FETCH_* and STATSV_LOGGED actions never occur.
And after this patch when the network request itself is canceled:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_FAILED STATSV_LOGGED FETCH_COMPLETE ABANDON_END EVENT_LOGGED
FETCH_FAILED occurs intentionally, STATSV_LOGGED and FETCH_COMPLETE
still happen even though the fetch didn't complete successfully, and
FETCH_END doesn't.
Additionally, since less data is transmitted, it's possible that the
timing and success rate of logging will improve on low bandwidth
connections.
Also, this patch tries to revise the JSDocs where possible to support
type checking and fix a call to the missing assert.fail() function in
changeListener.test.js.
Bug: T197700
Change-Id: I9a73b3086fc8fb0edd897a347b5497d5362e20ef
2018-06-25 13:26:11 +00:00
|
|
|
const abort = {
|
|
|
|
abort() {}
|
|
|
|
};
|
|
|
|
return $.Deferred()[ method ]( fetchResponse ).promise( abort );
|
2017-02-27 16:10:50 +00:00
|
|
|
}
|
2019-03-11 13:14:17 +00:00
|
|
|
}, () => 'pagetoken', previewTypes.TYPE_PAGE );
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.dwellAndShowPreview = (
|
2018-01-18 18:48:16 +00:00
|
|
|
title, el, ev, fetchResponse, reject = FETCH_RESOLUTION.RESOLVE
|
2018-02-16 12:52:56 +00:00
|
|
|
) => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const dwelled = this.dwell( title, el, ev, fetchResponse, reject );
|
2018-02-16 12:52:56 +00:00
|
|
|
// Resolve the wait timeout for the linkDwell and the fetch action
|
|
|
|
this.waitDeferred.resolve();
|
|
|
|
return dwelled;
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.abandon = () => {
|
|
|
|
this.resetWait();
|
|
|
|
return this.actions.abandon();
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.abandonAndWait = () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const abandoned = this.abandon();
|
2018-02-16 12:52:56 +00:00
|
|
|
this.waitDeferred.resolve();
|
|
|
|
return abandoned;
|
2017-02-27 16:10:50 +00:00
|
|
|
};
|
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
this.dwellAndPreviewDwell = ( title, el, ev, res ) => {
|
|
|
|
return this.dwellAndShowPreview( title, el, ev, res ).then( () => {
|
2017-02-27 16:10:50 +00:00
|
|
|
// Get out of the link, and before the delay ends...
|
2018-03-19 19:39:41 +00:00
|
|
|
const abandonPromise = this.abandon(),
|
2018-02-16 12:52:56 +00:00
|
|
|
abandonWaitDeferred = this.waitDeferred;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
// Dwell over the preview
|
2018-02-16 12:52:56 +00:00
|
|
|
this.actions.previewDwell( el );
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
// Then the abandon delay finishes
|
2018-02-16 12:52:56 +00:00
|
|
|
abandonWaitDeferred.resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
return abandonPromise;
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'it boots in INACTIVE state', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = this.store.getState();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-05-08 19:48:17 +00:00
|
|
|
state.preview.activeLink,
|
|
|
|
undefined,
|
|
|
|
'The initial active link is undefined.'
|
|
|
|
);
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-05-08 19:48:17 +00:00
|
|
|
state.preview.linkInteractionToken,
|
|
|
|
undefined,
|
|
|
|
'The initial token is undefined.'
|
|
|
|
);
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'in INACTIVE state, a link dwell switches it to ACTIVE', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const gateway = {
|
2019-01-23 16:50:19 +00:00
|
|
|
fetchPreviewForTitle() {
|
2018-03-19 19:39:41 +00:00
|
|
|
$.Deferred().promise();
|
|
|
|
}
|
|
|
|
};
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
this.actions.linkDwell(
|
2017-06-08 15:23:44 +00:00
|
|
|
this.title, this.el, 'event',
|
2017-02-27 16:10:50 +00:00
|
|
|
gateway,
|
|
|
|
constant( 'pagetoken' )
|
|
|
|
);
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = this.store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual( state.preview.activeLink, this.el, 'It has an active link' );
|
|
|
|
assert.strictEqual( state.preview.shouldShow, false, 'Initializes with NO_DATA' );
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'in ACTIVE state, fetch end switches it to DATA', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const store = this.store,
|
2017-05-09 18:41:23 +00:00
|
|
|
el = this.el;
|
|
|
|
|
2018-01-18 18:48:16 +00:00
|
|
|
return this.dwellAndShowPreview( this.title, el, 'event', 42 )
|
2018-03-14 23:50:09 +00:00
|
|
|
.then( () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-05-08 19:48:17 +00:00
|
|
|
state.preview.activeLink,
|
|
|
|
el,
|
|
|
|
'The active link is passed.'
|
|
|
|
);
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-01-18 18:48:16 +00:00
|
|
|
state.preview.shouldShow, true,
|
|
|
|
'Should show when data has been fetched' );
|
|
|
|
} );
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
2018-01-11 15:44:26 +00:00
|
|
|
QUnit.test( 'in ACTIVE state, fetch fail switches it to DATA', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const store = this.store,
|
2018-01-11 15:44:26 +00:00
|
|
|
el = this.el;
|
|
|
|
|
2018-01-18 18:48:16 +00:00
|
|
|
return this.dwellAndShowPreview(
|
|
|
|
this.title, el, 'event', 42, FETCH_RESOLUTION.REJECT
|
2018-03-14 23:50:09 +00:00
|
|
|
).then( () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual( state.preview.activeLink, el, 'The active link is passed.' );
|
|
|
|
assert.strictEqual( state.preview.shouldShow, true,
|
2018-01-18 18:48:16 +00:00
|
|
|
'Should show when data couldn\'t be fetched' );
|
2018-01-11 15:44:26 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-02-27 16:10:50 +00:00
|
|
|
QUnit.test( 'in ACTIVE state, abandon start, and then end, switch it to INACTIVE', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const el = this.el;
|
2017-05-09 18:41:23 +00:00
|
|
|
|
2018-01-18 18:48:16 +00:00
|
|
|
return this.dwellAndShowPreview( this.title, el, 'event', 42 )
|
2018-02-16 12:52:56 +00:00
|
|
|
.then( () => {
|
|
|
|
return this.abandonAndWait( el );
|
|
|
|
} ).then( () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = this.store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual( state.preview.activeLink, undefined,
|
2018-01-18 18:48:16 +00:00
|
|
|
'After abandoning, preview is back to INACTIVE' );
|
|
|
|
} );
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'in ACTIVE state, abandon link, and then dwell preview, should keep it active after all delays', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const el = this.el;
|
2017-05-09 18:41:23 +00:00
|
|
|
|
2017-08-16 18:27:35 +00:00
|
|
|
return this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
|
2018-02-16 12:52:56 +00:00
|
|
|
.then( () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = this.store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-05-08 19:48:17 +00:00
|
|
|
state.preview.activeLink,
|
|
|
|
el,
|
|
|
|
'The active link is passed.'
|
|
|
|
);
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'in ACTIVE state, abandon link, hover preview, back to link, should keep it active after all delays', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const el = this.el;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
// Dwell link, abandon it & hover preview
|
2017-08-16 18:27:35 +00:00
|
|
|
return this.dwellAndPreviewDwell( this.title, el, 'event', 42 )
|
2018-02-16 12:52:56 +00:00
|
|
|
.then( () => {
|
2017-02-27 16:10:50 +00:00
|
|
|
// Start abandoning the preview
|
2018-03-19 19:39:41 +00:00
|
|
|
const abandonedPreview = this.abandon();
|
|
|
|
const abandonWaitDeferred = this.waitDeferred;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
// Dwell back into the link, new event ('event2') is triggered
|
2018-03-19 19:39:41 +00:00
|
|
|
const dwelled = this.dwell( this.title, el, 'event2', 42 );
|
|
|
|
const dwellWaitDeferred = this.waitDeferred;
|
2017-02-27 16:10:50 +00:00
|
|
|
|
|
|
|
// Preview abandon happens next, before the fetch
|
2018-02-16 12:52:56 +00:00
|
|
|
abandonWaitDeferred.resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
// Then dwell wait & fetch happens
|
|
|
|
dwellWaitDeferred.resolve();
|
2017-02-27 16:10:50 +00:00
|
|
|
|
2018-02-16 12:52:56 +00:00
|
|
|
return $.when( abandonedPreview, dwelled );
|
2017-02-27 16:10:50 +00:00
|
|
|
} )
|
2018-02-16 12:52:56 +00:00
|
|
|
.then( () => {
|
2018-03-19 19:39:41 +00:00
|
|
|
const state = this.store.getState();
|
2018-05-20 12:32:51 +00:00
|
|
|
assert.strictEqual(
|
2018-05-08 19:48:17 +00:00
|
|
|
state.preview.activeLink,
|
|
|
|
el,
|
|
|
|
'The active link is passed.'
|
|
|
|
);
|
2017-02-27 16:10:50 +00:00
|
|
|
} );
|
|
|
|
} );
|