mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Popups
synced 2024-11-19 21:35:59 +00:00
e4719c4918
Action creator changes: * Make the linkAbandon action creator asynchronous by splitting it into two distinct actions, LINK_ABANDON_START and _END, the latter of which is dispatched after a 300 ms delay. * Introduce the previewDwell and previewAbandon action creators. The latter is an asynchronous action that mirrors the linkAbandon action. Reducer changes: * Make the LINK_DWELL, LINK_ABANDON_END, and PREVIEW_ABANDON_END action hide a preview, if one has been shown. * Make the LINK_ABANDON_END action NOOP if: * The user has interacted with another link, or * The user is interacting with the preview. Supporting changes: * Update the mw.popups.reducers#preview and #renderer unit tests to use an empty previous state so that the tests are more resilient to modifications of the state tree. Change-Id: I2ecf575bbb59bb64772f75da9b5a29c071b46a8d
204 lines
4.1 KiB
JavaScript
204 lines
4.1 KiB
JavaScript
( function ( mw, $ ) {
|
|
|
|
QUnit.module( 'ext.popups/reducers', {
|
|
setup: function () {
|
|
this.el = $( '<a>' );
|
|
}
|
|
} );
|
|
|
|
QUnit.test( '#rootReducer', function ( assert ) {
|
|
var state = mw.popups.reducers.rootReducer( undefined, { type: '@@INIT' } );
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.deepEqual(
|
|
state,
|
|
{
|
|
preview: {
|
|
enabled: undefined,
|
|
sessionToken: undefined,
|
|
pageToken: undefined,
|
|
linkInteractionToken: undefined,
|
|
activeLink: undefined,
|
|
activeEvent: undefined,
|
|
interactionStarted: undefined,
|
|
shouldShow: false,
|
|
isUserDwelling: false
|
|
},
|
|
renderer: {
|
|
isAnimating: false,
|
|
isInteractive: false,
|
|
showSettings: false
|
|
}
|
|
},
|
|
'It should initialize the state by default'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: BOOT', function ( assert ) {
|
|
var action = {
|
|
type: 'BOOT',
|
|
isUserInCondition: true,
|
|
sessionToken: '0123456789',
|
|
pageToken: '9876543210'
|
|
};
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( {}, action ),
|
|
{
|
|
enabled: true,
|
|
sessionToken: '0123456789',
|
|
pageToken: '9876543210'
|
|
},
|
|
'It should set enabled and the session tokens.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: LINK_DWELL', function ( assert ) {
|
|
var action = {
|
|
type: 'LINK_DWELL',
|
|
el: this.el,
|
|
event: {},
|
|
interactionStarted: mw.now(),
|
|
linkInteractionToken: '0123456789'
|
|
};
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( {}, action ),
|
|
{
|
|
activeLink: action.el,
|
|
activeEvent: action.event,
|
|
interactionStarted: action.interactionStarted,
|
|
linkInteractionToken: action.linkInteractionToken,
|
|
shouldShow: false
|
|
},
|
|
'It should set active link and event as well as interaction info and hide the preview.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: LINK_ABANDON_END', function ( assert ) {
|
|
var action = {
|
|
type: 'LINK_ABANDON_END',
|
|
el: this.el
|
|
},
|
|
state = {
|
|
activeLink: this.el
|
|
};
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( state, action ),
|
|
{
|
|
activeLink: undefined,
|
|
activeEvent: undefined,
|
|
interactionStarted: undefined,
|
|
linkInteractionToken: undefined,
|
|
fetchResponse: undefined,
|
|
shouldShow: false
|
|
},
|
|
'It should hide the preview and reset the interaction info.'
|
|
);
|
|
|
|
// ---
|
|
|
|
state = {
|
|
activeLink: this.el,
|
|
isUserDwelling: true
|
|
};
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( state, action ),
|
|
state,
|
|
'It should NOOP if the user is dwelling on the preview.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: FETCH_END', function ( assert ) {
|
|
var state = {
|
|
activeLink: this.el
|
|
},
|
|
action = {
|
|
type: 'FETCH_END',
|
|
el: this.el,
|
|
result: {}
|
|
};
|
|
|
|
assert.expect( 2 );
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( state, action ),
|
|
{
|
|
activeLink: state.activeLink, // Previous state.
|
|
|
|
fetchResponse: action.result,
|
|
shouldShow: true
|
|
},
|
|
'It should store the result and signal that a preview should be rendered.'
|
|
);
|
|
|
|
// ---
|
|
|
|
state = {
|
|
activeLink: $( '<a>' )
|
|
};
|
|
action = {
|
|
type: 'FETCH_END',
|
|
el: this.el,
|
|
result: {}
|
|
};
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( state, action ),
|
|
state,
|
|
'It should NOOP if the user has interacted with another link since the request was dispatched via the gateway.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: PREVIEW_DWELL', function ( assert ) {
|
|
var action = {
|
|
type: 'PREVIEW_DWELL'
|
|
};
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( {}, action ),
|
|
{
|
|
isUserDwelling: true
|
|
},
|
|
'It should mark the preview as being dwelled on.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#preview: PREVIEW_ABANDON_START', function ( assert ) {
|
|
var action = {
|
|
type: 'PREVIEW_ABANDON_START'
|
|
};
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.preview( {}, action ),
|
|
{
|
|
isUserDwelling: false
|
|
},
|
|
'It should mark the preview having been abandoned.'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( '#renderer', function ( assert ) {
|
|
assert.expect( 1 );
|
|
|
|
assert.deepEqual(
|
|
mw.popups.reducers.renderer( {}, { type: 'PREVIEW_ANIMATING' } ),
|
|
{
|
|
isAnimating: true,
|
|
isInteractive: false,
|
|
showSettings: false
|
|
},
|
|
'It should set isAnimating to true on the PREVIEW_ANIMATING action'
|
|
);
|
|
} );
|
|
|
|
}( mediaWiki, jQuery ) );
|
|
|