pulsar/spec/gutter-container-spec.js

89 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
const Gutter = require('../src/gutter');
const GutterContainer = require('../src/gutter-container');
describe('GutterContainer', () => {
2019-05-31 19:33:56 +03:00
let gutterContainer = null;
const fakeTextEditor = {
2019-05-31 19:33:56 +03:00
scheduleComponentUpdate() {}
};
beforeEach(() => {
2019-05-31 19:33:56 +03:00
gutterContainer = new GutterContainer(fakeTextEditor);
});
describe('when initialized', () =>
it('it has no gutters', () => {
2019-05-31 19:33:56 +03:00
expect(gutterContainer.getGutters().length).toBe(0);
}));
describe('::addGutter', () => {
it('creates a new gutter', () => {
2019-02-22 10:55:17 +03:00
const newGutter = gutterContainer.addGutter({
'test-gutter': 'test-gutter',
priority: 1
2019-05-31 19:33:56 +03:00
});
expect(gutterContainer.getGutters()).toEqual([newGutter]);
expect(newGutter.priority).toBe(1);
});
it('throws an error if the provided gutter name is already in use', () => {
2019-05-31 19:33:56 +03:00
const name = 'test-gutter';
gutterContainer.addGutter({ name });
expect(gutterContainer.addGutter.bind(null, { name })).toThrow();
});
it('keeps added gutters sorted by ascending priority', () => {
2019-05-31 19:33:56 +03:00
const gutter1 = gutterContainer.addGutter({ name: 'first', priority: 1 });
const gutter3 = gutterContainer.addGutter({ name: 'third', priority: 3 });
const gutter2 = gutterContainer.addGutter({
name: 'second',
priority: 2
});
expect(gutterContainer.getGutters()).toEqual([gutter1, gutter2, gutter3]);
});
});
describe('::removeGutter', () => {
2019-05-31 19:33:56 +03:00
let removedGutters;
2019-05-31 19:33:56 +03:00
beforeEach(function() {
gutterContainer = new GutterContainer(fakeTextEditor);
removedGutters = [];
2019-02-22 10:55:17 +03:00
gutterContainer.onDidRemoveGutter(gutterName =>
removedGutters.push(gutterName)
2019-05-31 19:33:56 +03:00
);
});
it('removes the gutter if it is contained by this GutterContainer', () => {
2019-05-31 19:33:56 +03:00
const gutter = gutterContainer.addGutter({
'test-gutter': 'test-gutter'
});
expect(gutterContainer.getGutters()).toEqual([gutter]);
gutterContainer.removeGutter(gutter);
expect(gutterContainer.getGutters().length).toBe(0);
expect(removedGutters).toEqual([gutter.name]);
});
it('throws an error if the gutter is not within this GutterContainer', () => {
2019-05-31 19:33:56 +03:00
const fakeOtherTextEditor = {};
const otherGutterContainer = new GutterContainer(fakeOtherTextEditor);
const gutter = new Gutter('gutter-name', otherGutterContainer);
expect(gutterContainer.removeGutter.bind(null, gutter)).toThrow();
});
});
describe('::destroy', () =>
it('clears its array of gutters and destroys custom gutters', () => {
2019-02-22 10:55:17 +03:00
const newGutter = gutterContainer.addGutter({
'test-gutter': 'test-gutter',
priority: 1
2019-05-31 19:33:56 +03:00
});
const newGutterSpy = jasmine.createSpy();
newGutter.onDidDestroy(newGutterSpy);
2019-05-31 19:33:56 +03:00
gutterContainer.destroy();
expect(newGutterSpy).toHaveBeenCalled();
expect(gutterContainer.getGutters()).toEqual([]);
}));
});