testing.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*!
  2. * TableSorter QUnit Testing
  3. */
  4. /*jshint unused: true */
  5. /*global QUnit: false, jQuery: false */
  6. jQuery(function($){
  7. var $keyboard = $.keyboard,
  8. compare = function( $key, data ) {
  9. var valid = true;
  10. $.each( data, function(k, v){
  11. if ( valid ) {
  12. switch( k ) {
  13. case 'hasClass':
  14. valid = $key.hasClass( v );
  15. break;
  16. case 'text':
  17. valid = $key.find('span').html() === v;
  18. break;
  19. default:
  20. valid = $key.attr(k) === v;
  21. }
  22. }
  23. });
  24. return valid;
  25. },
  26. runTests = function( kb ) {
  27. QUnit.module('core');
  28. /************************************************
  29. processName
  30. ************************************************/
  31. QUnit.test( 'processName', function(assert) {
  32. var p = kb.processName;
  33. assert.expect(17);
  34. assert.equal( p('a'), 'a', 'Single letter "a"');
  35. assert.equal( p('z'), 'z', 'Single letter "z"');
  36. assert.equal( p('-'), '-', 'Dash "-"'); // previously returned empty string
  37. assert.equal( p('_'), '_', 'Underscore "_"');
  38. assert.equal( p('te'), 'te', 'Two letters "te"');
  39. assert.equal( p('tes'), 'tes', 'Three letters "test"');
  40. assert.equal( p('test'), 'test', 'Four letters "test"');
  41. assert.equal( p('TeSt'), 'TeSt', 'Preserve character case "TeSt"');
  42. assert.equal( p('test_button'), 'test_button', 'Two words, leave underscore "test_button"');
  43. assert.equal( p('\u03b1'), '945', 'Replace non-alphanumeric with character code "\u03b1" -> "945"');
  44. assert.equal( p('\u03b1\u03b2'), '945-946', '"\u03b1\u03b2" -> "945-946"');
  45. assert.equal( p('xy\u03b1\u03b2\u03bez'), 'xy-945-946-958z', '"xy\u03b1\u03b2\u03bez" -> "xy-945-946-958z"');
  46. assert.equal( p('test_x'), 'test_x', '"test_x" -> "test_x"');
  47. assert.equal( p('test-xx'), 'test-xx', '"test-xx" -> "test-xx"');
  48. assert.equal( p('test-\u03b1'), 'test-945', '"test-\u03b1" -> "test-945"');
  49. assert.equal( p('test-\u03b1\u03b2'), 'test-945-946', '"test-\u03b1\u03b2" -> "test-945-946"');
  50. assert.equal( p('test_\u03b1\u03b2'), 'test-945-946', '"test_\u03b1\u03b2" -> "test_-945-946"');
  51. });
  52. /************************************************
  53. processKey
  54. ************************************************/
  55. QUnit.test( 'processKeys', function(assert) {
  56. var k = kb.processKeys;
  57. assert.expect(16);
  58. assert.deepEqual( k(''), { name: '', map: '', title: ''}, 'Empty name');
  59. assert.deepEqual( k('test'), { name: 'test', map: '', title: '' }, 'test');
  60. assert.deepEqual( k('@:this_is_an_at_symbol'), { name: '@', map: '', title: 'this is an at symbol' }, '@:this_is_an_at_symbol');
  61. assert.deepEqual( k(';(q)'), { name: ';', map: 'q', title: '' }, ';(q)');
  62. assert.deepEqual( k('\u03b1(a):lower_case_alpha_(type_a)'), { name: '\u03b1', map: 'a', title: 'lower case alpha (type a)' }, '\u03b1(a):lower_case_alpha_(type_a)');
  63. assert.deepEqual( k('((*)'), { name: '(', map: '*', title: '' }, '((*)');
  64. assert.deepEqual( k(')(()'), { name: ')', map: '(', title: '' }, ')(()');
  65. assert.deepEqual( k(')(():test_(ok)'), { name: ')', map: '(', title: 'test (ok)' }, ')(():test_(ok)');
  66. assert.deepEqual( k('=())'), { name: '=', map: ')', title: '' }, '=())');
  67. assert.deepEqual( k('=()):foo_(bar)'), { name: '=', map: ')', title: 'foo (bar)' }, '=()):foo_(bar)');
  68. assert.deepEqual( k('::bar_(foo)'), { name: ':', map: '', title: 'bar (foo)' }, '::bar_(foo)');
  69. assert.deepEqual( k('x(:)'), { name: 'x', map: ':', title: '' }, 'x(:)');
  70. assert.deepEqual( k(':(:)'), { name: ':', map: ':', title: '' }, ':(:)');
  71. assert.deepEqual( k('x(:):;'), { name: 'x', map: ':', title: ';' }, 'x(:):;');
  72. assert.deepEqual( k(':(:):;'), { name: ':', map: ':', title: ';' }, ':(:):;');
  73. assert.deepEqual( k(':(:):test2_(status:ok)'), { name: ':', map: ':', title: 'test2 (status:ok)' }, ':(:):test2_(status:ok)');
  74. });
  75. /************************************************
  76. customHash
  77. ************************************************/
  78. QUnit.test( 'customHash', function( assert ) {
  79. var layouts = $keyboard.layouts;
  80. assert.expect(3);
  81. // custom hash should not vary over time; so if a default layout changes,
  82. // it will show up here as an error
  83. assert.equal( kb.customHash( layouts.alpha ), '-744693299', 'Hash consistent on alpha layout');
  84. assert.equal( kb.customHash( layouts.qwerty ), '608635341', 'Hash consistent on qwerty layout');
  85. assert.equal( kb.customHash( layouts.num ), '1954922603', 'Hash consistent on num layout');
  86. });
  87. /************************************************
  88. makePreview
  89. ************************************************/
  90. QUnit.test( 'make preview', function( assert ) {
  91. var done = assert.async();
  92. assert.expect(3);
  93. $('#test')
  94. .html('<input id="keyboard_test" type="number" data-test="zzz" data-this-is-a-fake="attr" aria-haspopup="true">')
  95. .find('input')
  96. .keyboard({
  97. layout : 'qwerty',
  98. alwaysOpen: true,
  99. // visible callback *should* work here without a setTimeout, but it isn't for some unknown reason...
  100. initialized: function( e, keyboard ) {
  101. setTimeout(function(){
  102. var el = keyboard.preview,
  103. dataRemoved = typeof el['data-test'] === 'undefined' && typeof el['data-this-is-a-fake'] === 'undefined';
  104. assert.equal( el.type, 'text', 'Preview type changed from number to text' );
  105. assert.equal( typeof el['aria-haspopup'], 'undefined', 'Preview aria-haspopup removed' );
  106. assert.equal( dataRemoved, true, 'Preview data-attributes removed' );
  107. keyboard.destroy();
  108. done();
  109. }, 10);
  110. }
  111. });
  112. });
  113. /************************************************
  114. addKey
  115. ************************************************/
  116. QUnit.test( 'addKey', function(assert) {
  117. var tmp,
  118. k = kb.addKey;
  119. assert.expect(7);
  120. assert.equal( true, compare( k('accept', 'accept'), {
  121. 'data-name': 'accept',
  122. 'data-value': 'accept',
  123. 'title': 'Accept (Shift+Enter)',
  124. 'hasClass': 'ui-keyboard-accept',
  125. 'text': 'Accept'
  126. }),
  127. 'accept key'
  128. );
  129. assert.equal( true, compare( k('accept', 'a'), {
  130. 'data-name': 'accept',
  131. 'data-value': 'accept',
  132. 'title': 'Accept (Shift+Enter)',
  133. 'hasClass': 'ui-keyboard-accept',
  134. 'text': '\u2714'
  135. }),
  136. '(a)ccept key; symbol from display option'
  137. );
  138. tmp = 'meta99';
  139. kb.options.display.meta99 = '\u2660:numbers';
  140. assert.equal( true, compare( k(tmp, tmp), {
  141. 'data-name': 'meta99',
  142. 'data-value': 'meta99',
  143. 'title': 'numbers',
  144. 'hasClass': 'ui-keyboard-meta99',
  145. 'text': '\u2660'
  146. }),
  147. 'meta key testing'
  148. );
  149. tmp = 'meta99:test';
  150. kb.options.display.meta99 = '\u2660:numbers';
  151. assert.equal( true, compare( k(tmp, tmp), {
  152. 'data-name': 'meta99',
  153. 'data-value': 'meta99',
  154. 'title': 'test',
  155. 'hasClass': 'ui-keyboard-meta99',
  156. 'text': '\u2660'
  157. }),
  158. 'in key title overrides display title'
  159. );
  160. tmp = 'meta_testing';
  161. kb.options.display[tmp] = '\u2660:testing_keyboard';
  162. assert.equal( true, compare( k(tmp, tmp), {
  163. 'data-name': tmp,
  164. 'data-value': tmp,
  165. 'title': 'testing keyboard',
  166. 'hasClass': 'ui-keyboard-' + tmp,
  167. 'text': '\u2660'
  168. }),
  169. 'meta key extended name (underscore) testing'
  170. );
  171. tmp = 'meta-testing:testing!';
  172. kb.options.display['meta-testing'] = '\u2660';
  173. assert.equal( true, compare( k(tmp, tmp), {
  174. 'data-name': 'meta-testing',
  175. 'data-value': 'meta-testing',
  176. 'title': 'testing!',
  177. 'hasClass': 'ui-keyboard-meta-testing',
  178. 'text': '\u2660'
  179. }),
  180. 'meta key extended name (dash) testing'
  181. );
  182. tmp = 'xy\u03b1\u03b2\u03bez:test';
  183. assert.equal( true, compare( k(tmp, tmp, true), {
  184. 'data-name': 'xyαβξz',
  185. 'data-value': 'xyαβξz',
  186. 'title': 'test',
  187. 'hasClass': 'ui-keyboard-xy-945-946-958z',
  188. 'text': 'xyαβξz'
  189. }),
  190. 'unicode testing'
  191. );
  192. });
  193. /************************************************
  194. destroy
  195. ************************************************/
  196. QUnit.test( 'destroy method', function( assert ) {
  197. var done = assert.async();
  198. assert.expect(3);
  199. $('#test')
  200. .html('<input type="text" class="testing-abc">')
  201. .find('input')
  202. .keyboard({
  203. layout : 'qwerty',
  204. alwaysOpen: true,
  205. lockInput: true,
  206. // visible callback *should* work here without a setTimeout, but it isn't for some unknown reason...
  207. initialized: function( e, keyboard, el ) {
  208. setTimeout(function(){
  209. keyboard.destroy(function(){
  210. assert.equal( el.className, 'testing-abc', 'Destory removed all added class names' );
  211. assert.equal( $.isEmptyObject( $._data( el ) ), true, 'Destory removed all data & bindings' );
  212. assert.equal( typeof $('#test input').getkeyboard(), 'undefined', 'Cleared up data' );
  213. done();
  214. });
  215. }, 10);
  216. }
  217. });
  218. });
  219. };
  220. $('#input').keyboard({
  221. layout : 'qwerty',
  222. alwaysOpen: true,
  223. initialized: function(e, keyboard) {
  224. runTests( keyboard );
  225. },
  226. visible: function(e, keyboard) {
  227. keyboard.$keyboard.hide();
  228. }
  229. });
  230. });