pulsar/spec/panel-container-spec.js

161 lines
5.3 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:52:32 +03:00
describe('PanelContainer', () => {
2019-05-31 19:33:56 +03:00
let container;
2019-02-22 10:55:17 +03:00
class TestPanelItem {}
2017-03-08 01:52:32 +03:00
beforeEach(() => {
2019-05-31 19:33:56 +03:00
container = new PanelContainer({ viewRegistry: atom.views });
});
2017-03-08 01:52:32 +03:00
describe('::addPanel(panel)', () => {
it('emits an onDidAddPanel event with the index the panel was inserted at', () => {
2019-05-31 19:33:56 +03:00
const addPanelSpy = jasmine.createSpy();
container.onDidAddPanel(addPanelSpy);
2019-05-31 19:33:56 +03:00
const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(panel1);
expect(addPanelSpy).toHaveBeenCalledWith({ panel: panel1, index: 0 });
2019-05-31 19:33:56 +03:00
const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(panel2);
expect(addPanelSpy).toHaveBeenCalledWith({ panel: panel2, index: 1 });
});
});
2017-03-08 01:52:32 +03:00
describe('when a panel is destroyed', () => {
it('emits an onDidRemovePanel event with the index of the removed item', () => {
2019-05-31 19:33:56 +03:00
const removePanelSpy = jasmine.createSpy();
container.onDidRemovePanel(removePanelSpy);
2019-05-31 19:33:56 +03:00
const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(panel1);
const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(panel2);
2019-05-31 19:33:56 +03:00
expect(removePanelSpy).not.toHaveBeenCalled();
2019-05-31 19:33:56 +03:00
panel2.destroy();
expect(removePanelSpy).toHaveBeenCalledWith({ panel: panel2, index: 1 });
2019-05-31 19:33:56 +03:00
panel1.destroy();
expect(removePanelSpy).toHaveBeenCalledWith({ panel: panel1, index: 0 });
});
});
2017-03-08 01:52:32 +03:00
describe('::destroy()', () => {
it('destroys the container and all of its panels', () => {
2019-05-31 19:33:56 +03:00
const destroyedPanels = [];
2019-05-31 19:33:56 +03:00
const panel1 = new Panel({ item: new TestPanelItem() }, atom.views);
2019-02-22 10:55:17 +03:00
panel1.onDidDestroy(() => {
2019-05-31 19:33:56 +03:00
destroyedPanels.push(panel1);
});
container.addPanel(panel1);
2019-05-31 19:33:56 +03:00
const panel2 = new Panel({ item: new TestPanelItem() }, atom.views);
2019-02-22 10:55:17 +03:00
panel2.onDidDestroy(() => {
2019-05-31 19:33:56 +03:00
destroyedPanels.push(panel2);
});
container.addPanel(panel2);
2019-05-31 19:33:56 +03:00
container.destroy();
2019-05-31 19:33:56 +03:00
expect(container.getPanels().length).toBe(0);
expect(destroyedPanels).toEqual([panel1, panel2]);
});
});
2017-03-08 01:52:32 +03:00
describe('panel priority', () => {
describe('left / top panel container', () => {
2019-05-31 19:33:56 +03:00
let initialPanel;
2017-03-08 01:52:32 +03:00
beforeEach(() => {
// 'left' logic is the same as 'top'
2019-05-31 19:33:56 +03:00
container = new PanelContainer({ location: 'left' });
initialPanel = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(initialPanel);
});
2017-03-08 01:52:32 +03:00
describe('when a panel with low priority is added', () => {
it('is inserted at the beginning of the list', () => {
2019-05-31 19:33:56 +03:00
const addPanelSpy = jasmine.createSpy();
container.onDidAddPanel(addPanelSpy);
2019-02-22 10:55:17 +03:00
const panel = new Panel(
{ item: new TestPanelItem(), priority: 0 },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel);
2017-03-08 01:30:33 +03:00
2019-05-31 19:33:56 +03:00
expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 0 });
expect(container.getPanels()[0]).toBe(panel);
});
});
2017-03-08 01:52:32 +03:00
describe('when a panel with priority between two other panels is added', () => {
it('is inserted at the between the two panels', () => {
2019-05-31 19:33:56 +03:00
const addPanelSpy = jasmine.createSpy();
2019-02-22 10:55:17 +03:00
let panel = new Panel(
{ item: new TestPanelItem(), priority: 1000 },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel);
2019-05-31 19:33:56 +03:00
container.onDidAddPanel(addPanelSpy);
2019-02-22 10:55:17 +03:00
panel = new Panel(
{ item: new TestPanelItem(), priority: 101 },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel);
2019-05-31 19:33:56 +03:00
expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 1 });
expect(container.getPanels()[1]).toBe(panel);
});
});
});
2017-03-08 01:52:32 +03:00
describe('right / bottom panel container', () => {
2019-05-31 19:33:56 +03:00
let initialPanel;
2017-03-08 01:52:32 +03:00
beforeEach(() => {
// 'bottom' logic is the same as 'right'
2019-05-31 19:33:56 +03:00
container = new PanelContainer({ location: 'right' });
initialPanel = new Panel({ item: new TestPanelItem() }, atom.views);
container.addPanel(initialPanel);
});
2017-03-08 01:52:32 +03:00
describe('when a panel with high priority is added', () => {
it('is inserted at the beginning of the list', () => {
2019-05-31 19:33:56 +03:00
const addPanelSpy = jasmine.createSpy();
container.onDidAddPanel(addPanelSpy);
2019-02-22 10:55:17 +03:00
const panel = new Panel(
{ item: new TestPanelItem(), priority: 1000 },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel);
2017-03-08 01:30:33 +03:00
2019-05-31 19:33:56 +03:00
expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 0 });
expect(container.getPanels()[0]).toBe(panel);
});
});
2017-03-08 01:52:32 +03:00
describe('when a panel with low priority is added', () => {
it('is inserted at the end of the list', () => {
2019-05-31 19:33:56 +03:00
const addPanelSpy = jasmine.createSpy();
container.onDidAddPanel(addPanelSpy);
2019-02-22 10:55:17 +03:00
const panel = new Panel(
{ item: new TestPanelItem(), priority: 0 },
atom.views
2019-05-31 19:33:56 +03:00
);
container.addPanel(panel);
expect(addPanelSpy).toHaveBeenCalledWith({ panel, index: 1 });
expect(container.getPanels()[1]).toBe(panel);
});
});
});
});
});