// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. import { blankIcon, bugDotIcon, bugIcon, CommandToolbarButton, CommandToolbarButtonComponent, jupyterIcon, LabIcon, ReactiveToolbar, ReactWidget, Toolbar, ToolbarButton, ToolbarButtonComponent } from '@jupyterlab/ui-components'; import { framePromise, JupyterServer } from '@jupyterlab/testing'; import { CommandRegistry } from '@lumino/commands'; import type { ReadonlyJSONObject, ReadonlyPartialJSONObject } from '@lumino/coreutils'; import { MessageLoop } from '@lumino/messaging'; import type { PanelLayout } from '@lumino/widgets'; import { Panel, Widget } from '@lumino/widgets'; import * as React from 'react'; import { simulate } from 'simulate-event'; const server = new JupyterServer(); beforeAll(async () => { await server.start(); }, 30000); afterAll(async () => { await server.shutdown(); }); describe('@jupyterlab/ui-components', () => { describe('CommandToolbarButton', () => { let commands: CommandRegistry; const id = 'test-command'; const options: CommandRegistry.ICommandOptions = { execute: jest.fn() }; beforeEach(() => { commands = new CommandRegistry(); }); it('should render a command', async () => { commands.addCommand(id, options); const button = new CommandToolbarButton({ commands, id }); Widget.attach(button, document.body); await framePromise(); expect(button.hasClass('jp-CommandToolbarButton')).toBe(true); simulate(button.node.firstElementChild!, 'click'); expect(options.execute).toHaveBeenCalledTimes(1); }); it('should not trigger command on mousedown', async () => { options.execute = jest.fn(); commands.addCommand(id, options); const button = new CommandToolbarButton({ commands, id }); Widget.attach(button, document.body); await framePromise(); expect(button.hasClass('jp-CommandToolbarButton')).toBe(true); simulate(button.node.firstElementChild!, 'mousedown'); expect(options.execute).toHaveBeenCalledTimes(0); }); it('should trigger command on mousedown', async () => { options.execute = jest.fn(); commands.addCommand(id, options); const button = new CommandToolbarButton({ commands, id, noFocusOnClick: true }); Widget.attach(button, document.body); await framePromise(); expect(button.hasClass('jp-CommandToolbarButton')).toBe(true); simulate(button.node.firstElementChild!, 'mousedown'); expect(options.execute).toHaveBeenCalledTimes(1); }); it('should render the label command', async () => { const label = 'This is a test label'; commands.addCommand(id, { ...options, label }); const button = new CommandToolbarButton({ commands, id }); Widget.attach(button, document.body); await framePromise(); expect(button.node.textContent).toMatch(label); }); it('should render the customized label command', async () => { const label = 'This is a test label'; const buttonLabel = 'This is the button label'; commands.addCommand(id, { ...options, label }); const button = new CommandToolbarButton({ commands, id, label: buttonLabel }); Widget.attach(button, document.body); await framePromise(); expect(button.node.textContent).toMatch(buttonLabel); expect(button.node.textContent).not.toMatch(label); }); it('should render the icon command', async () => { const icon = jupyterIcon; commands.addCommand(id, { ...options, icon }); const button = new CommandToolbarButton({ commands, id }); Widget.attach(button, document.body); await framePromise(); expect(button.node.getElementsByTagName('svg')[0].dataset.icon).toMatch( icon.name ); }); it('should render the customized icon command', async () => { const icon = jupyterIcon; const buttonIcon = blankIcon; commands.addCommand(id, { ...options, icon }); const button = new CommandToolbarButton({ commands, id, icon: buttonIcon }); Widget.attach(button, document.body); await framePromise(); const iconSVG = button.node.getElementsByTagName('svg')[0]; expect(iconSVG.dataset.icon).toMatch(buttonIcon.name); expect(iconSVG.dataset.icon).not.toMatch(icon.name); }); describe('#render()', () => { let enabled = true; let iconClassValue = 'initial-icon-class'; async function render(button: CommandToolbarButton) { button.update(); await framePromise(); await button.renderPromise; } beforeEach(() => { enabled = true; iconClassValue = 'initial-icon-class'; commands.addCommand(id, { execute: () => undefined, label: 'Re-render test', icon: blankIcon, iconClass: () => iconClassValue, isEnabled: () => enabled }); }); it('should not re-render the button when an unrelated command change is notified', async () => { const button = new CommandToolbarButton({ commands, id }); await render(button); // Spy is installed after the initial render so its call count starts // at zero; `resolveReact` is only invoked while rendering the button // subtree, so it is a proxy for "the button re-rendered". const resolveReact = jest.spyOn(LabIcon, 'resolveReact'); try { // A `changed` notification with no underlying state change (the // common case when switching tabs) must not re-render the button. commands.notifyCommandChanged(id); await framePromise(); expect(resolveReact).not.toHaveBeenCalled(); // A `many-changed` notification (also emitted on tab switch) must // likewise be a no-op when nothing relevant changed. commands.notifyCommandChanged(); await framePromise(); expect(resolveReact).not.toHaveBeenCalled(); } finally { resolveReact.mockRestore(); } button.dispose(); }); it('should re-render the button when the command state changes', async () => { const button = new CommandToolbarButton({ commands, id }); await render(button); const resolveReact = jest.spyOn(LabIcon, 'resolveReact'); try { enabled = false; commands.notifyCommandChanged(id); await framePromise(); expect(resolveReact).toHaveBeenCalled(); } finally { resolveReact.mockRestore(); } button.dispose(); }); }); }); describe('CommandToolbarButtonComponent', () => { let commands: CommandRegistry; beforeEach(() => { commands = new CommandRegistry(); }); it('should not execute the command with stale arguments after args change', async () => { // Regression test: when only the command arguments change (the visual // props stay identical), the memoized button must still re-render so // that its click handler dispatches the command with the new args. const execute = jest.fn(); const argsId = 'test:rerender-args'; commands.addCommand(argsId, { execute, label: 'Args test', icon: blankIcon }); class Wrapper extends ReactWidget { args: ReadonlyJSONObject = { value: 1 }; render(): React.ReactElement { return React.createElement(CommandToolbarButtonComponent, { commands, id: argsId, args: this.args }); } } const wrapper = new Wrapper(); Widget.attach(wrapper, document.body); await framePromise(); await wrapper.renderPromise; simulate(wrapper.node.firstElementChild!, 'click'); expect(execute).toHaveBeenLastCalledWith({ value: 1 }); // Change the args (new reference and content); the rendered output of // the button is unchanged, but the handler must pick up the new args. wrapper.args = { value: 2 }; wrapper.update(); await framePromise(); await wrapper.renderPromise; simulate(wrapper.node.firstElementChild!, 'click'); expect(execute).toHaveBeenLastCalledWith({ value: 2 }); wrapper.dispose(); }); it('should execute the command in the new registry after the registry changes', async () => { const registryId = 'test:rerender-registry'; const execute = jest.fn(); commands.addCommand(registryId, { execute, label: 'Registry test', icon: blankIcon }); const otherExecute = jest.fn(); const otherCommands = new CommandRegistry(); otherCommands.addCommand(registryId, { execute: otherExecute, label: 'Registry test', icon: blankIcon }); class Wrapper extends ReactWidget { commands = commands; render(): React.ReactElement { return React.createElement(CommandToolbarButtonComponent, { commands: this.commands, id: registryId }); } } const wrapper = new Wrapper(); Widget.attach(wrapper, document.body); await framePromise(); await wrapper.renderPromise; // The command renders the same button in both registries, but the // handler must run the command in the new registry. wrapper.commands = otherCommands; wrapper.update(); await framePromise(); await wrapper.renderPromise; simulate(wrapper.node.firstElementChild!, 'click'); expect(otherExecute).toHaveBeenCalledTimes(1); expect(execute).not.toHaveBeenCalled(); wrapper.dispose(); }); }); describe('Toolbar', () => { let widget: Toolbar; beforeEach(async () => { widget = new Toolbar(); }, 30000); afterEach(async () => { widget.dispose(); }); describe('#constructor()', () => { it('should construct a new toolbar widget', () => { const widget = new Toolbar(); expect(widget).toBeInstanceOf(Toolbar); }); it('should add the `jp-Toolbar` class', () => { const widget = new Toolbar(); expect(widget.hasClass('jp-Toolbar')).toBe(true); }); }); describe('#names()', () => { it('should get an ordered list the toolbar item names', () => { widget.addItem('foo', new Widget()); widget.addItem('bar', new Widget()); widget.addItem('baz', new Widget()); expect(Array.from(widget.names())).toEqual(['foo', 'bar', 'baz']); }); }); describe('#addItem()', () => { it('should add an item to the toolbar', () => { const item = new Widget(); expect(widget.addItem('test', item)).toBe(true); expect(Array.from(widget.names())).toContain('test'); }); it('should add the `jp-Toolbar-item` class to the widget', () => { const item = new Widget(); widget.addItem('test', item); expect(item.hasClass('jp-Toolbar-item')).toBe(true); }); it('should return false if the name is already used', () => { widget.addItem('test', new Widget()); expect(widget.addItem('test', new Widget())).toBe(false); }); }); describe('#insertItem()', () => { it('should insert the item into the toolbar', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); widget.insertItem(1, 'c', new Widget()); expect(Array.from(widget.names())).toEqual(['a', 'c', 'b']); }); it('should clamp the bounds', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); widget.insertItem(10, 'c', new Widget()); expect(Array.from(widget.names())).toEqual(['a', 'b', 'c']); }); }); describe('#insertAfter()', () => { it('should insert an item into the toolbar after `c`', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); widget.insertItem(1, 'c', new Widget()); widget.insertAfter('c', 'd', new Widget()); expect(Array.from(widget.names())).toEqual(['a', 'c', 'd', 'b']); }); it('should return false if the target item does not exist', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); const value = widget.insertAfter('c', 'd', new Widget()); expect(value).toBe(false); }); }); describe('#insertBefore()', () => { it('should insert an item into the toolbar before `c`', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); widget.insertItem(1, 'c', new Widget()); widget.insertBefore('c', 'd', new Widget()); expect(Array.from(widget.names())).toEqual(['a', 'd', 'c', 'b']); }); it('should return false if the target item does not exist', () => { widget.addItem('a', new Widget()); widget.addItem('b', new Widget()); const value = widget.insertBefore('c', 'd', new Widget()); expect(value).toBe(false); }); }); describe('#handleEvent()', () => { let parent: Panel; let button: Widget; beforeEach(() => { parent = new Panel(); parent.addWidget(widget); button = new Widget({ node: document.createElement('button') }); widget.addItem('button', button); Widget.attach(parent, document.body); }); afterEach(() => { parent.dispose(); }); it.each(['pointerdown', 'keydown'])( 'should not activate the parent when a click after %s moves the focus to a menu', eventType => { const menu = document.createElement('div'); menu.tabIndex = -1; document.body.appendChild(menu); button.node.addEventListener('click', () => menu.focus()); const activate = jest.spyOn(parent, 'activate'); button.node.focus(); button.node.dispatchEvent(new Event(eventType, { bubbles: true })); simulate(button.node, 'click'); expect(activate).not.toHaveBeenCalled(); menu.remove(); } ); it('should activate the parent when a click on empty space leaves the focus on the body', () => { const input = new Widget({ node: document.createElement('input') }); parent.addWidget(input); const activate = jest.spyOn(parent, 'activate'); input.node.focus(); widget.node.dispatchEvent(new Event('pointerdown', { bubbles: true })); // A press on an element that cannot take focus moves the focus to the body. input.node.blur(); simulate(widget.node, 'click'); expect(activate).toHaveBeenCalled(); }); }); describe('.createFromCommand', () => { const commands = new CommandRegistry(); const testLogCommandId = 'test:toolbar-log'; const logArgs: ReadonlyPartialJSONObject[] = []; let enabled = false; let toggled = true; let visible = false; commands.addCommand(testLogCommandId, { execute: args => { logArgs.push(args); }, label: 'Test log command label', caption: 'Test log command caption', usage: 'Test log command usage', iconClass: 'test-icon-class', className: 'test-log-class', isEnabled: () => enabled, isToggled: () => toggled, isVisible: () => visible }); async function render(button: CommandToolbarButton) { button.update(); await framePromise(); expect(button.renderPromise).toBeDefined(); await button.renderPromise; } it('should create a button', () => { const button = new CommandToolbarButton({ commands, id: testLogCommandId }); expect(button).toBeInstanceOf(CommandToolbarButton); button.dispose(); }); it('should add main class', async () => { const button = new CommandToolbarButton({ commands, id: testLogCommandId }); await render(button); const buttonNode = button.node.firstChild as HTMLButtonElement; expect(buttonNode.classList.contains('test-log-class')).toBe(true); button.dispose(); }); it('should add an icon with icon class and label', async () => { const button = new CommandToolbarButton({ commands, id: testLogCommandId }); await render(button); const buttonNode = button.node.firstChild as HTMLButtonElement; expect(buttonNode.title).toBe('Test log command caption'); const iconNode = buttonNode.firstChild as HTMLElement; expect(iconNode.classList.contains('test-icon-class')).toBe(true); button.dispose(); }); it('should apply state classes', async () => { enabled = false; toggled = true; visible = false; const button = new CommandToolbarButton({ commands, id: testLogCommandId }); await render(button); const buttonNode = button.node.firstChild as HTMLButtonElement; expect(buttonNode.disabled).toBe(true); expect(buttonNode.classList.contains('lm-mod-toggled')).toBe(true); expect(buttonNode.getAttribute('aria-pressed')).toBe('true'); expect(buttonNode.classList.contains('lm-mod-hidden')).toBe(true); button.dispose(); }); it('should update state classes', async () => { enabled = false; toggled = true; visible = false; const button = new CommandToolbarButton({ commands, id: testLogCommandId }); await render(button); const buttonNode = button.node.firstChild as HTMLButtonElement; expect(buttonNode.disabled).toBe(true); expect(buttonNode.classList.contains('lm-mod-toggled')).toBe(true); expect(buttonNode.getAttribute('aria-pressed')).toBe('true'); expect(buttonNode.classList.contains('lm-mod-hidden')).toBe(true); enabled = true; visible = true; commands.notifyCommandChanged(testLogCommandId); await framePromise(); await button.renderPromise; expect(buttonNode.disabled).toBe(false); expect(buttonNode.classList.contains('lm-mod-toggled')).toBe(true); expect(buttonNode.getAttribute('aria-pressed')).toBe('true'); expect(buttonNode.classList.contains('lm-mod-hidden')).toBe(false); enabled = false; visible = false; button.dispose(); }); it('should update widget visibility on many-changed signal (GH-17697)', async () => { const button = new CommandToolbarButton({ commands, id: testLogCommandId }); // Mutate shared describe-scope variables; restore them in finally so // a mid-test assertion failure cannot corrupt subsequent tests. // Initial values come from the describe-scope declarations: // let enabled = false; let toggled = true; let visible = false; visible = false; toggled = false; enabled = true; try { await render(button); // The parent widget node (not the inner