keyboard.js 2.5 KB

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