How to unit test an AngularJS service / factory

Complexity

I'm already some time in the development using AngularJS, and what I do write works, but know I've come to a point where I would like to run unit tests on my AngularJS code.

I have created a very simple service that will inject a stylesheet onto the page, see the example code below:

var OfficeSuiteStylesheetInjectorModule = angular.module('OfficeSuiteStylesheetInjectorModule', []);

OfficeSuiteStylesheetInjectorModule.factory('stylesheetInjectorService', ['$q', function($q) {
    // Returns the service itself.
    return {
        inject: function(id, uri) {
            var deferred = $q.defer();

            // Embed the stylesheet into the page, but only when it's non-existing.
            if (!angular.element('#' + id).length) {
                    var link = StylesheetFactory.Create(id, uri);
                    {
                        link.onload = deferred.resolve;
                        angular.element('head').append(link);
                    }

                    return deferred.promise;
                }
        }
    }
}]);

It's not a big service, it's just dependend on $q for promises so that I can run additional logic when the stylesheet has been embedded in the page.

Now, I'm using Jasmine (I'm quite new to this) for testing my JavaScript code and I would like to test this module.

I have a skeleton:

// Tests for the angular 'StylesheetInjectorService'.
describe('StylesheetInjectorService', function() {
    var stylesheetInjectorService = {};

    // This code is executed before every test.
    beforeEach(function() {
        // Tells jamine that the module we're working on is the 'OfficeSuiteStylesheetInjectorModule'.
        angular.module('OfficeSuiteStylesheetInjectorModule');
    });

    // Ensures that it injects a stylesheet element into the page.
    it('Should inject a stylesheet element into the page.', function() {
        // How to test here that the stylesheet is injected?
    });
  });
});

How can I inject te service in the page and ensures that the stylesheet is loaded?

Edit: Loading service now works:

beforeEach(module('OfficeSuiteStylesheetInjectorModule'));

// This code is executed before every test.
beforeEach(function() {

    // Inject the required dependencies into the page.
    inject(function($injector) {
        stylesheetInjectorService = $injector.get('stylesheetInjectorService');
    });
});

The same question is still open however. How to test if a stylesheet was embedded in the page?

Any help is highly appreciated.

Kind regards

Kasper Lewau

To write a spec for the attachment of a stylesheet to angular.element('head') I would change the logic a bit to attach it to $document.head.

If you dont want to do that, I would recommend that you change your service into a directive seeing as how injecting a script element, is manipulating the DOM. That way you would kill two birds with one stone, as you would need to inject $compile to test your directive (which would enable you to $compile a custom head element to boot). But this is slightly "over the top" for now.

Implementation:

if (!angular.element('#' + id).length) {
  var link = StylesheetFactory.Create(id, uri);

    link.onload = deferred.resolve;

    $document.head.append(link);

    return deferred.promise;
  }
}

beforeEach:

/**
 * Sorry, this was previously $location which was just 
 * such a silly mistake.
 */
var $timeout; 

beforeEach(function () {
  inject(function ($injector) {
    $timeout = $injector.get('$timeout');
  });
});

it:

it('attaches the stylesheet to $document.head', function () {
  styleSheetInjectorService.inject('someId', '/path/to/some/stylesheet');
  $timeout.flush(); // flush promises
  expect(angular.element($document.head).lastChild[0].nodeName).to.eq('LINK');
});

Something along those lines should get you up and running. Bare in mind that the spec I wrote uses the chai#expect style assertions, and the mocha test framework. Edit the syntax to fit Jasmine if you mean to copy-paste.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to unit test an asynchronus factory which uses another factory in AngularJS?

From Dev

How to unit test an asynchronus factory which uses another factory in AngularJS?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

Angularjs Unit Test for Service

From Dev

How can I write a unit test for angularJs Factory

From Dev

How to unit-test an AngularJS service that uses localStorage internally

From Dev

How to unit test $location service search() method, in AngularJS?

From Dev

How to write unit test for a controller that depends on a factory service which itself depends on the $http service

From Dev

How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

Unit test service that returns promise Angularjs Jasmine

From Dev

How to unit test $mdSidenav service?

From Dev

How to unit test angularjs form?

From Dev

AngularJS + CanvasJS: how to unit test?

From Dev

AngularJS + CanvasJS: how to unit test?

From Dev

Failing unit test of factory with dependency in AngularJS using Jasmine & Karma

From Dev

AngularJS + Jasmine mock unit test $http factory of POST method

From Dev

Jasmine/AngularJS: Inject dependent service to service in unit test?

From Dev

angularjs unit test promise based service (SQlite database service)

From Java

How do I mock a service that returns promise in AngularJS Jasmine unit test?

From Dev

How to test an angularJS service with httpBackend?

From Dev

Jasmine unit test AngularJS factory with two dependencies ($http and another factory returning promise)

From Dev

How to mock AngularFire 2 service in unit test?

From Dev

How to unit test Service Fabric Actor with State

From Dev

How to unit test web service with Linq

From Dev

How to unit test web service with Linq

From Dev

How to determine whether a service was stopped in unit test?

From Dev

How to mock AngularFire 2 service in unit test?

Related Related

  1. 1

    How to unit test an asynchronus factory which uses another factory in AngularJS?

  2. 2

    How to unit test an asynchronus factory which uses another factory in AngularJS?

  3. 3

    how to mock $window to unit test AngularJS service?

  4. 4

    Angularjs Unit Test for Service

  5. 5

    How can I write a unit test for angularJs Factory

  6. 6

    How to unit-test an AngularJS service that uses localStorage internally

  7. 7

    How to unit test $location service search() method, in AngularJS?

  8. 8

    How to write unit test for a controller that depends on a factory service which itself depends on the $http service

  9. 9

    How to unit test (using Jasmine) a function in a controller which calls a factory service which returns a promise

  10. 10

    Correct way to mock an AngularJS Service in a unit test?

  11. 11

    Unit test service that returns promise Angularjs Jasmine

  12. 12

    Unit test service that returns promise Angularjs Jasmine

  13. 13

    How to unit test $mdSidenav service?

  14. 14

    How to unit test angularjs form?

  15. 15

    AngularJS + CanvasJS: how to unit test?

  16. 16

    AngularJS + CanvasJS: how to unit test?

  17. 17

    Failing unit test of factory with dependency in AngularJS using Jasmine & Karma

  18. 18

    AngularJS + Jasmine mock unit test $http factory of POST method

  19. 19

    Jasmine/AngularJS: Inject dependent service to service in unit test?

  20. 20

    angularjs unit test promise based service (SQlite database service)

  21. 21

    How do I mock a service that returns promise in AngularJS Jasmine unit test?

  22. 22

    How to test an angularJS service with httpBackend?

  23. 23

    Jasmine unit test AngularJS factory with two dependencies ($http and another factory returning promise)

  24. 24

    How to mock AngularFire 2 service in unit test?

  25. 25

    How to unit test Service Fabric Actor with State

  26. 26

    How to unit test web service with Linq

  27. 27

    How to unit test web service with Linq

  28. 28

    How to determine whether a service was stopped in unit test?

  29. 29

    How to mock AngularFire 2 service in unit test?

HotTag

Archive