keyboard.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { keyCodes, getCode, parseKeystroke } from '/ckeditor5/utils/keyboard.js';
  6. describe( 'Keyboard', () => {
  7. describe( 'keyCodes', () => {
  8. it( 'contains numbers', () => {
  9. expect( keyCodes[ '0' ] ).to.equal( 48 );
  10. expect( keyCodes[ '9' ] ).to.equal( 57 );
  11. } );
  12. it( 'contains letters', () => {
  13. expect( keyCodes.a ).to.equal( 65 );
  14. expect( keyCodes.z ).to.equal( 90 );
  15. } );
  16. it( 'modifiers and other keys', () => {
  17. expect( keyCodes.delete ).to.equal( 46 );
  18. expect( keyCodes.ctrl ).to.equal( 0x110000 );
  19. expect( keyCodes.cmd ).to.equal( 0x110000 );
  20. } );
  21. } );
  22. describe( 'getCode', () => {
  23. it( 'gets code of a number', () => {
  24. expect( getCode( '0' ) ).to.equal( 48 );
  25. } );
  26. it( 'gets code of a letter', () => {
  27. expect( getCode( 'a' ) ).to.equal( 65 );
  28. } );
  29. it( 'is case insensitive', () => {
  30. expect( getCode( 'A' ) ).to.equal( 65 );
  31. expect( getCode( 'Ctrl' ) ).to.equal( 0x110000 );
  32. expect( getCode( 'ENTER' ) ).to.equal( 13 );
  33. } );
  34. it( 'throws when passed unknown key name', () => {
  35. expect( () => {
  36. getCode( 'foo' );
  37. } ).to.throwCKEditorError( /^keyboard-unknown-key:/ );
  38. } );
  39. it( 'gets code of a keystroke info', () => {
  40. expect( getCode( { keyCode: 48 } ) ).to.equal( 48 );
  41. } );
  42. it( 'adds modifiers to the keystroke code', () => {
  43. expect( getCode( { keyCode: 48, altKey: true, ctrlKey: true, shiftKey: true } ) )
  44. .to.equal( 48 + 0x110000 + 0x220000 + 0x440000 );
  45. } );
  46. } );
  47. describe( 'parseKeystroke', () => {
  48. it( 'parses string', () => {
  49. expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x110000 + 65 );
  50. } );
  51. it( 'allows spacing', () => {
  52. expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x110000 + 65 );
  53. } );
  54. it( 'is case-insensitive', () => {
  55. expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x110000 + 65 );
  56. } );
  57. it( 'works with an array', () => {
  58. expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x110000 + 65 );
  59. } );
  60. it( 'works with an array which contains numbers', () => {
  61. expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
  62. } );
  63. it( 'works with two modifiers', () => {
  64. expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x110000 + 0x220000 + 65 );
  65. } );
  66. it( 'throws on unknown name', () => {
  67. expect( () => {
  68. parseKeystroke( 'foo' );
  69. } ).to.throwCKEditorError( /^keyboard-unknown-key:/ );
  70. } );
  71. } );
  72. } );