Best JavaScript code snippet using storybook-root
shortcuts.test.js
Source:shortcuts.test.js
1import { init as initShortcuts } from '../modules/shortcuts';2function createMockStore() {3 let state = {};4 return {5 getState: jest.fn().mockImplementation(() => state),6 setState: jest.fn().mockImplementation((s) => {7 state = { ...state, ...s };8 }),9 };10}11const mockAddonShortcut = {12 addon: 'my-addon',13 shortcut: {14 label: 'Do something',15 defaultShortcut: ['O'],16 actionName: 'doSomething',17 action: () => {18 //19 },20 },21};22const mockAddonSecondShortcut = {23 addon: 'my-addon',24 shortcut: {25 label: 'Do something else',26 defaultShortcut: ['P'],27 actionName: 'doSomethingElse',28 action: () => {29 //30 },31 },32};33const mockSecondAddonShortcut = {34 addon: 'my-other-addon',35 shortcut: {36 label: 'Create issue',37 defaultShortcut: ['N'],38 actionName: 'createIssue',39 action: () => {40 //41 },42 },43};44describe('shortcuts api', () => {45 it('gets defaults', () => {46 const store = createMockStore();47 const { api, state } = initShortcuts({ store });48 store.setState(state);49 expect(api.getDefaultShortcuts()).toHaveProperty('fullScreen', ['F']);50 });51 it('gets defaults including addon ones', async () => {52 const store = createMockStore();53 const { api, state } = initShortcuts({ store });54 store.setState(state);55 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);56 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);57 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);58 expect(api.getDefaultShortcuts()).toHaveProperty('fullScreen', ['F']);59 expect(api.getDefaultShortcuts()).toHaveProperty(60 `${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`,61 mockAddonShortcut.shortcut.defaultShortcut62 );63 expect(api.getDefaultShortcuts()).toHaveProperty(64 `${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`,65 mockAddonSecondShortcut.shortcut.defaultShortcut66 );67 expect(api.getDefaultShortcuts()).toHaveProperty(68 `${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`,69 mockSecondAddonShortcut.shortcut.defaultShortcut70 );71 });72 it('gets addons shortcuts', async () => {73 const store = createMockStore();74 const { api, state } = initShortcuts({ store });75 store.setState(state);76 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);77 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);78 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);79 expect(api.getAddonsShortcuts()).toStrictEqual({80 [`${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`]: mockAddonShortcut.shortcut,81 [`${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`]: mockAddonSecondShortcut.shortcut,82 [`${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`]: mockSecondAddonShortcut.shortcut,83 });84 });85 it('gets addons shortcut labels', async () => {86 const store = createMockStore();87 const { api, state } = initShortcuts({ store });88 store.setState(state);89 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);90 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);91 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);92 expect(api.getAddonsShortcutLabels()).toStrictEqual({93 [`${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`]: mockAddonShortcut94 .shortcut.label,95 [`${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`]: mockAddonSecondShortcut96 .shortcut.label,97 [`${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`]: mockSecondAddonShortcut98 .shortcut.label,99 });100 });101 it('gets addons shortcut defaults', async () => {102 const store = createMockStore();103 const { api, state } = initShortcuts({ store });104 store.setState(state);105 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);106 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);107 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);108 expect(api.getAddonsShortcutDefaults()).toStrictEqual({109 [`${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`]: mockAddonShortcut110 .shortcut.defaultShortcut,111 [`${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`]: mockAddonSecondShortcut112 .shortcut.defaultShortcut,113 [`${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`]: mockSecondAddonShortcut114 .shortcut.defaultShortcut,115 });116 });117 it('sets defaults', () => {118 const store = createMockStore();119 const { api, state } = initShortcuts({ store });120 store.setState(state);121 expect(api.getShortcutKeys().fullScreen).toEqual(['F']);122 });123 it('sets addon shortcut with default value', async () => {124 const store = createMockStore();125 const { api, state } = initShortcuts({ store });126 store.setState(state);127 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);128 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);129 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);130 expect(api.getDefaultShortcuts()).toHaveProperty('fullScreen', ['F']);131 expect(api.getDefaultShortcuts()).toHaveProperty(132 `${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`,133 mockAddonShortcut.shortcut.defaultShortcut134 );135 expect(api.getDefaultShortcuts()).toHaveProperty(136 `${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`,137 mockAddonSecondShortcut.shortcut.defaultShortcut138 );139 expect(api.getDefaultShortcuts()).toHaveProperty(140 `${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`,141 mockSecondAddonShortcut.shortcut.defaultShortcut142 );143 });144 it('sets defaults, augmenting anything that was persisted', () => {145 const store = createMockStore();146 store.setState({ shortcuts: { fullScreen: ['Z'] } });147 const { api, state } = initShortcuts({ store });148 store.setState(state);149 expect(api.getShortcutKeys().fullScreen).toEqual(['Z']);150 expect(api.getShortcutKeys().togglePanel).toEqual(['A']);151 });152 it('sets defaults, ignoring anything persisted that is out of date', () => {153 const store = createMockStore();154 store.setState({ shortcuts: { randomKey: ['Z'] } });155 const { api, state } = initShortcuts({ store });156 store.setState(state);157 expect(api.getShortcutKeys().randomKey).not.toBeDefined();158 });159 it('sets new values', async () => {160 const store = createMockStore();161 const { api, state } = initShortcuts({ store });162 store.setState(state);163 await api.setShortcut('fullScreen', ['X']);164 expect(api.getShortcutKeys().fullScreen).toEqual(['X']);165 });166 it('sets new values for addon shortcuts', async () => {167 const store = createMockStore();168 const { api, state } = initShortcuts({ store });169 store.setState(state);170 const { addon, shortcut } = mockAddonShortcut;171 await api.setAddonShortcut(addon, shortcut);172 await api.setShortcut(`${addon}-${shortcut.actionName}`, ['I']);173 expect(api.getShortcutKeys()[`${addon}-${shortcut.actionName}`]).toEqual(['I']);174 });175 it('restores all defaults', async () => {176 const store = createMockStore();177 const { api, state } = initShortcuts({ store });178 store.setState(state);179 const { addon, shortcut } = mockAddonShortcut;180 await api.setAddonShortcut(addon, shortcut);181 await api.setShortcut('fullScreen', ['X']);182 await api.setShortcut('togglePanel', ['B']);183 await api.setShortcut(`${addon}-${shortcut.actionName}`, ['I']);184 await api.restoreAllDefaultShortcuts();185 expect(api.getShortcutKeys().fullScreen).toEqual(['F']);186 expect(api.getShortcutKeys().togglePanel).toEqual(['A']);187 expect(api.getShortcutKeys()[`${addon}-${shortcut.actionName}`]).toEqual(188 shortcut.defaultShortcut189 );190 });191 it('restores single default', async () => {192 const store = createMockStore();193 const { api, state } = initShortcuts({ store });194 store.setState(state);195 await api.setAddonShortcut(mockAddonShortcut.addon, mockAddonShortcut.shortcut);196 await api.setAddonShortcut(mockAddonSecondShortcut.addon, mockAddonSecondShortcut.shortcut);197 await api.setAddonShortcut(mockSecondAddonShortcut.addon, mockSecondAddonShortcut.shortcut);198 await api.setShortcut('fullScreen', ['X']);199 await api.setShortcut('togglePanel', ['B']);200 await api.setShortcut(`${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`, [201 'I',202 ]);203 await api.setShortcut(204 `${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`,205 ['H']206 );207 await api.setShortcut(208 `${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`,209 ['G']210 );211 await api.restoreDefaultShortcut('fullScreen');212 await api.restoreDefaultShortcut(213 `${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`214 );215 expect(api.getShortcutKeys().fullScreen).toEqual(['F']);216 expect(api.getShortcutKeys().togglePanel).toEqual(['B']);217 expect(218 api.getShortcutKeys()[`${mockAddonShortcut.addon}-${mockAddonShortcut.shortcut.actionName}`]219 ).toEqual(mockAddonShortcut.shortcut.defaultShortcut);220 expect(221 api.getShortcutKeys()[222 `${mockAddonSecondShortcut.addon}-${mockAddonSecondShortcut.shortcut.actionName}`223 ]224 ).toEqual(['H']);225 expect(226 api.getShortcutKeys()[227 `${mockSecondAddonShortcut.addon}-${mockSecondAddonShortcut.shortcut.actionName}`228 ]229 ).toEqual(['G']);230 });...
native-shortcut.ts
Source:native-shortcut.ts
1/**2 * The shape of a shortcut native to Apple devices.3 */4interface AppleShortcut {5 /**6 * `true` if the secondary modifier key for Apple devices, or the Option key,7 * is pressed or needed; `false` otherwise.8 * @remark Note that though this key is the Option key on a physical9 * keyboard, the browser reads it as the Alt key.10 */11 altKey?: boolean;12 /**13 * The code of the non-modifier key being pressed or needed. The code is14 * equivalent to what's returned from `KeyboardEvent.code` for that keypress.15 */16 code: string;17 /**18 * `true` if he primary modifier key for Apple devices, or the Command key,19 * is pressed or needed; `false` otherwise.20 * @remark Note that through this key is the Command key on a physical21 * keyboard, the browser reads it as the Meta key.22 */23 metaKey?: boolean;24 /**25 * `true` if the shift key is pressed or needed; `false` otherwise.26 */27 shiftKey?: boolean;28}29/**30 * The shape of a native shortcut on non-Apple devices.31 */32interface DefaultShortcut {33 /**34 * `true` if the secondary modifier key for non-Apple devices, or the Alt key,35 * is pressed or needed; `false` otherwise.36 */37 altKey?: boolean;38 /**39 * The code of the non-modifier key being pressed or needed. The code is40 * equivalent to what's returned from `KeyboardEvent.code` for that keypress.41 */42 code: string;43 /**44 * `true` if the primary modifier key for non-Apple devices, or the Control45 * key, is pressed or needed; `false` otherwise.46 */47 ctrlKey?: boolean;48 /**49 * `true` if the shift key is pressed or needed; `false` otherwise.50 */51 shiftKey?: boolean;52}53/**54 * A native shortcut can either be an Apple-native shortcut or a PC-native55 * shortcut.56 */57export type NativeShortcut = AppleShortcut | DefaultShortcut;58/**59 * Tests a NativeShortcut to see if it is a valid AppleShortcut.60 * @param nativeShortcut The subject to be tested as an AppleShortcut.61 */62export function isAppleShortcut(63 nativeShortcut: NativeShortcut,64): nativeShortcut is AppleShortcut {65 return !(nativeShortcut as DefaultShortcut).ctrlKey;66}67/**68 * Tests a NativeShortcut to see if it is a valid DefaultShortcut.69 * @param nativeShortcut The subject to be tested as a DefaultShortcut.70 */71export function isDefaultShortcut(72 nativeShortcut: NativeShortcut,73): nativeShortcut is DefaultShortcut {74 return !(nativeShortcut as AppleShortcut).metaKey;...
keyboardShortcuts.ts
Source:keyboardShortcuts.ts
...8 .setFieldKey('defaultShortcut')9 .setSortOrder(undefined)10 .addField('customShortcuts', customShortcut(ArrayXMLField, 'customShortcuts', 'Custom Shortcuts', 42))11 .addField('customShortcut', customShortcut(ObjectXMLField, 'customShortcut', 'Custom Shortcut', minApi, 41))12 .addField('defaultShortcuts', defaultShortcut(ArrayXMLField, 'defaultShortcuts', 'Default Shortcuts', 42))13 .addField('defaultShortcut', defaultShortcut(ObjectXMLField, 'defaultShortcut', 'Default Shortcut', minApi, 41));...
Using AI Code Generation
1import { defaultShortcut } from 'storybook-root';2import { defaultShortcut } from 'storybook-root';3import { defaultShortcut } from 'storybook-root';4import { defaultShortcut } from 'storybook-root';5import { defaultShortcut } from 'storybook-root';6import { defaultShortcut } from 'storybook-root';7import { defaultShortcut } from 'storybook-root';8import { defaultShortcut } from 'storybook-root';9import { defaultShortcut } from 'storybook-root';10import { defaultShortcut } from 'storybook-root';11import { defaultShortcut } from 'storybook-root';12import { defaultShortcut } from 'storybook-root';13import { defaultShortcut } from 'storybook-root';
Using AI Code Generation
1import { defaultShortcut } from 'storybook-root'2import { defaultShortcut } from 'storybook-root'3import { defaultShortcut } from 'storybook-root'4import { defaultShortcut } from 'storybook-root'5import { defaultShortcut } from 'storybook-root'6import { defaultShortcut } from 'storybook-root'7import { defaultShortcut } from 'storybook-root'8import { defaultShortcut } from 'storybook-root'9import { defaultShortcut } from 'storybook-root'10import { defaultShortcut } from 'storybook-root'11import { defaultShortcut } from 'storybook-root'12import { defaultShortcut } from 'storybook-root'13import { defaultShortcut } from 'storybook-root'14import { defaultShortcut } from 'storybook-root'15import { defaultShortcut } from 'storybook-root'16import { defaultShortcut } from 'storybook-root'17import { defaultShortcut } from 'storybook-root'18import { defaultShortcut } from 'storybook-root'19import { defaultShortcut } from 'storybook-root'20import { defaultShortcut } from 'storybook-root'
Using AI Code Generation
1import { defaultShortcut } from '@storybook/addon-storyshots-puppeteer/dist/api';2import { createStorybookUrl } from '@storybook/addon-storyshots-puppeteer/dist/api';3import initStoryshots from '@storybook/addon-storyshots-puppeteer/dist/api';4import { getMatchImageSnapshotMethod } from '@storybook/addon-storyshots-puppeteer/dist/api';5import { multiSnapshotWithOptions } from '@storybook/addon-storyshots-puppeteer/dist/api';6import { renderWithOptions } from '@storybook/addon-storyshots-puppeteer/dist/api';7import { snapshotWithOptions } from '@storybook/addon-storyshots-puppeteer/dist/api';8import { toMatchImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';9import { toMatchSpecificImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';10import { toMatchImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';11import { toMatchSpecificImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';12import { toMatchImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';13import { toMatchSpecificImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';14import { toMatchImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';15import { toMatchSpecificImageSnapshot } from '@storybook/addon-storyshots-puppeteer/dist/api';
Using AI Code Generation
1import { defaultShortcut } from "storybook-root";2defaultShortcut();3import { addDecorator } from "@storybook/react";4import { withShortcuts } from "storybook-addon-shortcuts";5addDecorator(withShortcuts);6export const defaultShortcut = () => {7 console.log("defaultShortcut");8};9import { addons } from "@storybook/addons";10import { withShortcuts } from "storybook-addon-shortcuts";11addons.setConfig({12});13import { withShortcuts } from "storybook-addon-shortcuts";14export const decorators = [withShortcuts];15import { defaultShortcut } from "storybook-root";16export const shortcuts = {17};18import "storybook-addon-shortcuts/register";19| shortcuts | object | {} | Object with key (shortcut) and value (function to execute). |20| defaultShortcut | object | {} | Object with key (shortcut) and value (function to execute). |
Using AI Code Generation
1import { defaultShortcut } from 'storybook-root';2defaultShortcut('command+shift+P', 'Show search box');3import { setShortcut } from 'storybook-root';4setShortcut('command+shift+P', 'Show search box');5import { getShortcut } from 'storybook-root';6import { removeShortcut } from 'storybook-root';7removeShortcut('command+shift+P');8import { getShortcuts } from 'storybook-root';9import { clearShortcuts } from 'storybook-root';10clearShortcuts();11import { getShortcutKeys } from 'storybook-root';12import { getShortcutValues } from 'storybook-root';13import { getShortcuts } from 'storybook-root';14import { clearShortcuts } from 'storybook-root';15clearShortcuts();16import { getShortcutKeys } from 'storybook-root';17import { getShortcutValues } from 'storybook-root';18import { getShortcuts } from 'storybook-root';
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!