we are using karma and mocha for unit-testing in angularjs. But we are having problems with testing our $locale stuff, like formats of dates.
We have three test, is the $locale recreated for each test or not ? And if i use services in those test, are my services also reset or is it possible that test 1 affects test 2 because my service contained some data ?
Here a code snippet:
it 'should format currency in german', ->
inject (localizer, $filter) ->
localizer 'de'
expect($filter('currency') 5125, '€').to.be.equal '5.125,00 €'
expect($filter('currency') 5, '€').to.be.equal '5,00 €'
expect($filter('currency') .51, '€').to.be.equal '0,51 €'
expect($filter('currency') 0.515, '€').to.be.equal '0,52 €'
it 'should format date in german', ->
inject (localizer, $filter) ->
localizer 'de'
myDate = new Date 2015,5,22
expect($filter('date') myDate,'shortDate').to.be.equal '22.06.15'
app.service 'localizer', ($locale, locales) -> (currentLocale) ->
console.log("Seting currentLocale to:"+currentLocale+"| lastLocale:"+lastLocale)
if currentLocale isnt lastLocale
console.log("currentLocale is now:"+currentLocale)
lastLocale = currentLocale
localeData = locales[currentLocale]
angular.extend $locale.DATETIME_FORMATS, localeData.DATETIME_FORMATS
angular.extend $locale.NUMBER_FORMATS, localeData.NUMBER_FORMATS
The problem is, if after a test with 'de' is again a test with 'de' in the $locale are the english formating values. If i put a localizer 'tr' before the second localizer 'de' it works. Why are the values of $locae are changing? Is it possible to A) prevent $locale from changing or B) to reset my service before each test.