| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { every } from '@lumino/algorithm'; |
| 5 |
import { Message } from '@lumino/messaging'; |
| 6 |
import { AccordionLayout, Title, Widget } from '@lumino/widgets'; |
| 7 |
import { expect } from 'chai'; |
| 8 |
|
| 9 |
const renderer: AccordionLayout.IRenderer = { |
| 10 |
titleClassName: '.lm-AccordionTitle', |
| 11 |
createHandle: () => document.createElement('div'), |
| 12 |
createSectionTitle: (title: Title<Widget>) => document.createElement('h3') |
| 13 |
}; |
| 14 |
|
| 15 |
class LogAccordionLayout extends AccordionLayout { |
| 16 |
methods: string[] = []; |
| 17 |
|
| 18 |
protected init(): void { |
| 19 |
super.init(); |
| 20 |
this.methods.push('init'); |
| 21 |
} |
| 22 |
|
| 23 |
protected attachWidget(index: number, widget: Widget): void { |
| 24 |
super.attachWidget(index, widget); |
| 25 |
this.methods.push('attachWidget'); |
| 26 |
} |
| 27 |
|
| 28 |
protected moveWidget( |
| 29 |
fromIndex: number, |
| 30 |
toIndex: number, |
| 31 |
widget: Widget |
| 32 |
): void { |
| 33 |
super.moveWidget(fromIndex, toIndex, widget); |
| 34 |
this.methods.push('moveWidget'); |
| 35 |
} |
| 36 |
|
| 37 |
protected detachWidget(index: number, widget: Widget): void { |
| 38 |
super.detachWidget(index, widget); |
| 39 |
this.methods.push('detachWidget'); |
| 40 |
} |
| 41 |
|
| 42 |
protected onFitRequest(msg: Message): void { |
| 43 |
super.onFitRequest(msg); |
| 44 |
this.methods.push('onFitRequest'); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
describe('@lumino/widgets', () => { |
| 49 |
describe('AccordionLayout', () => { |
| 50 |
describe('#constructor()', () => { |
| 51 |
it('should accept a renderer', () => { |
| 52 |
const layout = new AccordionLayout({ renderer }); |
| 53 |
expect(layout).to.be.an.instanceof(AccordionLayout); |
| 54 |
}); |
| 55 |
|
| 56 |
it('should be vertical by default', () => { |
| 57 |
const layout = new AccordionLayout({ renderer }); |
| 58 |
expect(layout.orientation).to.equal('vertical'); |
| 59 |
}); |
| 60 |
}); |
| 61 |
|
| 62 |
describe('#titleSpace', () => { |
| 63 |
it('should get the inter-element spacing for the split layout', () => { |
| 64 |
const layout = new AccordionLayout({ renderer }); |
| 65 |
expect(layout.titleSpace).to.equal(22); |
| 66 |
}); |
| 67 |
|
| 68 |
it('should set the inter-element spacing for the split layout', () => { |
| 69 |
const layout = new AccordionLayout({ renderer }); |
| 70 |
layout.titleSpace = 10; |
| 71 |
expect(layout.titleSpace).to.equal(10); |
| 72 |
}); |
| 73 |
|
| 74 |
it('should post a fit request to the parent widget', done => { |
| 75 |
let layout = new LogAccordionLayout({ renderer }); |
| 76 |
let parent = new Widget(); |
| 77 |
parent.layout = layout; |
| 78 |
layout.titleSpace = 10; |
| 79 |
requestAnimationFrame(() => { |
| 80 |
expect(layout.methods).to.contain('onFitRequest'); |
| 81 |
done(); |
| 82 |
}); |
| 83 |
}); |
| 84 |
|
| 85 |
it('should be a no-op if the value does not change', done => { |
| 86 |
let layout = new LogAccordionLayout({ renderer }); |
| 87 |
let parent = new Widget(); |
| 88 |
parent.layout = layout; |
| 89 |
layout.titleSpace = 22; |
| 90 |
requestAnimationFrame(() => { |
| 91 |
expect(layout.methods).to.not.contain('onFitRequest'); |
| 92 |
done(); |
| 93 |
}); |
| 94 |
}); |
| 95 |
}); |
| 96 |
|
| 97 |
describe('#renderer', () => { |
| 98 |
it('should get the renderer for the layout', () => { |
| 99 |
const layout = new AccordionLayout({ renderer }); |
| 100 |
expect(layout.renderer).to.equal(renderer); |
| 101 |
}); |
| 102 |
}); |
| 103 |
|
| 104 |
describe('#titles', () => { |
| 105 |
it('should be a read-only sequence of the accordion titles in the layout', () => { |
| 106 |
const layout = new AccordionLayout({ renderer }); |
| 107 |
let parent = new Widget(); |
| 108 |
parent.layout = layout; |
| 109 |
const widgets = [new Widget(), new Widget(), new Widget()]; |
| 110 |
for (const widget of widgets) { |
| 111 |
layout.addWidget(widget); |
| 112 |
} |
| 113 |
|
| 114 |
expect(every(layout.titles, h => h instanceof HTMLElement)); |
| 115 |
expect(layout.titles).to.have.length(widgets.length); |
| 116 |
}); |
| 117 |
}); |
| 118 |
|
| 119 |
describe('#attachWidget()', () => { |
| 120 |
it('should insert a title node before the widget', () => { |
| 121 |
let layout = new LogAccordionLayout({ renderer }); |
| 122 |
let parent = new Widget(); |
| 123 |
parent.layout = layout; |
| 124 |
let widget = new Widget(); |
| 125 |
|
| 126 |
layout.addWidget(widget); |
| 127 |
|
| 128 |
expect(layout.methods).to.contain('attachWidget'); |
| 129 |
expect(parent.node.contains(widget.node)).to.equal(true); |
| 130 |
expect(layout.titles.length).to.equal(1); |
| 131 |
|
| 132 |
const title = layout.titles[0]; |
| 133 |
expect(widget.node.previousElementSibling).to.equal(title); |
| 134 |
expect(title.getAttribute('aria-label')).to.equal( |
| 135 |
`${parent.title.label} Section` |
| 136 |
); |
| 137 |
expect(title.getAttribute('aria-expanded')).to.equal('true'); |
| 138 |
expect(title.classList.contains('lm-mod-expanded')).to.be.true; |
| 139 |
|
| 140 |
expect(widget.node.getAttribute('role')).to.equal('region'); |
| 141 |
expect(widget.node.getAttribute('aria-labelledby')).to.equal(title.id); |
| 142 |
parent.dispose(); |
| 143 |
}); |
| 144 |
}); |
| 145 |
|
| 146 |
describe('#moveWidget()', () => { |
| 147 |
it("should move a title in the parent's DOM node", () => { |
| 148 |
let layout = new LogAccordionLayout({ renderer }); |
| 149 |
let widgets = [new Widget(), new Widget(), new Widget()]; |
| 150 |
let parent = new Widget(); |
| 151 |
parent.layout = layout; |
| 152 |
widgets.forEach(w => { |
| 153 |
layout.addWidget(w); |
| 154 |
}); |
| 155 |
let widget = widgets[0]; |
| 156 |
let title = layout.titles[0]; |
| 157 |
|
| 158 |
layout.insertWidget(2, widget); |
| 159 |
|
| 160 |
expect(layout.methods).to.contain('moveWidget'); |
| 161 |
expect(layout.titles[2]).to.equal(title); |
| 162 |
parent.dispose(); |
| 163 |
}); |
| 164 |
}); |
| 165 |
|
| 166 |
describe('#detachWidget()', () => { |
| 167 |
it("should detach a title from the parent's DOM node", () => { |
| 168 |
let layout = new LogAccordionLayout({ renderer }); |
| 169 |
let widget = new Widget(); |
| 170 |
let parent = new Widget(); |
| 171 |
parent.layout = layout; |
| 172 |
layout.addWidget(widget); |
| 173 |
const title = layout.titles[0]; |
| 174 |
|
| 175 |
layout.removeWidget(widget); |
| 176 |
|
| 177 |
expect(layout.methods).to.contain('detachWidget'); |
| 178 |
expect(parent.node.contains(title)).to.equal(false); |
| 179 |
expect(layout.titles).to.have.length(0); |
| 180 |
parent.dispose(); |
| 181 |
}); |
| 182 |
}); |
| 183 |
|
| 184 |
describe('#dispose', () => { |
| 185 |
it('clear the titles list', () => { |
| 186 |
const layout = new AccordionLayout({ renderer }); |
| 187 |
const widgets = [new Widget(), new Widget(), new Widget()]; |
| 188 |
widgets.forEach(w => { |
| 189 |
layout.addWidget(w); |
| 190 |
}); |
| 191 |
|
| 192 |
layout.dispose(); |
| 193 |
|
| 194 |
expect(layout.titles).to.have.length(0); |
| 195 |
}); |
| 196 |
}); |
| 197 |
}); |
| 198 |
}); |