2018-06-27 21:01:51 +00:00
|
|
|
|
import getPageviewTracker, { getSendBeacon, limitByEncodedURILength } from '../../src/getPageviewTracker';
|
2018-03-27 19:42:01 +00:00
|
|
|
|
|
|
|
|
|
QUnit.module( 'ext.popups#getPageviewTracker', {
|
|
|
|
|
beforeEach: function () {
|
|
|
|
|
this.makeBeaconUrl = this.sandbox.stub();
|
|
|
|
|
this.prepare = this.sandbox.stub();
|
|
|
|
|
this.trackerGetter = () => ( { makeBeaconUrl: this.makeBeaconUrl,
|
|
|
|
|
prepare: this.prepare } );
|
2019-02-19 13:09:52 +00:00
|
|
|
|
|
2018-03-27 19:42:01 +00:00
|
|
|
|
this.loader = () => Promise.resolve();
|
2019-02-19 13:09:52 +00:00
|
|
|
|
|
2018-04-10 14:56:07 +00:00
|
|
|
|
this.Title = {
|
|
|
|
|
newFromText: this.sandbox.stub()
|
|
|
|
|
};
|
2019-10-17 08:51:49 +00:00
|
|
|
|
mw.Title = this.Title;
|
2018-04-10 14:56:07 +00:00
|
|
|
|
},
|
|
|
|
|
afterEach() {
|
2019-10-17 08:51:49 +00:00
|
|
|
|
mw.Title = null;
|
2018-03-27 19:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
const enabledConfig = {
|
|
|
|
|
get: () => true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QUnit.test( 'getPageviewTracker', function ( assert ) {
|
|
|
|
|
const loader = this.sandbox.stub();
|
|
|
|
|
const sendBeacon = this.sandbox.stub();
|
2018-04-10 14:56:07 +00:00
|
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
|
const data = {
|
|
|
|
|
page_title: 'Test title',
|
|
|
|
|
source_title: 'Source title',
|
|
|
|
|
page_namespace: 1,
|
|
|
|
|
source_url: 'http://some/url'
|
|
|
|
|
};
|
|
|
|
|
const eventData = {
|
|
|
|
|
page_title: 'Test_title',
|
|
|
|
|
source_title: 'Source_title',
|
|
|
|
|
page_namespace: 1,
|
|
|
|
|
source_url: 'http://some/url'
|
|
|
|
|
};
|
|
|
|
|
this.Title.newFromText.withArgs( data.page_title ).returns( {
|
|
|
|
|
getPrefixedDb: () => eventData.page_title
|
|
|
|
|
} );
|
|
|
|
|
this.Title.newFromText.withArgs( data.source_title ).returns( {
|
|
|
|
|
getPrefixedDb: () => eventData.source_title
|
|
|
|
|
} );
|
|
|
|
|
/* eslint-enable camelcase */
|
2018-03-27 19:42:01 +00:00
|
|
|
|
const tracker = getPageviewTracker( enabledConfig,
|
|
|
|
|
loader, this.trackerGetter, sendBeacon );
|
|
|
|
|
|
|
|
|
|
loader.resolves();
|
|
|
|
|
return tracker( 'event.VirtualPageView', data ).then( () => {
|
2018-04-24 22:03:32 +00:00
|
|
|
|
assert.strictEqual( loader.callCount, 1, 'loader called once' );
|
2019-06-19 02:08:47 +00:00
|
|
|
|
assert.ok( loader.calledWith( [ 'ext.eventLogging' ] ),
|
2018-03-27 19:42:01 +00:00
|
|
|
|
'appropriate code is loaded' );
|
2018-05-08 19:48:17 +00:00
|
|
|
|
assert.strictEqual(
|
|
|
|
|
this.Title.newFromText.callCount,
|
|
|
|
|
2,
|
|
|
|
|
'The title factory was invoked twice.'
|
|
|
|
|
);
|
2018-04-10 14:56:07 +00:00
|
|
|
|
assert.ok( this.prepare.calledWith( 'VirtualPageView', eventData ),
|
2018-03-27 19:42:01 +00:00
|
|
|
|
'mw.eventLog.prepare called appropriately' );
|
2018-04-24 22:03:32 +00:00
|
|
|
|
assert.strictEqual( this.makeBeaconUrl.callCount, 1,
|
2018-03-27 19:42:01 +00:00
|
|
|
|
'makeBeacon called with result of prepare' );
|
2018-04-24 22:03:32 +00:00
|
|
|
|
assert.strictEqual( sendBeacon.callCount, 1,
|
2018-03-27 19:42:01 +00:00
|
|
|
|
'sendBeacon called with url from makeBeaconUrl' );
|
|
|
|
|
} );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
QUnit.test( 'getSendBeacon', function ( assert ) {
|
|
|
|
|
let success = false;
|
|
|
|
|
const navtr = {
|
|
|
|
|
successful: true,
|
|
|
|
|
sendBeacon: function () {
|
|
|
|
|
// This local variable helps test context. Don't refactor.
|
|
|
|
|
success = this.successful;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const sendBeacon = getSendBeacon( navtr );
|
|
|
|
|
sendBeacon();
|
|
|
|
|
assert.ok( success, 'native sendBeacon is used when available and run in appropriate context' );
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
QUnit.test( 'getSendBeacon (fallback)', function ( assert ) {
|
|
|
|
|
const spy = this.sandbox.spy( document, 'createElement' );
|
|
|
|
|
const sendBeacon = getSendBeacon( {} );
|
|
|
|
|
sendBeacon();
|
2018-04-24 22:03:32 +00:00
|
|
|
|
assert.strictEqual( spy.callCount, 1, 'an img element is used as fallback' );
|
2018-03-27 19:42:01 +00:00
|
|
|
|
} );
|
2018-06-27 21:01:51 +00:00
|
|
|
|
|
|
|
|
|
QUnit.test( 'limitByEncodedURILength', function ( assert ) {
|
|
|
|
|
const shortUrl = 'https://en.wikipedia.org/wiki/banana',
|
|
|
|
|
longEncodedUrl = 'https://ka.wikipedia.org/wiki/%E1%83%95%E1%83%98%E1%83%99%E1%83%98%E1%83%9E%E1%83%94%E1%83%93%E1%83%98%E1%83%90:%E1%83%95%E1%83%98%E1%83%99%E1%83%98%E1%83%A1_%E1%83%A3%E1%83%A7%E1%83%95%E1%83%90%E1%83%A0%E1%83%A1_%E1%83%AB%E1%83%94%E1%83%92%E1%83%9A%E1%83%94%E1%83%91%E1%83%98/%E1%83%AB%E1%83%94%E1%83%92%E1%83%9A%E1%83%94%E1%83%91%E1%83%98%E1%83%A1_%E1%83%A1%E1%83%98%E1%83%90/%E1%83%99%E1%83%90%E1%83%AE%E1%83%94%E1%83%97%E1%83%98/%E1%83%A1%E1%83%90%E1%83%92%E1%83%90%E1%83%A0%E1%83%94%E1%83%AF%E1%83%9D%E1%83%A1_%E1%83%9B%E1%83%A3%E1%83%9C%E1%83%98%E1%83%AA%E1%83%98%E1%83%9E%E1%83%90%E1%83%9A%E1%83%98%E1%83%A2%E1%83%94%E1%83%A2%E1%83%98',
|
|
|
|
|
randomSequence = 'チプロ للا المتحة Добро пожаловат פעילה למען שוווв ВикипедиюA %20%F0%9F%A4%A8%20 IJ@_#*($PJOWR',
|
|
|
|
|
contentRg = new RegExp( limitByEncodedURILength( randomSequence, 326 ) );
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
limitByEncodedURILength( shortUrl, 1000 ), shortUrl,
|
|
|
|
|
'short url is not truncated' );
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
encodeURIComponent(
|
|
|
|
|
limitByEncodedURILength( longEncodedUrl, 1000 )
|
|
|
|
|
).length < 1000, true,
|
|
|
|
|
'long url is truncated to the correct length' );
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
encodeURIComponent(
|
|
|
|
|
limitByEncodedURILength( randomSequence, 50 )
|
|
|
|
|
).length < 50, true,
|
|
|
|
|
'Random string is truncated to the correct length' );
|
|
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
|
contentRg.test( randomSequence ), true,
|
|
|
|
|
'Truncated string contains the same content as the original'
|
|
|
|
|
);
|
|
|
|
|
} );
|