No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

translation-tests.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. var $ = require('jquery');
  2. var Options = require('select2/options');
  3. var Defaults = require('select2/defaults');
  4. module('Options - Translations', {
  5. beforeEach: function () {
  6. Defaults.reset();
  7. },
  8. afterEach: function () {
  9. Defaults.reset();
  10. }
  11. });
  12. test('partial dictonaries are reset when default reset', function (assert) {
  13. Defaults.set('language', {
  14. test: 'testing'
  15. });
  16. Defaults.reset();
  17. assert.ok(
  18. !Defaults.defaults.language.test,
  19. 'The partial dictionary should have been reset'
  20. );
  21. });
  22. test('default language chain is English', function (assert) {
  23. var $element = $('<select></select>');
  24. var options = new Options({}, $element);
  25. assert.deepEqual(
  26. options.get('language'),
  27. ['en']
  28. );
  29. });
  30. test(
  31. 'default translation includes all of the required messages',
  32. function (assert) {
  33. var $element = $('<select></select>');
  34. var options = new Options({}, $element);
  35. assert.deepEqual(
  36. Object.keys(options.get('translations').all()),
  37. [
  38. 'errorLoading',
  39. 'inputTooLong',
  40. 'inputTooShort',
  41. 'loadingMore',
  42. 'maximumSelected',
  43. 'noResults',
  44. 'searching',
  45. 'removeAllItems'
  46. ]
  47. );
  48. }
  49. );
  50. test('partial dictionaries can be passed', function (assert) {
  51. var $element = $('<select></select>');
  52. var options = new Options({
  53. language: {
  54. searching: function () {
  55. return 'Something';
  56. }
  57. }
  58. }, $element);
  59. var translations = options.get('translations');
  60. assert.equal(
  61. translations.get('searching')(),
  62. 'Something',
  63. 'The partial dictionary still overrides translations'
  64. );
  65. assert.equal(
  66. translations.get('noResults')(),
  67. 'No results found',
  68. 'You can still get English translations for keys not passed in'
  69. );
  70. });
  71. test('partial dictionaries can be combined with defaults', function (assert) {
  72. var $element = $('<select></select>');
  73. Defaults.set('language', {
  74. test: function () {
  75. return 'Testing';
  76. }
  77. });
  78. var options = new Options({
  79. language: {
  80. searching: function () {
  81. return 'Something';
  82. }
  83. }
  84. }, $element);
  85. var translations = options.get('translations');
  86. assert.equal(
  87. translations.get('searching')(),
  88. 'Something',
  89. 'The partial dictionary still overrides translations'
  90. );
  91. assert.equal(
  92. translations.get('test')(),
  93. 'Testing',
  94. 'The defaults were included in the fallback chain'
  95. );
  96. assert.equal(
  97. translations.get('noResults')(),
  98. 'No results found',
  99. 'You can still get English translations for keys not passed in'
  100. );
  101. });
  102. test('partial dictionaries can used in fallback chains', function (assert) {
  103. var $element = $('<select></select>');
  104. var options = new Options({
  105. language: [
  106. {
  107. searching: function () {
  108. return 'Something';
  109. }
  110. },
  111. {
  112. test: function () {
  113. return 'Testing';
  114. }
  115. }
  116. ]
  117. }, $element);
  118. var translations = options.get('translations');
  119. assert.equal(
  120. translations.get('searching')(),
  121. 'Something',
  122. 'The partial dictionary still overrides translations'
  123. );
  124. assert.equal(
  125. translations.get('test')(),
  126. 'Testing',
  127. 'The defaults were included in the fallback chain'
  128. );
  129. assert.equal(
  130. translations.get('noResults')(),
  131. 'No results found',
  132. 'You can still get English translations for keys not passed in'
  133. );
  134. });
  135. test('language can be set via the options', function (assert) {
  136. var $element = $('<select></select>');
  137. var options = new Options({
  138. language: 'es'
  139. }, $element);
  140. assert.deepEqual(
  141. options.get('language'),
  142. ['es', 'en']
  143. );
  144. });
  145. test('multi-part language is broken out', function (assert) {
  146. var $element = $('<select></select>');
  147. var options = new Options({
  148. language: 'pt-BR'
  149. }, $element);
  150. assert.deepEqual(
  151. options.get('language'),
  152. ['pt-BR', 'pt', 'en']
  153. );
  154. });
  155. test('default language can be set', function (assert) {
  156. var $element = $('<select></select>');
  157. Defaults.set('language', 'es');
  158. var options = new Options({}, $element);
  159. assert.deepEqual(
  160. options.get('language'),
  161. ['es', 'en']
  162. );
  163. });
  164. test('lanugage set via options adds to default chain', function (assert) {
  165. var $element = $('<select></select>');
  166. Defaults.set('language', 'es');
  167. var options = new Options({
  168. language: 'it'
  169. }, $element);
  170. assert.deepEqual(
  171. options.get('language'),
  172. ['it', 'es', 'en']
  173. );
  174. });
  175. test('default language chain can be set', function (assert) {
  176. var $element = $('<select></select>');
  177. Defaults.set('language', ['es', 'it', 'en']);
  178. var options = new Options({}, $element);
  179. assert.deepEqual(
  180. options.get('language'),
  181. ['es', 'it', 'en']
  182. );
  183. });
  184. test('language can be set by lang attr', function (assert) {
  185. var $element = $('<select lang="es"></select>');
  186. var options = new Options({}, $element);
  187. assert.deepEqual(
  188. options.get('language'),
  189. ['es', 'en']
  190. );
  191. });
  192. test('language can be inherited by lang attr', function (assert) {
  193. var $element = $('<div lang="es"><select></select></div>').find('select');
  194. var options = new Options({}, $element);
  195. assert.deepEqual(
  196. options.get('language'),
  197. ['es', 'en']
  198. );
  199. });
  200. test('multi-part language can be inherited by lang attr', function (assert) {
  201. var $element = $('<div lang="pt-BR"><select></select></div>').find('select');
  202. var options = new Options({}, $element);
  203. assert.deepEqual(
  204. options.get('language'),
  205. ['pt-BR', 'pt', 'en']
  206. );
  207. });
  208. test('lang attr overrides default language', function (assert) {
  209. var $element = $('<select lang="it"></select>');
  210. Defaults.set('language', 'es');
  211. var options = new Options({}, $element);
  212. assert.deepEqual(
  213. options.get('language'),
  214. ['it', 'es', 'en']
  215. );
  216. });
  217. test('default language overrides inherited lang attr', function (assert) {
  218. var $element = $('<div lang="it"><select></select></div>').find('select');
  219. Defaults.set('language', 'es');
  220. var options = new Options({}, $element);
  221. assert.deepEqual(
  222. options.get('language'),
  223. ['es', 'it', 'en']
  224. );
  225. });