From 1457da9797a05eac88401eabac6bedf03f0ea3c4 Mon Sep 17 00:00:00 2001 From: Andy Creighton Date: Thu, 28 Jan 2016 12:03:30 -0500 Subject: [PATCH] comment clean up and minor fixes to tests --- src/xapi-util.js | 27 +++----- test/my.mock.wrapper.js | 135 +++------------------------------------- test/tests/test.util.js | 16 +++-- 3 files changed, 26 insertions(+), 152 deletions(-) diff --git a/src/xapi-util.js b/src/xapi-util.js index f5aa4ba..94bcb98 100644 --- a/src/xapi-util.js +++ b/src/xapi-util.js @@ -38,45 +38,30 @@ }; ADL.xapiutil.getLangVal = function (langprop) { -//test for undefined first + if (!langprop) return; -//let's get wordy for a bit and think things out -//is this really the best way to do this? don't know but let's do it -//and then look at how to make it better - //so we've tested and there is something passed in at this point - //next we grab the keys in an array so we can easily get to what we want + var options = Object.keys(langprop); // test that langprop is a dict (obj) // skips if not a dict(obj) and returns - //is undefined what we want to return - - //so maybe the calling function will have to test and then get id or something if (options.length == 0) return undefined; - //at this point something was passed in and that something has some kind of properties that we have the keys for in an array - //so let's get the language and see if we can find a match - //note we only go from what we are given and whittle down until we find a match or had back the first option if we find nothing - //should we have looked harder to make sure this is a dictionary, or worked to find a more complex match, i'm kind of hoping not because i'm happy to not do work that won't really make it better, but on the other hand if this really would make this better at what it it doing, then I want to do it var lang = ADL.xapiutil.getLang(), - ret, //make undefined + ret, dispGotten = false; + do { //test and retest - //if the key works, unlock the door if (langprop[lang]) { ret = langprop[lang]; dispGotten = true; } - //if not, adjust the key and set up to try again else if (lang.indexOf('-')) { lang = lang.substring(0, lang.lastIndexOf('-')); -// console.log('lang is now', lang); } - //there was another else option, but the while test takes care of stopping if there is nothing left of our string } while (!dispGotten && lang !==""); - //and here we return what we've found or haven't - //let's go see if this will all run, and let's make some tests to try it out return ret; }; @@ -84,7 +69,7 @@ if (!onBrowser) throw new Error("Node not supported."); var stmts = []; -// debugger; + ADL.XAPIWrapper.getStatements(searchParams, null, function getMore(r) { if (! (r && r.response) ) return; var res = JSON.parse(r.response); @@ -145,6 +130,7 @@ }; ADL.xapiutil.getObjectIdString = function (o) { + if (!o) return 'unknown' if (o.id) return o.id; var type = ADL.xapiutil.getObjectType(o); if (type === "Agent" || type === "Group") return ADL.xapiutil.getActorIdString(o); @@ -155,6 +141,7 @@ }; ADL.xapiutil.getObjectDisplay = function (o) { + if (!o) return "unknown" var disp = getObjDefName(o) || o.name || o.id; if (! disp) { var type = ADL.xapiutil.getObjectType(o); diff --git a/test/my.mock.wrapper.js b/test/my.mock.wrapper.js index 83b4d1e..0fad3f0 100644 --- a/test/my.mock.wrapper.js +++ b/test/my.mock.wrapper.js @@ -845,17 +845,15 @@ function isDate(date) { */ ADL.XHR_request = function(lrs, url, method, data, auth, callback, callbackargs, ignore404, extraHeaders) { - var arrstmts = []; var urlparts = url.split('/'); - var index = urlparts[urlparts.length - 1]; var keys = Object.keys(ADL.stmts); var numstmts = keys.length; var cutoff = 4; //only send 4 statements at a time -//index will either be zero or where to start with more statements + //index will either be zero or where to start with more statements if (index == "statements") { index = 0; @@ -864,148 +862,33 @@ function isDate(date) { { index = parseInt(index); } -//if doling out our ususal size block of statments would exceed the amount -//of statements that we have, then adjust the cutoff and send out what's left + //if doling out our ususal size block of statments would exceed the //amount of statements that we have, then adjust the cutoff + //and send out what's left if (cutoff > (numstmts - index)) { cutoff = numstmts - index; } -//make a mini array of the keys we want to return + //make a mini array of the keys we want to return var thesekeys = keys.slice(index, index + cutoff); -//use the mini array of keys to get the statments we want to return - hooray!! + for (var k in thesekeys) { arrstmts.push(ADL.stmts[thesekeys[k]]); } -//assume there's no more statements, if this is more, then return the next index number - it's magic + //assume there's no more statements, if there are more, + //then return the next index number var morestmts = ""; if ((index + cutoff) < numstmts) { morestmts = "/" + (index + cutoff); } -//mkae a response object, and make that a string + //make a response object, and make that a string var response = { statements: arrstmts, more: morestmts}; response = JSON.stringify(response); -//a good days work, we're outta here + callback({response}); - // "use strict"; - // - // var xhr, - // finished = false, - // xDomainRequest = false, - // ieXDomain = false, - // ieModeRequest, - // urlparts = url.toLowerCase().match(/^(.+):\/\/([^:\/]*):?(\d+)?(\/.*)?$/), - // location = window.location, - // urlPort, - // result, - // extended, - // prop, - // until; - // - // //Consolidate headers - // var headers = {}; - // headers["Content-Type"] = "application/json"; - // headers["Authorization"] = auth; - // headers['X-Experience-API-Version'] = ADL.XAPIWrapper.xapiVersion; - // if(extraHeaders !== null){ - // for(var headerName in extraHeaders){ - // headers[headerName] = extraHeaders[headerName]; - // } - // } - // - // //See if this really is a cross domain - // xDomainRequest = (location.protocol.toLowerCase() !== urlparts[1] || location.hostname.toLowerCase() !== urlparts[2]); - // if (!xDomainRequest) { - // urlPort = (urlparts[3] === null ? ( urlparts[1] === 'http' ? '80' : '443') : urlparts[3]); - // xDomainRequest = (urlPort === location.port); - // } - // - // //If it's not cross domain or we're not using IE, use the usual XmlHttpRequest - // if (!xDomainRequest || typeof(XDomainRequest) === 'undefined') { - // xhr = new XMLHttpRequest(); - // xhr.open(method, url, callback != null); - // for(var headerName in headers){ - // xhr.setRequestHeader(headerName, headers[headerName]); - // } - // } - // //Otherwise, use IE's XDomainRequest object - // else { - // ieXDomain = true; - // ieModeRequest = ie_request(method, url, headers, data); - // xhr = new XDomainRequest(); - // xhr.open(ieModeRequest.method, ieModeRequest.url); - // } - // - //Setup request callback - // function requestComplete() { - // if(!finished){ - // // may be in sync or async mode, using XMLHttpRequest or IE XDomainRequest, onreadystatechange or - // // onload or both might fire depending upon browser, just covering all bases with event hooks and - // // using 'finished' flag to avoid triggering events multiple times - // finished = true; - // var notFoundOk = (ignore404 && xhr.status === 404); - // if (xhr.status === undefined || (xhr.status >= 200 && xhr.status < 400) || notFoundOk) { - // if (callback) { - // if(callbackargs){ - // callback(xhr, callbackargs); - // } - // else { - // try { - // var body = JSON.parse(xhr.responseText); - // callback(xhr,body); - // } - // catch(e){ - // callback(xhr,xhr.responseText); - // } - // } - // } else { - // result = xhr; - // return xhr; - // } - // } else { - // var warning; - // try { - // warning = "There was a problem communicating with the Learning Record Store. ( " - // + xhr.status + " | " + xhr.response+ " )" + url - // } catch (ex) { - // warning = ex.toString(); - // } - // ADL.XAPIWrapper.log(warning); - // ADL.xhrRequestOnError(xhr, method, url, callback, callbackargs); - // result = xhr; - // return xhr; - // } - // } else { - // return result; - // } - // }; - // - // xhr.onreadystatechange = function () { - // if (xhr.readyState === 4) { - // return requestComplete(); - // } - // }; - // - // xhr.onload = requestComplete; - // xhr.onerror = requestComplete; - // //xhr.onerror = ADL.xhrRequestOnError(xhr, method, url); - // - // xhr.send(ieXDomain ? ieModeRequest.data : data); - // - // if (!callback) { - // // synchronous - // if (ieXDomain) { - // // synchronous call in IE, with no asynchronous mode available. - // until = 1000 + new Date(); - // while (new Date() < until && xhr.readyState !== 4 && !finished) { - // delay(); - // } - // } - // return requestComplete(); - // } }; /* diff --git a/test/tests/test.util.js b/test/tests/test.util.js index ddb5866..eebae11 100644 --- a/test/tests/test.util.js +++ b/test/tests/test.util.js @@ -1,5 +1,5 @@ after(function () { - //i've got nothing to go in here + //nothing in here }); describe('testing xAPI utilities', function () { @@ -54,9 +54,7 @@ describe('testing xAPI utilities', function () { }); it('should return the first display option if language code does not match any of the keys', function () { ("undefined").should.eql(typeof util.getLangVal(s6.verb.display)); - // (util.getLangVal(s6.verb.display)).should.eql("établi"); }); -//what else do we want to test it('should throw out junk', function () { ("undefined").should.eql(typeof util.getLangVal(s5.actor)); ("undefined").should.eql(typeof util.getLangVal(s5.verb)); @@ -199,7 +197,7 @@ describe('testing xAPI utilities', function () { (util.getObjectId(s1.object)).should.eql(s1.object.id); }); it('should get the actor id, if no id and objectType is Agent', function () { - (util.getObjectId(s2.object)).should.eql(util.getActorId(s2.object)); + (util.getObjectId(s2.object)).should.eql(s2.object.mbox); }); it('should get the actor id, if no id and objectType is Group', function () { (util.getObjectId(s3.object)).should.eql(util.getActorId(s3.object)); @@ -222,7 +220,7 @@ describe('testing xAPI utilities', function () { }); it('should get the Actor Id String, if no id and type is Agent or Group', function () { (util.getObjectIdString(s2.object)).should.be.String(); - (util.getObjectIdString(s2.object)).should.eql(util.getActorIdString(s2.object)); + (util.getObjectIdString(s2.object)).should.eql(s2.object.mbox); }); it('should get the Actor-Verb-Object String, if no id and type is SubStatement', function () { (util.getObjectIdString(s4.object)).should.be.String(); @@ -231,9 +229,10 @@ describe('testing xAPI utilities', function () { it('should return unknown, if those do not work', function () { ('unknown').should.eql(util.getObjectIdString(s6.object)); }); -//what if we pass this something bogus it('should return unknown, if you pass it junk', function () { ("unknown").should.eql(util.getObjectIdString(stmts)); + ("unknown").should.eql(util.getObjectIdString('stmts')); + ("unknown").should.eql(util.getObjectIdString()); }); }); @@ -253,6 +252,11 @@ describe('testing xAPI utilities', function () { it('or should get the Actor-Verb-Object String', function () { (util.getObjectDisplay(s4.object)).should.eql(s3.actor.mbox_sha1sum + ":" + s3.verb.id + ":" + s3.object.mbox_sha1sum); }); + it('should handle junk', function () { + ('unknown').should.eql(util.getObjectDisplay(s3)); + ('unknown').should.eql(util.getObjectDisplay()); + ('unknown').should.eql(util.getObjectDisplay('s3')); + }); }); })