UNPKG

translator

Version:

[ABANDONED] Translator for node and also for browser

181 lines (172 loc) 7.81 kB
// Generated by CoffeeScript 1.6.3 (function() { var Api, api, backup, expect, fs, path; expect = require('chai').expect; fs = require('fs'); path = require('path'); Api = require('../../../lib/Api'); api = null; backup = {}; describe('Api', function() { beforeEach(function() { var paths, _i, _len, _path, _results; api = new Api('../../data/config.json'); paths = [api.configPath, path.join(api.config.path, './web/pages/homepage/en.promo.json')]; _results = []; for (_i = 0, _len = paths.length; _i < _len; _i++) { _path = paths[_i]; _results.push(backup[_path] = fs.readFileSync(_path, { encoding: 'utf8' })); } return _results; }); afterEach(function() { var data, _path, _results; _results = []; for (_path in backup) { data = backup[_path]; _results.push(fs.writeFileSync(_path, data)); } return _results; }); describe('#getLanguages()', function() { return it('should get all used languages', function() { return expect(api.getLanguages()).to.have.members(['en', 'cs', 'sk']); }); }); describe('#hasLanguage()', function() { it('should return true if language exists', function() { return expect(api.hasLanguage('cs')).to.be["true"]; }); return it('should return true if language does not exists', function() { return expect(api.hasLanguage('fr')).to.be["false"]; }); }); describe('#addLanguage()', function() { it('should not do anything if language already exists', function() { api.addLanguage('cs'); api.release(); return expect(api.getLanguages()).to.have.members(['en', 'cs', 'sk']); }); return it('should add new language', function() { api.addLanguage('fr'); api.release(); return expect(api.getLanguages()).to.have.members(['en', 'cs', 'sk', 'fr']); }); }); describe('#getDictionaries()', function() { return it('should get list of all dictionaries for selected language', function() { return expect(api.getDictionaries()).to.be.eql({ first: {}, web: { pages: { homepage: { cached: {}, promo: {}, simple: {} } } } }); }); }); describe('#addDictionary()', function() { it('should throw an error if dictionary already exists', function() { return expect(function() { return api.addDictionary('web.pages.homepage.simple'); }).to["throw"](Error, "Dictionary 'web.pages.homepage.simple' already exists."); }); return it('should create new dictionary', function() { var _path; _path = path.join(api.config.path, './web/pages/homepage/en.newDictionary.json'); api.addDictionary('web.pages.homepage.newDictionary'); expect(api.getDictionaries().web.pages.homepage).to.have.keys(['simple', 'cached', 'promo', 'newDictionary']); expect(fs.readFileSync(_path, { encoding: 'utf8' })).to.be.equal('{}'); return fs.unlinkSync(_path); }); }); describe('#renameDictionary()', function() { it('should throw an error if source dictionary does not exists', function() { return expect(function() { return api.renameDictionary('unknown.dictionary', 'new.name'); }).to["throw"](Error, "Dictionary 'unknown.dictionary' does not exists."); }); return it('should throw an error if target dictionary already exists', function() { return expect(function() { return api.renameDictionary('web.pages.homepage.promo', 'web.pages.homepage.simple'); }).to["throw"](Error, "Dictionary 'web.pages.homepage.simple' already exists."); }); }); describe('#removeDictionary()', function() { it('should throw an error if source dictionary does not exists', function() { return expect(function() { return api.removeDictionary('unknown.dictionary'); }).to["throw"](Error, "Dictionary 'unknown.dictionary' does not exists."); }); return it('should remove dictionary', function() { api.removeDictionary('web.pages.homepage.promo'); return expect(api.getDictionaries().web.pages.homepage).to.have.keys(['simple', 'cached']); }); }); describe('#getTranslations()', function() { return it('should load all translations in dictionary', function() { return expect(api.getTranslations('web.pages.homepage.simple')).to.be.eql({ title: ['Title of promo box'] }); }); }); describe('#addTranslation()', function() { it('should throw an error if translation already exists', function() { return expect(function() { return api.addTranslation('web.pages.homepage.promo', 'title', 'New title'); }).to["throw"](Error, "Translation 'title' already exists in 'web.pages.homepage.promo' dictionary."); }); return it('should add new translation', function() { var translations; api.addTranslation('web.pages.homepage.promo', 'subtitle', 'New subtitle'); translations = api.getTranslations('web.pages.homepage.promo'); expect(translations).to.have.keys('title', 'info', 'list', 'cars', 'mobile', 'fruits', 'keys', 'values', 'advanced', 'newList', 'subtitle'); return expect(translations.subtitle).to.be.equal('New subtitle'); }); }); describe('#editTranslation()', function() { it('should throw an error if translation does not exists', function() { return expect(function() { return api.editTranslation('web.pages.homepage.promo', 'subtitle', 'New subtitle'); }).to["throw"](Error, "Translation 'subtitle' does not exists in 'web.pages.homepage.promo' dictionary."); }); return it('should change translation', function() { api.editTranslation('web.pages.homepage.promo', 'title', 'New title'); return expect(api.getTranslations('web.pages.homepage.promo').title).to.be.eql('New title'); }); }); describe('#renameTranslation()', function() { it('should throw an error if source translation does not exists', function() { return expect(function() { return api.renameTranslation('web.pages.homepage.promo', 'subtitle', '_subtitle'); }).to["throw"](Error, "Translation 'subtitle' does not exists in 'web.pages.homepage.promo' dictionary."); }); it('should throw an error if target translation already exists', function() { return expect(function() { return api.renameTranslation('web.pages.homepage.promo', 'title', 'info'); }).to["throw"](Error, "Translation 'info' already exists in 'web.pages.homepage.promo' dictionary."); }); return it('should rename translation', function() { var translations; api.renameTranslation('web.pages.homepage.promo', 'title', '_title'); translations = api.getTranslations('web.pages.homepage.promo'); expect(translations).to.have.keys(['_title', 'info', 'list', 'cars', 'mobile', 'fruits', 'keys', 'values', 'advanced', 'newList']); return expect(translations._title).to.be.eql(['Title of promo box']); }); }); return describe('#removeTranslation()', function() { return it('should remove translation', function() { api.removeTranslation('web.pages.homepage.promo', 'title'); return expect(api.getTranslations('web.pages.homepage.promo')).to.have.keys('info', 'list', 'cars', 'mobile', 'fruits', 'keys', 'values', 'advanced', 'newList'); }); }); }); }).call(this);