pulsar/spec/panel-container-element-spec.js

274 lines
8.4 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
'use strict';
2017-03-08 01:52:32 +03:00
2019-05-31 19:33:56 +03:00
const Panel = require('../src/panel');
const PanelContainer = require('../src/panel-container');
2017-03-08 01:30:33 +03:00
2017-03-08 01:52:32 +03:00
describe('PanelContainerElement', () => {
2019-05-31 19:33:56 +03:00
let jasmineContent, element, container;
2019-02-22 10:55:17 +03:00
class TestPanelContainerItem {}
2017-03-08 01:30:33 +03:00
class TestPanelContainerItemElement_ extends HTMLElement {
connectedCallback() {
2019-05-31 19:33:56 +03:00
this.classList.add('test-root');
}
2019-05-31 19:33:56 +03:00
initialize(model) {
this.model = model;
return this;
}
2019-05-31 19:33:56 +03:00
focus() {}
}
window.customElements.define(
2017-03-08 01:52:32 +03:00
'atom-test-container-item-element',
TestPanelContainerItemElement_
);
const TestPanelContainerItemElement = document.createElement(
'atom-test-container-item-element'
2019-05-31 19:33:56 +03:00
);
2017-03-08 01:52:32 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
jasmineContent = document.body.querySelector('#jasmine-content');
2019-02-22 10:55:17 +03:00
atom.views.addViewProvider(TestPanelContainerItem, model =>
TestPanelContainerItemElement.initialize(model)
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
container = new PanelContainer({
viewRegistry: atom.views,
location: 'left'
2019-05-31 19:33:56 +03:00
});
element = container.getElement();
jasmineContent.appendChild(element);
});
2017-03-08 01:52:32 +03:00
it('has a location class with value from the model', () => {
2019-05-31 19:33:56 +03:00
expect(element).toHaveClass('left');
});
2017-03-08 01:52:32 +03:00
it('removes the element when the container is destroyed', () => {
2019-05-31 19:33:56 +03:00
expect(element.parentNode).toBe(jasmineContent);
container.destroy();
expect(element.parentNode).not.toBe(jasmineContent);
});
2017-03-08 01:52:32 +03:00
describe('adding and removing panels', () => {
it('allows panels to be inserted at any position', () => {
2019-02-22 10:55:17 +03:00
const panel1 = new Panel(
{ item: new TestPanelContainerItem(), priority: 10 },
atom.views
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
const panel2 = new Panel(
{ item: new TestPanelContainerItem(), priority: 5 },
atom.views
2019-05-31 19:33:56 +03:00
);
2019-02-22 10:55:17 +03:00
const panel3 = new Panel(
{ item: new TestPanelContainerItem(), priority: 8 },
atom.views
2019-05-31 19:33:56 +03:00
);
2019-05-31 19:33:56 +03:00
container.addPanel(panel1);
container.addPanel(panel2);
container.addPanel(panel3);
2019-05-31 19:33:56 +03:00
expect(element.childNodes[2]).toBe(panel1.getElement());
expect(element.childNodes[1]).toBe(panel3.getElement());
expect(element.childNodes[0]).toBe(panel2.getElement());
});
2017-03-08 01:30:33 +03:00
describe('when the container is at the left location', () =>
2017-03-08 01:52:32 +03:00
it('adds atom-panel elements when a new panel is added to the container; removes them when the panels are destroyed', () => {
2019-05-31 19:33:56 +03:00
expect(element.childNodes.length).toBe(0);
2019-02-22 10:55:17 +03:00
const panel1 = new Panel(
{ item: new TestPanelContainerItem() },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel1);
expect(element.childNodes.length).toBe(1);
expect(element.childNodes[0]).toHaveClass('left');
expect(element.childNodes[0]).toHaveClass('tool-panel'); // legacy selector support
expect(element.childNodes[0]).toHaveClass('panel-left'); // legacy selector support
2019-05-31 19:33:56 +03:00
expect(element.childNodes[0].tagName).toBe('ATOM-PANEL');
2019-02-22 10:55:17 +03:00
const panel2 = new Panel(
{ item: new TestPanelContainerItem() },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel2);
expect(element.childNodes.length).toBe(2);
2019-05-31 19:33:56 +03:00
expect(panel1.getElement().style.display).not.toBe('none');
expect(panel2.getElement().style.display).not.toBe('none');
2019-05-31 19:33:56 +03:00
panel1.destroy();
expect(element.childNodes.length).toBe(1);
2019-05-31 19:33:56 +03:00
panel2.destroy();
expect(element.childNodes.length).toBe(0);
}));
2017-03-08 01:52:32 +03:00
describe('when the container is at the bottom location', () => {
beforeEach(() => {
2019-02-22 10:55:17 +03:00
container = new PanelContainer({
viewRegistry: atom.views,
location: 'bottom'
2019-05-31 19:33:56 +03:00
});
element = container.getElement();
jasmineContent.appendChild(element);
});
2017-03-08 01:52:32 +03:00
it('adds atom-panel elements when a new panel is added to the container; removes them when the panels are destroyed', () => {
2019-05-31 19:33:56 +03:00
expect(element.childNodes.length).toBe(0);
2019-02-22 10:55:17 +03:00
const panel1 = new Panel(
{ item: new TestPanelContainerItem(), className: 'one' },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel1);
expect(element.childNodes.length).toBe(1);
expect(element.childNodes[0]).toHaveClass('bottom');
expect(element.childNodes[0]).toHaveClass('tool-panel'); // legacy selector support
expect(element.childNodes[0]).toHaveClass('panel-bottom'); // legacy selector support
expect(element.childNodes[0].tagName).toBe('ATOM-PANEL');
expect(panel1.getElement()).toHaveClass('one');
2019-02-22 10:55:17 +03:00
const panel2 = new Panel(
{ item: new TestPanelContainerItem(), className: 'two' },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel2);
expect(element.childNodes.length).toBe(2);
expect(panel2.getElement()).toHaveClass('two');
2019-05-31 19:33:56 +03:00
panel1.destroy();
expect(element.childNodes.length).toBe(1);
2019-05-31 19:33:56 +03:00
panel2.destroy();
expect(element.childNodes.length).toBe(0);
});
});
});
2017-03-08 01:52:32 +03:00
describe('when the container is modal', () => {
beforeEach(() => {
2019-02-22 10:55:17 +03:00
container = new PanelContainer({
viewRegistry: atom.views,
location: 'modal'
2019-05-31 19:33:56 +03:00
});
element = container.getElement();
jasmineContent.appendChild(element);
});
2017-03-08 01:52:32 +03:00
it('allows only one panel to be visible at a time', () => {
2019-02-22 10:55:17 +03:00
const panel1 = new Panel(
{ item: new TestPanelContainerItem() },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel1);
2019-05-31 19:33:56 +03:00
expect(panel1.getElement().style.display).not.toBe('none');
2019-02-22 10:55:17 +03:00
const panel2 = new Panel(
{ item: new TestPanelContainerItem() },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel2);
2019-05-31 19:33:56 +03:00
expect(panel1.getElement().style.display).toBe('none');
expect(panel2.getElement().style.display).not.toBe('none');
2019-05-31 19:33:56 +03:00
panel1.show();
2019-05-31 19:33:56 +03:00
expect(panel1.getElement().style.display).not.toBe('none');
expect(panel2.getElement().style.display).toBe('none');
});
2017-03-08 01:52:32 +03:00
it("adds the 'modal' class to panels", () => {
2019-02-22 10:55:17 +03:00
const panel1 = new Panel(
{ item: new TestPanelContainerItem() },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel1);
2019-05-31 19:33:56 +03:00
expect(panel1.getElement()).toHaveClass('modal');
// legacy selector support
2019-05-31 19:33:56 +03:00
expect(panel1.getElement()).not.toHaveClass('tool-panel');
expect(panel1.getElement()).toHaveClass('overlay');
expect(panel1.getElement()).toHaveClass('from-top');
});
2019-02-22 10:55:17 +03:00
describe('autoFocus', () => {
2019-05-31 19:33:56 +03:00
function createPanel(autoFocus = true) {
2017-08-12 08:23:35 +03:00
const panel = new Panel(
{
item: new TestPanelContainerItem(),
2019-01-16 07:18:19 +03:00
autoFocus: autoFocus,
2017-08-12 08:23:35 +03:00
visible: false
},
atom.views
2019-05-31 19:33:56 +03:00
);
2017-08-12 08:23:35 +03:00
2019-05-31 19:33:56 +03:00
container.addPanel(panel);
return panel;
2017-08-12 08:23:35 +03:00
}
2019-02-22 10:55:17 +03:00
it('focuses the first tabbable item if available', () => {
2019-05-31 19:33:56 +03:00
const panel = createPanel();
const panelEl = panel.getElement();
const inputEl = document.createElement('input');
2017-08-12 08:23:35 +03:00
2019-05-31 19:33:56 +03:00
panelEl.appendChild(inputEl);
expect(document.activeElement).not.toBe(inputEl);
2017-08-14 19:56:43 +03:00
2019-05-31 19:33:56 +03:00
panel.show();
expect(document.activeElement).toBe(inputEl);
2022-08-25 15:44:49 +03:00
panel.destroy();
2019-05-31 19:33:56 +03:00
});
it('focuses the autoFocus element if available', () => {
2019-05-31 19:33:56 +03:00
const inputEl1 = document.createElement('input');
const inputEl2 = document.createElement('input');
const panel = createPanel(inputEl2);
const panelEl = panel.getElement();
2019-01-16 07:18:19 +03:00
2019-05-31 19:33:56 +03:00
panelEl.appendChild(inputEl1);
panelEl.appendChild(inputEl2);
expect(document.activeElement).not.toBe(inputEl2);
2019-01-16 07:18:19 +03:00
2019-05-31 19:33:56 +03:00
panel.show();
expect(document.activeElement).toBe(inputEl2);
2022-08-25 15:44:49 +03:00
panel.destroy();
2019-05-31 19:33:56 +03:00
});
2019-01-16 07:18:19 +03:00
2019-02-22 10:55:17 +03:00
it('focuses the entire panel item when no tabbable item is available and the panel is focusable', () => {
2019-05-31 19:33:56 +03:00
const panel = createPanel();
const panelEl = panel.getElement();
2019-05-31 19:33:56 +03:00
spyOn(panelEl, 'focus');
panel.show();
expect(panelEl.focus).toHaveBeenCalled();
2022-08-25 15:44:49 +03:00
panel.destroy()
2019-05-31 19:33:56 +03:00
});
2019-02-22 10:55:17 +03:00
it('returns focus to the original activeElement', () => {
2019-05-31 19:33:56 +03:00
const panel = createPanel();
const previousActiveElement = document.activeElement;
const panelEl = panel.getElement();
panelEl.appendChild(document.createElement('input'));
2019-05-31 19:33:56 +03:00
panel.show();
panel.hide();
2019-05-31 19:33:56 +03:00
waitsFor(() => document.activeElement === previousActiveElement);
2017-08-12 08:23:35 +03:00
runs(() => {
2019-05-31 19:33:56 +03:00
expect(document.activeElement).toBe(previousActiveElement);
});
});
});
});
});