Skip to content

Instantly share code, notes, and snippets.

@firecentaur
Created November 10, 2017 10:56
Show Gist options
  • Save firecentaur/d3de98764a9d2560b3308f3a6776660e to your computer and use it in GitHub Desktop.
Save firecentaur/d3de98764a9d2560b3308f3a6776660e to your computer and use it in GitHub Desktop.
MockRouter
/**
* gets and returns static json mocks
* @param jsonFilePath
* @returns {[null,null,null]}
*/
function getData(jsonFilePath) {
var request = new XMLHttpRequest();
request.open('GET', jsonFilePath, false);
request.send(null);
return [request.status, request.response, {}];
}
/**
* getJsonForGroup is a predicate function to be used as the url parameter in
* $httpBackend.whenGet/whenPOST/whenPUT/whenDELETE function calls.
*
* It takes as its parameters a group id (gid) and a members roleName
*
* ie: $httpBackend.whenGET(getJsonForGroup("member")).respond.apply(this, getData(mepGroupJsonForMember));
*
* It will return true / false if the current request url is a /group/load? url,
* AND the current member has/ does not have the roleName specified
*
* This function determins the roleName of the current user by looking at roles for the user for the group as
* defined in the $window.USER.groups array
*
* Since you know this function will return true or false, you can use it's truth value
* to load specific json files
*
* ie: $httpBackend.whenGET(getJsonForGroup("9","member")).respond.apply(this, getData(mepGroupJsonForMember));
*
* Here you can see that mepGroupJsonForMember.json will be returned if the request url is a /group/load call
* for gid=9
*
* @param gid - group id
* @param page - page number
* @param roleName - role: ie member,editor,admin
* @returns {{test: test}}
*/
var getJsonForGroup = function (gid,page,roleName) {
return {
test: function (incomingUrl) {
var urlToMatch = '/group/load?gid='+gid+'&paginationOptions=%7B%22pageNumber%22:'+page+',%22searchType%22:%22all%22,%22searchTerm%22:%22%22,%22pageSize%22:10,%22sort%22:null%7D';
if (incomingUrl == urlToMatch) {
var usersRole;
//get usersMepRole
_.each($window.USER.groups, function (group) {
if (parseInt(group.id) == parseInt(gid)){
usersRole = group.roles[0];
}
});
if (usersRole.toLowerCase() == roleName.toLowerCase()) {
return true;//return true so the json gets sent
} else {
return false;//return false so that the json does not get sent
}
} else {
return false;//return false so that the json does not get sent
}
}
}
};
/**
* getJsonForMyGroups is a mockRouter for responding with specific json files for specific users for the getMyGroups call
* An example call of this mockRouter looks like:
*
* $httpBackend.whenGET(getJsonForMyGroups("testmember1")).respond.apply(this, getData(myGroupsJsonForTestMember1));
*
* In the example above, makes ure to specify the user name as a parameter and the specific json in the getData parameter in the response
*
* @param page
* @param username
* @returns {{test: test}}
*/
var getJsonForMyGroups = function (page,username) {
return {
test: function (incomingUrl) {
var urlToMatch = '/group/getMyGroups?paginationOptions=%7B%22pageNumber%22:'+page+',%22searchType%22:%22all%22,%22searchTerm%22:%22%22,%22pageSize%22:5,%22sort%22:null%7D';
if (incomingUrl == urlToMatch) {
var currentUsername = $window.USER.username;
if (username.toLowerCase() == currentUsername.toLowerCase()) {
return true;//return true so the json gets sent
} else {
return false;//return false so that the json does not get sent
}
} else {
return false;//return false so that the json does not get sent
}
}
}
};
/**
* getJsonForGetContent is a predicate function for $httpBackend.whenGET/whenPOST etc
* It will return true if the current url is a getContent call, for the category,cid,page specified
* and the username who is logged in.
*
* Use this predicate function to load the appropriate getContentFor<group><page><username>.json
*
* @param category
* @param cid
* @param page
* @param username
* @returns {{test: test}}
*/
var getJsonForGetContent = function (category, cid,page,username) {
return {
test: function (incomingUrl) {
var urlToMatch = '/content/getContent?category='+category+'&cid='+cid+'&contentToInclude=%7B%22videos%22:true,%22decks%22:true,%22folders%22:true%7D&paginationOptions=%7B%22pageNumber%22:'+page+',%22searchType%22:%22all%22,%22searchTerm%22:%22%22,%22pageSize%22:10,%22sort%22:null%7D';
if (incomingUrl == urlToMatch) {
var currentUsername = $window.USER.username;
if (username.toLowerCase() == currentUsername.toLowerCase()) {
return true;//return true so the json gets sent
} else {
return false;//return false so that the json does not get sent
}
} else {
return false;//return false so that the json does not get sent
}
}
}
};
//load mep group json
var mepGroupJsonForMember = '/responses/group/load/loadMepGroupPage1Member.json';
var mepGroupJsonForEditor = '/responses/group/load/loadMepGroupPage1Editor.json';
//getMyGroups json
var getMyGroupsPage1Member = '/responses/group/getMyGroups/getMyGroupsPage1Member.json';
//getContent json
var getContentJsonForMember = '/responses/content/getContent/getContentForMepGroupPage1Member.json';
//load mep group
$httpBackend.whenGET(getJsonForGroup(9,1,"member")).respond.apply(this, getData(mepGroupJsonForMember));
$httpBackend.whenGET(getJsonForGroup(9,1,"editor")).respond.apply(this, getData(mepGroupJsonForEditor));
//load my groups
$httpBackend.whenGET(getJsonForMyGroups(0,"member")).respond.apply(this, getData(getMyGroupsPage1Member));
//getContent
$httpBackend.whenGET(getJsonForGetContent("Group",9,1,"member")).respond.apply(this, getData(getContentJsonForMember));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment