diff options
author | Thomas Steur <thomas.steur@gmail.com> | 2016-09-30 02:03:13 +0300 |
---|---|---|
committer | Thomas Steur <thomas.steur@gmail.com> | 2016-09-30 02:03:13 +0300 |
commit | 43bd151e711d5c719cf89d2746c23da5e29dd264 (patch) | |
tree | af3c4969a64f759811a08c026b739b3d8e716435 /tests/javascript | |
parent | 68fea796ef5a81cf7c5f4fb17508968ac898bb7c (diff) | |
parent | 7d115b50ecb1660175e64e4e546c72546fd56d69 (diff) |
Merge branch '2.x-dev' into 3.0-m09
Diffstat (limited to 'tests/javascript')
-rw-r--r-- | tests/javascript/index.php | 115 |
1 files changed, 113 insertions, 2 deletions
diff --git a/tests/javascript/index.php b/tests/javascript/index.php index 34a8d252f5..f8cf328249 100644 --- a/tests/javascript/index.php +++ b/tests/javascript/index.php @@ -28,8 +28,6 @@ $targetFileName = '/tests/resources/piwik.test.js'; $sourceFile = PIWIK_DOCUMENT_ROOT . TrackerUpdater::DEVELOPMENT_PIWIK_JS; $targetFile = PIWIK_DOCUMENT_ROOT . $targetFileName; -file_put_contents($targetFile, ''); - $updater = new TrackerUpdater($sourceFile, $targetFile); $updater->setTrackerFiles(new JsTestPluginTrackerFiles()); $updater->checkWillSucceed(); @@ -611,6 +609,119 @@ function PiwikTest() { } }); + test("Piwik plugin methods", function() { + expect(26); + + // TESTS FOR retryMissedPluginCalls + + // these 2 calls should fail because they do not exist + _paq.push(['MyCustomPlugin::myCustomStaticMethod']); + _paq.push(['MyCustomPlugin::myCustomStaticMethod2']); + _paq.push(['MyCustomPlugin.myCustomMethod']); + + // now we define these method + var called = 0; + var calledStatic = 0; + var calledStatic2 = 0; + Piwik.MyCustomPlugin = {myCustomStaticMethod: function () { calledStatic++; }}; + var asyncTrackers = Piwik.getAsyncTrackers(); + var i = 0; + for (i; i < asyncTrackers.length; i++) { + asyncTrackers[i].MyCustomPlugin = {myCustomMethod: function () { called++; }}; + } + + // now we retry those calls + Piwik.retryMissedPluginCalls(); + + strictEqual(1, called, "retryMissedPluginCalls, successfully executed non static method once it is defined"); + strictEqual(1, calledStatic, "retryMissedPluginCalls, successfully executed static method once it is defined"); + strictEqual(0, calledStatic2, "retryMissedPluginCalls, should not have executed not defined method"); + + // defining another method + Piwik.MyCustomPlugin.myCustomStaticMethod2 = function () { calledStatic2++; }; + + // retrying again should not call the missed plugin calls again because they are now defined + Piwik.retryMissedPluginCalls(); + + strictEqual(1, called, "retryMissedPluginCalls, should not execute a resolved missed call again"); + strictEqual(1, calledStatic, "retryMissedPluginCalls, should not execute a resolved missed call again"); + strictEqual(1, calledStatic2, "retryMissedPluginCalls, successfully executed static method 2 once it is defined"); + + // calling them now that they are defined increases the counter immediately + _paq.push(['MyCustomPlugin::myCustomStaticMethod']); + _paq.push(['MyCustomPlugin.myCustomMethod']); + + strictEqual(2, called, "executing static plugin method works directly if defined"); + strictEqual(2, calledStatic, "executing plugin method works directly if defined"); + strictEqual(1, calledStatic2, "a method is only executed when actually pushed"); + + // TESTS FOR events + var calledEvent1 = 0; + var calledEvent1_1 = 0; + var calledEvent2 = 0; + var passedArgs = null; + + function callEvent1() { calledEvent1++; } + function callEvent1_1() { calledEvent1_1++; } + function callEvent2(arg1, arg2) { calledEvent2++; passedArgs = [arg1, arg2]; } + + Piwik.on('myEvent1', callEvent1); + Piwik.on('myEvent2', callEvent2); + + Piwik.trigger('myEvent1', []); + strictEqual(1, calledEvent1, "event, should trigger event and call handler callEvent1"); + + Piwik.trigger('myEvent1', []); + strictEqual(2, calledEvent1, "event, should trigger event whenever it is called and call handler callEvent1 again"); + strictEqual(0, calledEvent2, "event, should only execute event listeners that listen to that triggered event"); + + Piwik.trigger('myEvent2', ['arg1', 'arg2']); + strictEqual(2, calledEvent1, "event, should not have executed that event because it has different name"); + strictEqual(1, calledEvent2, "event, should have executed different handler this time"); + deepEqual(['arg1', 'arg2'], passedArgs, "event, should be possible to pass arguments to events"); + + Piwik.on('myEvent1', callEvent1_1); + + Piwik.trigger('myEvent1', []); + strictEqual(3, calledEvent1, "event, should call multiple event handlers when many listen to same event"); + strictEqual(1, calledEvent1_1, "event, should call multiple event handlers when many listen to same event"); + + Piwik.off('myEvent1', callEvent1); + + Piwik.trigger('myEvent1', []); + strictEqual(3, calledEvent1, "event, it is possible to remove an event listener and it will not be executed anymore"); + strictEqual(2, calledEvent1_1, "event, should still call other event listeners when others were removed"); + + /** + * TESTING DOM + **/ + var loaded = false; + var ready = false; + var customEvent = false; + + strictEqual('object', typeof Piwik.DOM, "Piwik.DOM object is defined"); + strictEqual('function', typeof Piwik.DOM.onReady, "DOM.onReady method is defined"); + strictEqual('function', typeof Piwik.DOM.onLoad, "DOM.onLoad method is defined"); + strictEqual('function', typeof Piwik.DOM.addEventListener, "DOM.addEventListener method is defined"); + + Piwik.DOM.onLoad(function () { + loaded = true; + }); + Piwik.DOM.onReady(function () { + ready = true; + }); + + strictEqual(true, ready, "onReady, DOM should be ready"); + strictEqual(true, loaded, "event, DOM should be loaded"); + + Piwik.DOM.addEventListener(_e('click7'), 'myCustomEvent', function () { + customEvent = true; + }); + triggerEvent(_e('click7'), 'myCustomEvent'); + + strictEqual(true, customEvent, "DOM.addEventListener works"); + }); + test("Query", function() { var tracker = Piwik.getTracker(); var query = tracker.getQuery(); |