question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing Angular views using translateStaticFilesLoader

See original GitHub issue

Dear all,

I would like to unit test my view which contains translate filters.

The following loads the view and displays the key of my translation, but not its actual real value.

  it('should load the view', inject(function($rootScope, _$templateCache_, _$compile_, $translate, $translateStaticFilesLoader, $httpBackend, $translationCache) {
    $httpBackend.when('GET', 'locale-fr.json').respond({
      "vehicule": "Véhicule"
    });
    $httpBackend.when('GET', 'locale-de.json').respond({
      "vehicule": "Fahrzeug"
    });
    $httpBackend.expectGET('locale-fr.json');
    $translateStaticFilesLoader({
      key: 'fr',
      prefix: 'locale-',
      suffix: '.json'
    });
    $httpBackend.flush();

    var tpl = _$templateCache_.get("/detail.tpl.html");
    var scope = $rootScope.$new();
    var view = _$compile_(angular.element(tpl))(scope);
    scope.$digest();
    console.log(view.find('th'));
  }));

My view contains:

<th>{{ 'vehicule' | translate }}</th>

The expected ouput of the console should be Véhicule but it is vehicule.

Would you have any idea on how to provide my keys to the view?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
knallicommented, Dec 27, 2015

@sovattha Well, you can reduce the complexity of this case to the thing want to test: your template. Instead of retesting angular-translate’s async loading process, I would recommend

  • a static set of translations
  • no XHR mock tooling: no $httpBackend

So, using copy-paste of your code of the op:

describe('', function () {
  beforeEach(module('pascalprecht.translate', function ($translateProvider) {
    $translateProvider.translations('fr', {
      "vehicule": "Véhicule"
    });
    $translateProvider.translations('de', {
      "vehicule": "Fahrzeug"
    });
    $translateProvider.preferredLanguage('fr');
  }));

  it('should load the view', inject(function($rootScope, _$templateCache_, _$compile_, $translate, $translationCache) {
    var tpl = _$templateCache_.get("/detail.tpl.html");
    var scope = $rootScope.$new();
    var view = _$compile_(angular.element(tpl))(scope);
    scope.$digest();
    console.log(view.find('th'));
  }));
});
0reactions
sovatthacommented, Dec 27, 2015

Thank you knalli, yes in a way, you are right. I try to test too much things in the same test. I will follow your guideline. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing - Angular
The testing documentation offers tips and techniques for unit and integration testing Angular applications through a sample application created with the ...
Read more >
How do unit test with angular-translate - Stack Overflow
I concur with hugo, testing code that uses callable angular services in general (not only $translate but also $timeout and $interval) is pretty...
Read more >
Angular unit testing 101 (with examples) - DEV Community ‍ ‍
Angular automated testing. Skeleton of a test. Using the three Jasmine APIs mentioned above, a skeleton of a unit test should look like...
Read more >
Unit Testing Angular Services, HTTP Calls and HTTP ...
This Angular 10 tutorial will provide you with enough knowledge on setting up a test file to unit test a service as well...
Read more >
Unit testing Angular with Jest tutorial - YouTube
Learn how to unit test an Angular project in Jest in just a few simple steps.(Sorry about the font size, it was my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found