Best JavaScript code snippet using storybook-root
shortcuts.test.js
Source:shortcuts.test.js
...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');...
register-shortcuts.ts
Source:register-shortcuts.ts
...7 reset: ToolbarShortcutConfig & { action: () => void };8}9export const registerShortcuts = async (api: API, id: string, shortcuts: Shortcuts) => {10 if (shortcuts && shortcuts.next) {11 await api.setAddonShortcut(ADDON_ID, {12 label: shortcuts.next.label,13 defaultShortcut: shortcuts.next.keys,14 actionName: `${id}:next`,15 action: shortcuts.next.action,16 });17 }18 if (shortcuts && shortcuts.previous) {19 await api.setAddonShortcut(ADDON_ID, {20 label: shortcuts.previous.label,21 defaultShortcut: shortcuts.previous.keys,22 actionName: `${id}:previous`,23 action: shortcuts.previous.action,24 });25 }26 if (shortcuts && shortcuts.reset) {27 await api.setAddonShortcut(ADDON_ID, {28 label: shortcuts.reset.label,29 defaultShortcut: shortcuts.reset.keys,30 actionName: `${id}:reset`,31 action: shortcuts.reset.action,32 });33 }...
Using AI Code Generation
1import { setAddonShortcut } from 'storybook-root';2setAddonShortcut('my-addon', 'alt+shift+M');3import { setAddonShortcut } from 'storybook-root';4setAddonShortcut('my-addon', 'alt+shift+M');5import { setAddonShortcut } from 'storybook-root';6setAddonShortcut('my-addon', 'alt+shift+M');7import { setAddonShortcut } from 'storybook-root';8setAddonShortcut('my-addon', 'alt+shift+M');9import { setAddonShortcut } from 'storybook-root';10setAddonShortcut('my-addon', 'alt+shift+M');11import { setAddonShortcut } from 'storybook-root';12setAddonShortcut('my-addon', 'alt+shift+M');13import { setAddonShortcut } from 'storybook-root';14setAddonShortcut('my-addon', 'alt+shift+M');15import { setAddonShortcut } from 'storybook-root';16setAddonShortcut('my-addon', 'alt+shift+M');
Using AI Code Generation
1export const setAddonShortcut = (addonShortcut) => {2}3export const setAddonShortcut = (addonShortcut) => {4}5export const setAddonShortcut = (addonShortcut) => {6}7export const setAddonShortcut = (addonShortcut) => {8}9export const setAddonShortcut = (addonShortcut) => {10}11export const setAddonShortcut = (addonShortcut) => {12}13export const setAddonShortcut = (addonShortcut) => {14}15export const setAddonShortcut = (addonShortcut) => {16}17export const setAddonShortcut = (addonShortcut) => {18}19export const setAddonShortcut = (addonShortcut) => {20}21export const setAddonShortcut = (addonShortcut) => {22}23export const setAddonShortcut = (addonShortcut) => {24}25export const setAddonShortcut = (addonShortcut) => {26}27export const setAddonShortcut = (addonShortcut) => {28}29export const setAddonShortcut = (addonShortcut) => {30}31export const setAddonShortcut = (addonShortcut) => {32}
Using AI Code Generation
1const storybookRoot = require('./storybook-root');2storybookRoot.setAddonShortcut('addon-name', 'a');3const addonShortcut = {};4exports.setAddonShortcut = (addonName, shortcut) => {5 addonShortcut[addonName] = shortcut;6};7const storybookRoot = require('./storybook-root');8const addonShortcut = storybookRoot.getAddonShortcut();9console.log(addonShortcut);
Using AI Code Generation
1import { setAddonShortcut } from './storybook-root';2import { ADDON_EVENT } from './constants';3describe('setAddonShortcut', () => {4 const dispatchEvent = jest.fn();5 const addEventListener = jest.fn((event, callback) => {6 callback({ dispatchEvent });7 });8 const window = {9 };10 const shortcut = 'shortcut';11 it('should dispatch the addon event', () => {12 setAddonShortcut(window, shortcut);13 expect(dispatchEvent).toHaveBeenCalledWith(14 expect.objectContaining({15 detail: {16 },17 })18 );19 });20});21import { ADDON_EVENT } from './constants';22export const setAddonShortcut = (window, shortcut) => {23 window.addEventListener('keydown', ({ ctrlKey, altKey, key }) => {24 if (ctrlKey && altKey && key === shortcut) {25 window.dispatchEvent(26 new CustomEvent(ADDON_EVENT, {27 detail: {28 },29 })30 );31 }32 });33};34export const ADDON_EVENT = 'storybook-addon-event';35"dependencies": {36}37module.exports = {38};39import { addParameters } from '@storybook/react';40import { jsxDecorator } from 'storybook-addon-jsx';41addParameters({42 jsx: {
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!!