123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- var $ = require('jquery');
- var Options = require('select2/options');
- var Defaults = require('select2/defaults');
-
- module('Options - Translations', {
- beforeEach: function () {
- Defaults.reset();
- },
- afterEach: function () {
- Defaults.reset();
- }
- });
-
- test('partial dictonaries are reset when default reset', function (assert) {
- Defaults.set('language', {
- test: 'testing'
- });
-
- Defaults.reset();
-
- assert.ok(
- !Defaults.defaults.language.test,
- 'The partial dictionary should have been reset'
- );
- });
-
- test('default language chain is English', function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['en']
- );
- });
-
- test(
- 'default translation includes all of the required messages',
- function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- Object.keys(options.get('translations').all()),
- [
- 'errorLoading',
- 'inputTooLong',
- 'inputTooShort',
- 'loadingMore',
- 'maximumSelected',
- 'noResults',
- 'searching',
- 'removeAllItems'
- ]
- );
- }
- );
-
- test('partial dictionaries can be passed', function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({
- language: {
- searching: function () {
- return 'Something';
- }
- }
- }, $element);
-
- var translations = options.get('translations');
-
- assert.equal(
- translations.get('searching')(),
- 'Something',
- 'The partial dictionary still overrides translations'
- );
-
- assert.equal(
- translations.get('noResults')(),
- 'No results found',
- 'You can still get English translations for keys not passed in'
- );
- });
-
- test('partial dictionaries can be combined with defaults', function (assert) {
- var $element = $('<select></select>');
-
- Defaults.set('language', {
- test: function () {
- return 'Testing';
- }
- });
-
- var options = new Options({
- language: {
- searching: function () {
- return 'Something';
- }
- }
- }, $element);
-
- var translations = options.get('translations');
-
- assert.equal(
- translations.get('searching')(),
- 'Something',
- 'The partial dictionary still overrides translations'
- );
-
- assert.equal(
- translations.get('test')(),
- 'Testing',
- 'The defaults were included in the fallback chain'
- );
-
- assert.equal(
- translations.get('noResults')(),
- 'No results found',
- 'You can still get English translations for keys not passed in'
- );
- });
-
- test('partial dictionaries can used in fallback chains', function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({
- language: [
- {
- searching: function () {
- return 'Something';
- }
- },
- {
- test: function () {
- return 'Testing';
- }
- }
- ]
- }, $element);
-
- var translations = options.get('translations');
-
- assert.equal(
- translations.get('searching')(),
- 'Something',
- 'The partial dictionary still overrides translations'
- );
-
- assert.equal(
- translations.get('test')(),
- 'Testing',
- 'The defaults were included in the fallback chain'
- );
-
- assert.equal(
- translations.get('noResults')(),
- 'No results found',
- 'You can still get English translations for keys not passed in'
- );
- });
-
- test('language can be set via the options', function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({
- language: 'es'
- }, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'en']
- );
- });
-
- test('multi-part language is broken out', function (assert) {
- var $element = $('<select></select>');
-
- var options = new Options({
- language: 'pt-BR'
- }, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['pt-BR', 'pt', 'en']
- );
- });
-
- test('default language can be set', function (assert) {
- var $element = $('<select></select>');
-
- Defaults.set('language', 'es');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'en']
- );
- });
-
- test('lanugage set via options adds to default chain', function (assert) {
- var $element = $('<select></select>');
-
- Defaults.set('language', 'es');
-
- var options = new Options({
- language: 'it'
- }, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['it', 'es', 'en']
- );
- });
-
- test('default language chain can be set', function (assert) {
- var $element = $('<select></select>');
-
- Defaults.set('language', ['es', 'it', 'en']);
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'it', 'en']
- );
- });
-
- test('language can be set by lang attr', function (assert) {
- var $element = $('<select lang="es"></select>');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'en']
- );
- });
-
- test('language can be inherited by lang attr', function (assert) {
- var $element = $('<div lang="es"><select></select></div>').find('select');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'en']
- );
- });
-
- test('multi-part language can be inherited by lang attr', function (assert) {
- var $element = $('<div lang="pt-BR"><select></select></div>').find('select');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['pt-BR', 'pt', 'en']
- );
- });
-
- test('lang attr overrides default language', function (assert) {
- var $element = $('<select lang="it"></select>');
-
- Defaults.set('language', 'es');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['it', 'es', 'en']
- );
- });
-
- test('default language overrides inherited lang attr', function (assert) {
- var $element = $('<div lang="it"><select></select></div>').find('select');
-
- Defaults.set('language', 'es');
-
- var options = new Options({}, $element);
-
- assert.deepEqual(
- options.get('language'),
- ['es', 'it', 'en']
- );
- });
|