This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): use same fallback for module.run when no storage key is…
- Loading branch information
Showing
2 changed files
with
115 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
describe('pascalprecht.translate', function () { | ||
|
||
describe('$translateModule#run', function () { | ||
|
||
describe('with no storage', function() { | ||
|
||
describe('and no preferred language', function() { | ||
|
||
beforeEach(module('pascalprecht.translate')); | ||
|
||
it('should set used locale to undefined', inject(function($translate) { | ||
expect($translate.use()).toBeUndefined(); | ||
})); | ||
}); | ||
|
||
describe('but with a preferred language', function() { | ||
|
||
beforeEach(module('pascalprecht.translate', function ($translateProvider) { | ||
$translateProvider.preferredLanguage('en'); | ||
})); | ||
|
||
it('should use preferredLanguage', inject(function($translate) { | ||
expect($translate.use()).toEqual('en'); | ||
})); | ||
}); | ||
}); | ||
|
||
describe('with a storage', function() { | ||
|
||
var storage; | ||
var storageMockCreator = function(storageValue) { | ||
var storage = jasmine.createSpyObj('storage', ['get', 'set']); | ||
storage.get.andReturn(storageValue); | ||
return storage; | ||
}; | ||
|
||
describe('containing value', function() { | ||
|
||
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) { | ||
storage = storageMockCreator('en'); | ||
$provide.value('storageMock', storage); | ||
|
||
$translateProvider | ||
.useStorage('storageMock') | ||
.preferredLanguage('de'); | ||
})); | ||
|
||
it('should use value from storage when provided', inject(function($translate) { | ||
expect(storage.get).toHaveBeenCalled(); | ||
expect($translate.use()).toEqual('en'); | ||
})); | ||
}); | ||
|
||
describe('but not containing any value', function() { | ||
|
||
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) { | ||
storage = storageMockCreator(undefined); | ||
$provide.value('storageMock', storage); | ||
|
||
$translateProvider | ||
.useStorage('storageMock') | ||
.preferredLanguage('de'); | ||
})); | ||
|
||
it('should fallback to preferred locale', inject(function($translate) { | ||
expect(storage.get).toHaveBeenCalled(); | ||
expect($translate.use()).toEqual('de'); | ||
})); | ||
}); | ||
|
||
describe('but with no loadable translations', function() { | ||
|
||
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) { | ||
storage = storageMockCreator('en'); | ||
$provide.value('storageMock', storage); | ||
$provide.factory('loaderMock', function($q) { | ||
return function(options) { | ||
if(options.key === 'en') { | ||
return $q.reject('en'); | ||
} else if(options.key === 'de') | ||
return $q.when({ | ||
key: 'de', | ||
table: {} | ||
}); | ||
} | ||
}); | ||
|
||
$translateProvider | ||
.useStorage('storageMock') | ||
.useLoader('loaderMock') | ||
.preferredLanguage('de'); | ||
})); | ||
|
||
it('should fallback to preferred locale', inject(function($translate, $rootScope) { | ||
$rootScope.$digest(); | ||
expect(storage.get).toHaveBeenCalled(); | ||
expect($translate.use()).toEqual('de'); | ||
})); | ||
}); | ||
}); | ||
}); | ||
}); |