Skip to content

Commit

Permalink
Test (utils): adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-stv committed Aug 4, 2023
1 parent 915f8f8 commit 4d20dbf
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions packages/ckeditor5-utils/tests/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ describe( 'Keyboard', () => {

describe( 'parseKeystroke', () => {
const initialEnvMac = env.isMac;
const initialEnviOS = env.isiOS;

afterEach( () => {
env.isMac = initialEnvMac;
env.isiOS = initialEnviOS;
} );

describe( 'on Macintosh', () => {
Expand Down Expand Up @@ -134,6 +136,50 @@ describe( 'Keyboard', () => {
} );
} );

describe( 'on iOS', () => {
beforeEach( () => {
env.isiOS = true;
} );

it( 'parses string', () => {
expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x880000 + 65 );
} );

it( 'parses string without modifier', () => {
expect( parseKeystroke( '[' ) ).to.equal( 91 );
} );

it( 'allows spacing', () => {
expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x880000 + 65 );
} );

it( 'is case-insensitive', () => {
expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x880000 + 65 );
} );

it( 'works with an array', () => {
expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x880000 + 65 );
} );

it( 'works with an array which contains numbers', () => {
expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
} );

it( 'works with two modifiers', () => {
expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x880000 + 0x220000 + 65 );
} );

it( 'supports forced modifier', () => {
expect( parseKeystroke( 'ctrl!+a' ) ).to.equal( 0x110000 + 65 );
} );

it( 'throws on unknown name', () => {
expectToThrowCKEditorError( () => {
parseKeystroke( 'foo' );
}, 'keyboard-unknown-key', null );
} );
} );

describe( 'on non–Macintosh', () => {
beforeEach( () => {
env.isMac = false;
Expand Down Expand Up @@ -181,9 +227,11 @@ describe( 'Keyboard', () => {

describe( 'getEnvKeystrokeText', () => {
const initialEnvMac = env.isMac;
const initialEnviOS = env.isiOS;

afterEach( () => {
env.isMac = initialEnvMac;
env.isiOS = initialEnviOS;
} );

describe( 'on Macintosh', () => {
Expand Down Expand Up @@ -232,6 +280,52 @@ describe( 'Keyboard', () => {
} );
} );

describe( 'on iOS', () => {
beforeEach( () => {
env.isiOS = true;
} );

it( 'replaces CTRL with ⌘', () => {
expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
} );

it( 'replaces CTRL! with ⌃', () => {
expect( getEnvKeystrokeText( 'CTRL!' ) ).to.equal( '⌃' );
expect( getEnvKeystrokeText( 'CTRL!+A' ) ).to.equal( '⌃A' );
expect( getEnvKeystrokeText( 'ctrl!+A' ) ).to.equal( '⌃A' );
} );

it( 'replaces SHIFT with ⇧', () => {
expect( getEnvKeystrokeText( 'SHIFT' ) ).to.equal( '⇧' );
expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( '⇧A' );
expect( getEnvKeystrokeText( 'shift+A' ) ).to.equal( '⇧A' );
} );

it( 'replaces ALT with ⌥', () => {
expect( getEnvKeystrokeText( 'ALT' ) ).to.equal( '⌥' );
expect( getEnvKeystrokeText( 'ALT+A' ) ).to.equal( '⌥A' );
expect( getEnvKeystrokeText( 'alt+A' ) ).to.equal( '⌥A' );
} );

it( 'work for multiple modifiers', () => {
expect( getEnvKeystrokeText( 'CTRL+SHIFT+X' ) ).to.equal( '⌘⇧X' );
expect( getEnvKeystrokeText( 'ALT+SHIFT+X' ) ).to.equal( '⌥⇧X' );
} );

it( 'normalizes value', () => {
expect( getEnvKeystrokeText( 'ESC' ) ).to.equal( 'Esc' );
expect( getEnvKeystrokeText( 'TAB' ) ).to.equal( 'Tab' );
expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
expect( getEnvKeystrokeText( 'a' ) ).to.equal( 'A' );
expect( getEnvKeystrokeText( 'CTRL+a' ) ).to.equal( '⌘A' );
expect( getEnvKeystrokeText( 'ctrl+b' ) ).to.equal( '⌘B' );
expect( getEnvKeystrokeText( 'CTRL+[' ) ).to.equal( '⌘[' );
expect( getEnvKeystrokeText( 'CTRL+]' ) ).to.equal( '⌘]' );
} );
} );

describe( 'on non–Macintosh', () => {
beforeEach( () => {
env.isMac = false;
Expand Down

0 comments on commit 4d20dbf

Please sign in to comment.