Created
January 28, 2014 18:20
-
-
Save chetanankola/8673181 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global YUI */ | |
/** | |
* @module search-assist-ult-tests | |
* Tests | |
* Most common problems in testing: | |
- since we using teh same sa variable and same search-box id for all tests, subscriptions duplicate | |
- a good solution is to call sa._detachAllSubscriptions(); where the library will remove all subscriptions | |
- It also requires hence to attach all subscription to a global object during development | |
for eg: allSubscr._navigateSrchNodeOnKeyUp = searchbox.on('keyup') | |
* check form submissionData when suggestion is clicked in contrast to when return key is used | |
* - when ult: {} object is specified it has atleast spaceId | |
* - if ult object is specified with spaceid then ylc token should be generated | |
* - if no ult: {} is specified.. no ylc token is generated | |
* - test yltCustom too | |
* - test extraultparameters | |
* - test autocomplete logging sent to gossip | |
* - test that if ultEnabled: false then no logging should happen | |
* - For custom dataSrc no ult logging should happen | |
*/ | |
YUI.add('search-assist-ult-tests', function (Y, NAME) { | |
'use strict'; | |
var suite = new Y.Test.Suite('search-assist-ult-tests'), | |
A = Y.Assert, | |
oldJsonP = Y.jsonp, | |
sboxId = '#search-box', | |
//defaultAction = "keyup", | |
globalSubscr = {}, | |
saGlobals = new Y.SEARCHASSIST.SAGLOBALS({device: 'desktop', sboxId: sboxId}),//defined in ../../common/search-assist-globals | |
cssGlobals = saGlobals.cssGlobals, | |
testGlobals = saGlobals.testGlobals, | |
eventGlobals = saGlobals.eventGlobals, | |
//globalVars = saGlobals.globalVars, | |
globalMockData = testGlobals.mockData, | |
sa = null, | |
done, | |
useMockData; | |
done = function (testObj) { | |
Y.log(testObj, 'debug'); | |
Y.log('Y.Test.Runner._waiting is now ' + Y.Test.Runner._waiting, 'debug', NAME); | |
//Y.Test.Runner._waiting = true; | |
//testObj.resume(); | |
//Y.Test.Runner._running = false; | |
Y.Test.Runner.resume(); | |
// var subscription; | |
// Y.log("############### DONE TESTS #############", 'debug', NAME); | |
// Y.log("###############################################", 'debug', NAME); | |
// Y.log("###############################################", 'debug', NAME); | |
// Y.log("############### TEARING DOWN TESTS #########", 'debug', NAME); | |
// Y.log("###############################################", 'debug', NAME); | |
// Y.log("###############################################", 'debug', NAME); | |
// //destroy sa's registered events since sa is used in many test cases | |
// if (sa) { | |
// sa._detachAllSubscriptions(); | |
// } | |
// * | |
// * all subscriptions done during tests should be attached to globalSubscr variable so that | |
// * it can be picked up for detaching events, since we use the same sa object for many tests | |
// * see some tests to understand how this works. | |
// * for eg you can do globalSubscr.EVT_SUGGEST_MARKUP_RENDERED = sa.on('asdasdasd', function () {}); | |
// if (globalSubscr) { | |
// for (subscription in globalSubscr) { | |
// if (globalSubscr.hasOwnProperty(subscription)) { | |
// Y.log('detaching subscription: ' + subscription, 'debug'); | |
// globalSubscr[subscription].detach(); | |
// } | |
// } | |
// } | |
// // make sure tray is closed | |
// // sa.simulateEsc(); | |
// // since sa is reused in same suite.. destroy it | |
// if (sa) { | |
// sa.destroy(); | |
// sa = null; | |
// } | |
// Y.jsonp = oldJsonP; | |
}; | |
useMockData = function (mockData, mockError) { | |
Y.jsonp = function (url, obj) { | |
Y.log(url, 'debug'); | |
if (mockData) { | |
obj.on.success(mockData); | |
} else { | |
obj.on.failure(mockError); | |
} | |
}; | |
}; | |
suite.add(new Y.Test.Case({ | |
name: 'Search assist Automated Tests', | |
setUp: function () { | |
Y.log("~~~~~~~~~~~~~~~~~STARTING NEW TEST~~~~~~~~~~~~~~~~~~~~~\n\n\n", 'debug', NAME); | |
//reset the searchbox | |
Y.one(sboxId).set('value', ''); | |
// Y.on('end-test', function (obj) { | |
// obj.resume(function () { | |
// Y.log("test done", 'debug', NAME); | |
// }); | |
// }); | |
}, | |
/** | |
* @method tearDown | |
* will be called for each test | |
* in this function we destroy the search assist object and any subscription done in the test | |
*/ | |
tearDown: function () { | |
var subscription; | |
Y.log("###############################################", 'debug', NAME); | |
Y.log("###############################################", 'debug', NAME); | |
Y.log("#####################TEARING DOWN TESTS########", 'debug', NAME); | |
Y.log("###############################################", 'debug', NAME); | |
Y.log("###############################################", 'debug', NAME); | |
////destroy sa's registered events since sa is used in many test cases | |
if (sa) { | |
sa._detachAllSubscriptions(); | |
} | |
// * | |
// * all subscriptions done during tests should be attached to globalSubscr variable so that | |
// * it can be picked up for detaching events, since we use the same sa object for many tests | |
// * see some tests to understand how this works. | |
// * for eg you can do globalSubscr.EVT_SUGGEST_MARKUP_RENDERED = sa.on('asdasdasd', function () {}); | |
if (globalSubscr) { | |
for (subscription in globalSubscr) { | |
if (globalSubscr.hasOwnProperty(subscription)) { | |
Y.log('detaching subscription: ' + subscription, 'debug'); | |
globalSubscr[subscription].detach(); | |
} | |
} | |
} | |
//make sure tray is closed | |
//sa.simulateEsc(); | |
//since sa is reused in same suite.. destroy it | |
if (sa) { | |
sa.destroy(); | |
sa = null; | |
} | |
Y.jsonp = oldJsonP; | |
}, | |
// test submissionData.fr2 = 'sa-gp' //for click, touchend or gesturemoveend | |
// | |
"Enable ult, click on suggestion, check ult parameters for suggestion click": function () { | |
var self = this; | |
sa = new Y.SEARCHASSIST.CLIENT({ | |
searchBox: sboxId,//[required] | |
hostNodeToAttach: '#attach-sa', | |
enableTrending: true, | |
preventFormSubmit: true, | |
ultEnabled: true,//by default is true | |
ult: { | |
spaceId: "123456" //(mandatory param) if not specified no ylc token will be generated | |
}, | |
extraUltParams: { //if you want to add more linktrack params with the default set | |
randomParam: "1" | |
}, | |
yltCustom: '_ylt=customizedYltToken=1' | |
}); | |
useMockData(globalMockData, null); | |
A.isObject(sa, "Sa object is READY......"); | |
Y.log("\n\nREGISTERING TEST 1 ..........", 'debug', NAME); | |
// catch the event that has linktrack information | |
globalSubscr.EVT_SEARCH_FORM_INSTRUMENTED = sa.on(eventGlobals.EVT_SEARCH_FORM_INSTRUMENTED, function (e) {//booya | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("caught event search form instrumented", 'debug', NAME); | |
Y.log(e.spaceId, 'debug'); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
//self.resume(function () { | |
A.areEqual(e.spaceId, '123456'); | |
//self.wait(function () { | |
//Y.later(1000, this, function () { | |
done(self); | |
//}); | |
//}, 2000); | |
//Y.Test.Runner._resume(function(){}); | |
//console.log(Y.Test.Runner._waiting); | |
//self.resume(done); | |
//self.resume(function () {}); | |
//Y.fire('end-test', self); | |
//done(); | |
//}); | |
}); | |
globalSubscr.EVT_SUGGEST_MARKUP_RENDERED = sa.on(eventGlobals.EVT_SUGGEST_MARKUP_RENDERED, function () { | |
A.isObject(Y.one('.' + cssGlobals.CLASS_TRAY), 'div sa-tray exists'); | |
A.isObject(Y.one('.' + cssGlobals.CLASS_TRAY_LIST_CONTAINER), 'tray list container exists'); | |
var fnode = Y.one('.' + cssGlobals.CLASS_TRAY_LIST_CONTAINER + ' li'); | |
A.isObject(fnode, 'First suggestion exists'); | |
//simulate click on first suggestion | |
testGlobals.simulateActionOnNode(fnode, 'click'); | |
}); | |
testGlobals.openTray(); | |
Y.Test.Runner._waiting = false; | |
self.wait(); | |
}, | |
"test2 ult, click on suggestion, check ult parameters for suggestion click": function () { | |
var self = this; | |
sa = new Y.SEARCHASSIST.CLIENT({ | |
searchBox: sboxId,//[required] | |
hostNodeToAttach: '#attach-sa', | |
enableTrending: true, | |
preventFormSubmit: true, | |
ultEnabled: true,//by default is true | |
ult: { | |
spaceId: "abdef" //(mandatory param) if not specified no ylc token will be generated | |
}, | |
extraUltParams: { //if you want to add more linktrack params with the default set | |
randomParam: "1" | |
}, | |
yltCustom: '_ylt=customizedYltToken=1' | |
}); | |
useMockData(globalMockData, null); | |
this.wait(function () { | |
A.isObject(sa); | |
Y.log("\n\nREGISTERING TEST 2...........", 'debug', NAME); | |
// catch the event that has linktrack information | |
globalSubscr.EVT_SEARCH_FORM_INSTRUMENTED = sa.on(eventGlobals.EVT_SEARCH_FORM_INSTRUMENTED, function (e) {//booya | |
//self.resume(function () { | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("caught event search form instrumented", 'debug', NAME); | |
Y.log(e.spaceId, 'debug'); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
Y.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%", 'debug', NAME); | |
//qself.resume(function () { | |
A.areEqual(e.spaceId, 'abcdef'); | |
//self.resume(); | |
//console.log(Y.Test.Runner._waiting); | |
//Y.later(4000, this, function () { | |
done(self); | |
//}); | |
//}); | |
//}); | |
}); | |
globalSubscr.EVT_SUGGEST_MARKUP_RENDERED = sa.on(eventGlobals.EVT_SUGGEST_MARKUP_RENDERED, function () { | |
A.isObject(Y.one('.' + cssGlobals.CLASS_TRAY), 'div sa-tray exists'); | |
A.isObject(Y.one('.' + cssGlobals.CLASS_TRAY_LIST_CONTAINER), 'tray list container exists'); | |
var fnode = Y.one('.' + cssGlobals.CLASS_TRAY_LIST_CONTAINER + ' li'); | |
A.isObject(fnode, 'First suggestion exists'); | |
//simulate click on first suggestion | |
testGlobals.simulateActionOnNode(fnode, 'click'); | |
//self.wait(); | |
}); | |
testGlobals.openTray(); | |
//self.wait(); | |
}, 5000); | |
} | |
})); | |
Y.Test.Runner.add(suite); | |
}, '0.0.1', { | |
requires: [ | |
'test', | |
'search-assist-client', | |
'search-assist-globals' | |
] | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment