Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var someUtilityFunction = () {
- return window.location.search.substring(1);
- };
- test('#1 someUtilityFunction works', function () {
- // setup
- var oldQS = window.location.search;
- window.location.search = '?key1=value1&key2=value2&key3=value3';
- var expectedOutput = 'key1=value1&key2=value2&key3=value3';
- // test
- equals(someUtilityFunction(),
- expectedOutput,
- 'someUtilityFunction works as expected.');
- // teardown
- window.location.search = oldQS;
- });
- var customWindow = {
- location: {
- search: "",
- hash: ""
- }
- };
- var someUtilityFunction;
- (function(window) {
- // window is now shadowed by your local variable
- someUtilityFunction = () {
- return window.location.search.substring(1);
- };
- })(customWindow);
- // first some more preparation for our mock
- customWindow.window = customWindow;
- with(customWindow) {
- // this still creates the var in the global scope
- var someUtilityFunction = () {
- // window is a property of customWindow
- return window.location.search.substring(1);
- };
- // since customWindow is our scope now
- // this will work also
- someUtilityFunction = () {
- // location is a property of customWindow too
- return location.search.substring(1);
- };
- }
- window.location.search.replace(/^?/, "");
- window.location.substr(1);
Add Comment
Please, Sign In to add comment