Skip to content

Commit

Permalink
Chore: Add safety check in util.replaceHeader() (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum authored Jan 30, 2018
1 parent 217fbce commit 3a47c2e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/lib/PreviewUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,18 @@ class PreviewUI {
* @return {void}
*/
replaceHeader(replacementHeader) {
const headerToShow = this.container.querySelector(replacementHeader);
if (!headerToShow) {
return;
}

// First hide all possible headers
const headers = this.container.querySelectorAll(`.${CLASS_BOX_PREVIEW_HEADER}`);
[].forEach.call(headers, (header) => {
header.classList.add(CLASS_HIDDEN);
});

// Show the specified header
const headerToShow = this.container.querySelector(replacementHeader);
headerToShow.classList.remove(CLASS_HIDDEN);
}

Expand Down
22 changes: 16 additions & 6 deletions src/lib/__tests__/PreviewUI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,29 @@ describe('lib/PreviewUI', () => {


describe('replaceHeader()', () => {
it('should hide all headers and then show the specified header', () => {
const newHeader = document.createElement('div');

beforeEach(() => {
containerEl = ui.setup(options);
const newHeader = document.createElement('div');
newHeader.className = 'bp-header bp-draw-header bp-is-hidden';

containerEl.appendChild(newHeader);
});

ui.replaceHeader('.bp-draw-header');
it('should do nothing if no valid header is specified', () => {
ui.replaceHeader('.bp-invalid-header');

expect(newHeader.classList.contains('bp-is-hidden')).to.be.false;
const baseHeader = containerEl.querySelector('.bp-base-header');
expect(newHeader).to.have.class('bp-is-hidden');
expect(baseHeader).to.not.have.class('bp-is-hidden');

});

it('should hide all headers and then show the specified header', () => {
ui.replaceHeader('.bp-draw-header');

const baseHeader = containerEl.querySelector('.bp-base-header');
expect(baseHeader.classList.contains('bp-is-hidden')).to.be.true;
expect(newHeader).to.not.have.class('bp-is-hidden');
expect(baseHeader).to.have.class('bp-is-hidden');

});
});
Expand Down

0 comments on commit 3a47c2e

Please sign in to comment.