How to mock a window.location function in Karma/jasmine

Hugoren Martinako

I would like to mock a functionality in Karma who returns a file after clicking a download button. I have the following AngularJS controller:

var secure = angular.module('secure', []);
secure.controller('ProcedureController', ProcedureController);
ProcedureController.$inject = ['$controller', '$rootScope', '$scope', '$http'];

function ProcedureController($controller, $rootScope, $scope, $http) {

  ... // Controller does more stuff

  var href = window.location.href.split('/');
  var baseUrl = href[0] + '//' + href[2];
  var url = baseUrl + "/secure/regulations";

  $http.get(url)
    .success(function (data) {
        $scope.questions = data[0];
    })

  $scope.download = function (msg) {
    window.location = url + "/" + msg + "/attachment";
  }
}

The window.location object just call a RESTful service who provides him the desired file directly. And this is, basically, my try-test:

describe('ProcedureController', function () {
  beforeEach(module('secure'));

  beforeEach(inject(function ($rootScope, $http, $controller, $injector) {
    scope = $rootScope.$new();
    ProcedureController = $controller('ProcedureController', {
        $scope: scope,
        $http: $http
    });
  }));

  it ('should download something', function() {
    expect(scope.download(1)).toBeDefined();
  });

 });

So, my idea is to check when scope.download function is called, if it returns the right url, namely, when I try scope.download(1), the expected answer would be /secure/regulations/1/attachment, roughly.

How should I mock it? Any help is appreciated!

Wawy

Use $window instead:

var secure = angular.module('secure', []);
secure.controller('ProcedureController', ProcedureController);
ProcedureController.$inject = ['$controller', '$rootScope', '$scope', '$http', '$window'];

function ProcedureController($controller, $rootScope, $scope, $http, $window) {

  ... // Controller does more stuff

  var href = $window.location.href.split('/');
  var baseUrl = href[0] + '//' + href[2];
  var url = baseUrl + "/secure/regulations";

  $http.get(url)
    .success(function (data) {
        $scope.questions = data[0];
    })

  $scope.download = function (msg) {
    $window.location = url + "/" + msg + "/attachment";
  }
}

describe('ProcedureController', function () {
  var windowObj = {location: {href: ''}};

  beforeEach(mock.module(function($provide) {
     $provide.value('$window', windowObj);
  }));
  beforeEach(module('secure'));

  beforeEach(inject(function ($rootScope, $http, $controller, $injector) {
    scope = $rootScope.$new();
    ProcedureController = $controller('ProcedureController', {
        $scope: scope,
        $http: $http
    });
  }));

  it ('should download something', function() {
    expect(scope.download).toBeDefined();
    scope.download(1);
    expect(windowObj.location.href).toEqual('/secure/regulations/1/attachment');
  });

 });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to mock window.location.href with Jest + Vuejs?

From Dev

How to mock nested function in Jest?

From Dev

How to mock a tornado coroutine function using mock framework for unit testing?

From Dev

Why does window.location.reload need to be wrapped in a function(){}?

From Dev

How to watch for changes in window.location with Dart?

From Dev

How to mock a decorated function

From Dev

How to mock $window.location.replace in AngularJS unit test?

From Dev

Variable from function into window.location.href 'not defined'

From Dev

How to open image links in new window with window.location

From Dev

How to mock location service using KIF-framework

From Dev

how to mock $window to unit test AngularJS service?

From Dev

Python – How to mock a single function

From Dev

how to use window.location.search

From Dev

How to stub/mock a const function

From Dev

String is not a function on window location href

From Dev

How do I mock $window injected manually in provider private function?

From Dev

How to get the location of the component of a window in Linux?

From Dev

How to implement mock location?

From Dev

AngularJS $http mock respond(...): how to mock a `location` header in the response?

From Dev

How to check for Mock Location in Android Marshmallow?

From Dev

how to mock macro function using google mock

From Dev

How to mock jquery ready function

From Dev

How to not make window.location relative to file

From Dev

Click function, Window.location

From Dev

How to mock a void function inside a void function?

From Dev

How to store data after window.location?

From Dev

How to correctly and consistently mock location in Android?

From Dev

How to use the Input.Location and mock location in the unity editor

From Dev

Python how to mock a function within another function

Related Related

  1. 1

    How to mock window.location.href with Jest + Vuejs?

  2. 2

    How to mock nested function in Jest?

  3. 3

    How to mock a tornado coroutine function using mock framework for unit testing?

  4. 4

    Why does window.location.reload need to be wrapped in a function(){}?

  5. 5

    How to watch for changes in window.location with Dart?

  6. 6

    How to mock a decorated function

  7. 7

    How to mock $window.location.replace in AngularJS unit test?

  8. 8

    Variable from function into window.location.href 'not defined'

  9. 9

    How to open image links in new window with window.location

  10. 10

    How to mock location service using KIF-framework

  11. 11

    how to mock $window to unit test AngularJS service?

  12. 12

    Python – How to mock a single function

  13. 13

    how to use window.location.search

  14. 14

    How to stub/mock a const function

  15. 15

    String is not a function on window location href

  16. 16

    How do I mock $window injected manually in provider private function?

  17. 17

    How to get the location of the component of a window in Linux?

  18. 18

    How to implement mock location?

  19. 19

    AngularJS $http mock respond(...): how to mock a `location` header in the response?

  20. 20

    How to check for Mock Location in Android Marshmallow?

  21. 21

    how to mock macro function using google mock

  22. 22

    How to mock jquery ready function

  23. 23

    How to not make window.location relative to file

  24. 24

    Click function, Window.location

  25. 25

    How to mock a void function inside a void function?

  26. 26

    How to store data after window.location?

  27. 27

    How to correctly and consistently mock location in Android?

  28. 28

    How to use the Input.Location and mock location in the unity editor

  29. 29

    Python how to mock a function within another function

HotTag

Archive