Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
new tests for locales
  • Loading branch information
evantahler committed Apr 8, 2017
1 parent fdb72ba commit 8b3ebb6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@ log/*
cache/*
coverage/*
node_modules/*
locales/test-env-*
public/javascript/actionheroClient.js
public/javascript/actionheroClient.min.js

Expand Down
5 changes: 4 additions & 1 deletion config/api.js
Expand Up @@ -74,7 +74,10 @@ exports.test = {
'otherRoom': {}
},
paths: {
'locale': [require('os').tmpdir() + require('path').sep + 'locales']
'locale': [
// require('os').tmpdir() + require('path').sep + 'locales',
path.join(__dirname, '/../locales')
]
},
rpcTimeout: 3000
}
Expand Down
8 changes: 8 additions & 0 deletions config/i18n.js
Expand Up @@ -25,3 +25,11 @@ exports['default'] = {
}
}
}

exports.test = {
i18n: function (api) {
return {
updateFiles: true
}
}
}
53 changes: 27 additions & 26 deletions test/core/i18n.js
Expand Up @@ -11,43 +11,47 @@ var ActionheroPrototype = require(path.join(__dirname, '/../../actionhero.js'))
var actionhero = new ActionheroPrototype()
var api

var tmpPath = require('os').tmpdir() + require('path').sep + 'locales' + require('path').sep

var readLocaleFile = (locale) => {
if (!locale) { locale = api.config.i18n.defaultLocale }
var file = api.config.general.paths.locale[0] + '/' + locale + '.json'
var contents = String(fs.readFileSync(file))
var json = JSON.parse(contents)
return json
}

var spanish = {
'Your random number is {{number}}': 'Su número aleatorio es {{number}}',
actionhero: {
errors: {
missingParams: '{{param}} es un parámetro requerido para esta acción',
fileNotFound: 'Ese archivo no se encuentra'
}
}
}

fs.writeFileSync(path.join(__dirname, '/../../locales/test-env-es.json'), JSON.stringify(spanish, null, 2))

describe('Core: i18n', () => {
before((done) => {
// sleep to ensure normal local files are saved to disk
setTimeout(done, 500)
})

before((done) => {
var spanish = {
'Your random number is {{number}}': 'Su número aleatorio es {{number}}',
'That file is not found': 'Ese archivo no se encuentra',
'{{param}} is a required parameter for this action': '{{param}} es un parámetro requerido para esta acción'
}
fs.writeFileSync(tmpPath + 'es.json', JSON.stringify(spanish))

actionhero.start((error, a) => {
expect(error).to.be.null()
api = a
var options = api.config.i18n
options.directory = api.config.general.paths.locale[0]
options.locales = ['en', 'es']
options.locales = ['test-env-en', 'test-env-es']
options.defaultLocale = 'test-env-en'
api.i18n.configure(options)
done()
})
})

after((done) => {
// api.utils.deleteDirectorySync( api.config.general.paths.locale[0] );
// fs.unlinkSync(path.join(__dirname, '/../../locales/test-env-en.json'))
// fs.unlinkSync(path.join(__dirname, '/../../locales/test-env-es.json'))
actionhero.stop(() => {
done()
})
Expand All @@ -57,7 +61,7 @@ describe('Core: i18n', () => {
api.specHelper.runAction('randomNumber', (response) => {
expect(response.randomNumber).to.be.at.most(1)
expect(response.randomNumber).to.be.at.least(0)
var content = readLocaleFile();
var content = readLocaleFile('test-env-en');
[
'Your random number is {{number}}'
].forEach((s) => {
Expand All @@ -67,41 +71,38 @@ describe('Core: i18n', () => {
})
})

// to test this we would need to temporarliy enable logging for the test ENV...
it('should respect the content of the localization files for the server logs')

it('should respect the content of the localization files for generic messages to connections', (done) => {
api.i18n.determineConnectionLocale = () => { return 'en' }
api.i18n.determineConnectionLocale = () => { return 'test-env-en' }
api.specHelper.runAction('randomNumber', (response) => {
expect(response.stringRandomNumber).to.match(/Your random number is/)

api.i18n.determineConnectionLocale = () => { return 'es' }
api.i18n.determineConnectionLocale = () => { return 'test-env-es' }
api.specHelper.runAction('randomNumber', (response) => {
expect(response.stringRandomNumber).to.match(/Su número aleatorio es/)
done()
})
})
})

it('should respect the content of the localization files for api errors to connections', (done) => {
api.i18n.determineConnectionLocale = () => { return 'en' }
it('should respect the content of the localization files for api errors to connections or use defaults', (done) => {
api.i18n.determineConnectionLocale = () => { return 'test-env-en' }
api.specHelper.runAction('cacheTest', (response) => {
expect(response.error).to.match(/key is a required parameter for this action/)
expect(response.error).to.equal('Error: actionhero.errors.missingParams')

api.i18n.determineConnectionLocale = () => { return 'es' }
api.i18n.determineConnectionLocale = () => { return 'test-env-es' }
api.specHelper.runAction('cacheTest', (response) => {
expect(response.error).to.match(/key es un parámetro requerido para esta acción/)
done()
})
})
})

it('should respect the content of the localization files for http errors to connections', (done) => {
api.i18n.determineConnectionLocale = () => { return 'en' }
it('should respect the content of the localization files for http errors to connections or use defaults', (done) => {
api.i18n.determineConnectionLocale = () => { return 'test-env-en' }
api.specHelper.getStaticFile('missing-file.html', (data) => {
expect(data.error).to.match(/That file is not found/)
expect(data.error).to.equal('actionhero.errors.fileNotFound')

api.i18n.determineConnectionLocale = () => { return 'es' }
api.i18n.determineConnectionLocale = () => { return 'test-env-es' }
api.specHelper.getStaticFile('missing-file.html', (data) => {
expect(data.error).to.match(/Ese archivo no se encuentra/)
done()
Expand Down

0 comments on commit 8b3ebb6

Please sign in to comment.