Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(translateService): reset proposed language if there's no pending …
Browse files Browse the repository at this point in the history
…loader
  • Loading branch information
0x-r4bbit committed Aug 26, 2013
1 parent 7f1afb3 commit 6b477fc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
10 changes: 6 additions & 4 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,13 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
translations(key, translationTable);

pendingLoader = false;
$nextLang = undefined;
deferred.resolve(key);
$rootScope.$broadcast('$translateLoadingEnd');
}, function (key) {
$rootScope.$broadcast('$translateLoadingError');
deferred.reject(key);
$nextLang = undefined;
$rootScope.$broadcast('$translateLoadingEnd');
});

Expand Down Expand Up @@ -796,12 +798,12 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
*/
$translate.refresh = function(langKey) {
var deferred = $q.defer();

function onLoadSuccess() {
deferred.resolve();
$rootScope.$broadcast('$translateRefreshEnd');
}

function onLoadFailure() {
deferred.reject();
$rootScope.$broadcast('$translateRefreshEnd');
Expand All @@ -828,7 +830,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if ($uses) {
loaders.push($translate.uses($uses));
}

if (loaders.length > 0) {
$q.all(loaders).then(onLoadSuccess, onLoadFailure);
} else onLoadSuccess();
Expand All @@ -848,7 +850,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
loader.then(onLoadSuccess, onLoadFailure);

} else deferred.reject();

return deferred.promise;
};

Expand Down
62 changes: 34 additions & 28 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,13 @@ describe('pascalprecht.translate', function () {
it('should return proposedLanguage', function () {
expect($translate.proposedLanguage()).toEqual('en');
});

it('should be undefine when no there\'s no pending loader', function () {
inject(function ($timeout) {
$timeout.flush();
expect($translate.proposedLanguage()).toBeUndefined();
});
});
});

describe('$missingTranslationHandlerFactory', function () {
Expand Down Expand Up @@ -1193,10 +1200,10 @@ describe('pascalprecht.translate', function () {
it('should broadcast $translateRefreshEnd event if no lang is given', function() {
inject(function($translate, $rootScope, $timeout) {
spyOn($rootScope, '$broadcast');

$translate.refresh();
$timeout.flush();

expect($rootScope.$broadcast).toHaveBeenCalledWith('$translateRefreshEnd');
});
});
Expand All @@ -1212,10 +1219,10 @@ describe('pascalprecht.translate', function () {
it('should broadcast $translateRefreshEnd event if current lang is given', function() {
inject(function($translate, $rootScope, $timeout) {
spyOn($rootScope, '$broadcast');

$translate.refresh('en');
$timeout.flush();

expect($rootScope.$broadcast).toHaveBeenCalledWith('$translateRefreshEnd');
});
});
Expand All @@ -1231,10 +1238,10 @@ describe('pascalprecht.translate', function () {
it('should broadcast $translateRefreshEnd event if other lang is given', function() {
inject(function($translate, $rootScope, $timeout) {
spyOn($rootScope, '$broadcast');

$translate.refresh('ru');
$timeout.flush();

expect($rootScope.$broadcast).toHaveBeenCalledWith('$translateRefreshEnd');
});
});
Expand Down Expand Up @@ -1354,14 +1361,14 @@ describe('pascalprecht.translate', function () {

});


// Return value
describe('', function() {

beforeEach(module('pascalprecht.translate', function($provide) {
$translateProvider.fallbackLanguage('ru');
}));

it('should return a promise', function() {
inject(function($translate, $timeout) {
var promise = $translate.refresh();
Expand All @@ -1370,7 +1377,7 @@ describe('pascalprecht.translate', function () {
$timeout.flush();
});
});

it('should resolve a promise when refresh is successfully done', function() {
inject(function($translate, $timeout) {
var result;
Expand All @@ -1382,8 +1389,8 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('resolved');
});
});
it('should resolve a promise when refresh of current language is successfully done',

it('should resolve a promise when refresh of current language is successfully done',
function() {
inject(function($translate, $timeout) {
var result;
Expand All @@ -1395,8 +1402,8 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('resolved');
});
});
it('should resolve a promise when refresh of not current language is successfully done',

it('should resolve a promise when refresh of not current language is successfully done',
function() {
inject(function($translate, $timeout) {
var result;
Expand All @@ -1408,11 +1415,11 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('resolved');
});
});

it('should reject a promise when loading of at least one language is failed', function() {
inject(function($translate, $timeout) {
shouldResolve = false;

var result;
$translate.refresh().then(
function() { result = 'resolved'; },
Expand All @@ -1422,11 +1429,11 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('rejected');
});
});

it('should reject a promise when refresh of the current language is failed', function() {
inject(function($translate, $timeout) {
shouldResolve = false;

var result;
$translate.refresh('en').then(
function() { result = 'resolved'; },
Expand All @@ -1436,11 +1443,11 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('rejected');
});
});

it('should reject a promise when refresh of not current language is failed', function() {
inject(function($translate, $timeout) {
shouldResolve = false;

var result;
$translate.refresh('ru').then(
function() { result = 'resolved'; },
Expand All @@ -1450,32 +1457,31 @@ describe('pascalprecht.translate', function () {
expect(result).toEqual('rejected');
});
});

it('should reject a promise if attempting to refresh not existent language', function() {
inject(function($translate, $timeout, $rootScope) {
shouldResolve = false;

var result;
$translate.refresh('ne').then(
function() { result = 'resolved'; },
function() { result = 'rejected'; }
);
try {

try {
$timeout.flush();
} catch (e) {
$rootScope.$digest();
}

expect(result).toEqual('rejected');
});
});

});

});

});

});
});

0 comments on commit 6b477fc

Please sign in to comment.