Skip to content

Commit

Permalink
fix: Prevent duplicate PanelSnap instances on the same container
Browse files Browse the repository at this point in the history
  • Loading branch information
guidobouman committed Apr 14, 2018
1 parent 9bcb296 commit cf4ea6a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/panelSnap.js
Expand Up @@ -28,9 +28,13 @@ export default class PanelSnap {
};

this.container = this.options.container;
if (this.container.dataset.panelsnapId) {
throw new Error('PanelSnap is already initialised on this container, aborting.');
}

INSTANCE_COUNTER += 1;
this.instanceIndex = INSTANCE_COUNTER;
this.container.dataset.panelsnapId = this.instanceIndex;

this.scrollingContainer = getScrollingElement(this.container);
this.panelList = document.querySelectorAll(`[data-panelsnap-id="${this.instanceIndex}"] ${this.options.panelSelector}`);
Expand Down
14 changes: 14 additions & 0 deletions src/panelSnap.test.js
Expand Up @@ -5,4 +5,18 @@ describe('Constructor', () => {
const instance = new PanelSnap();
expect(instance).toBeInstanceOf(PanelSnap);
});

test('prevents duplicate init of panelSnap on the same element', () => {
const container = document.createElement('div');
// eslint-disable-next-line no-unused-vars
const firstInstance = new PanelSnap({ container });
expect(() => new PanelSnap({ container })).toThrow('already initialised');
});

test('prevents duplicate init of panelSnap on the same element', () => {
const container = document.createElement('div');
// eslint-disable-next-line no-unused-vars
const firstInstance = new PanelSnap({ container });
expect(() => new PanelSnap({ container })).toThrow('already initialised');
});
});

0 comments on commit cf4ea6a

Please sign in to comment.