| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
/*----------------------------------------------------------------------------- |
| 4 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 5 |
| |
| 6 |
| Distributed under the terms of the BSD 3-Clause License. |
| 7 |
| |
| 8 |
| The full license is in the file LICENSE, distributed with this software. |
| 9 |
|----------------------------------------------------------------------------*/ |
| 10 |
import { expect } from 'chai'; |
| 11 |
|
| 12 |
import { Title } from '@lumino/widgets'; |
| 13 |
|
| 14 |
const owner = { |
| 15 |
name: 'Bob' |
| 16 |
}; |
| 17 |
|
| 18 |
describe('@lumino/widgets', () => { |
| 19 |
describe('Title', () => { |
| 20 |
describe('#constructor()', () => { |
| 21 |
it('should accept title options', () => { |
| 22 |
let title = new Title({ owner }); |
| 23 |
expect(title).to.be.an.instanceof(Title); |
| 24 |
}); |
| 25 |
}); |
| 26 |
|
| 27 |
describe('#changed', () => { |
| 28 |
it('should be emitted when the title state changes', () => { |
| 29 |
let called = false; |
| 30 |
let title = new Title({ owner }); |
| 31 |
title.changed.connect((sender, arg) => { |
| 32 |
expect(sender).to.equal(title); |
| 33 |
expect(arg).to.equal(undefined); |
| 34 |
called = true; |
| 35 |
}); |
| 36 |
title.label = 'baz'; |
| 37 |
expect(called).to.equal(true); |
| 38 |
}); |
| 39 |
|
| 40 |
it('should be cleared if it is disposed', () => { |
| 41 |
let called = false; |
| 42 |
let title = new Title({ owner }); |
| 43 |
title.changed.connect((sender, arg) => { |
| 44 |
called = true; |
| 45 |
}); |
| 46 |
|
| 47 |
title.dispose(); |
| 48 |
|
| 49 |
title.label = 'baz'; |
| 50 |
expect(called).to.equal(false); |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
describe('#owner', () => { |
| 55 |
it('should be the title owner', () => { |
| 56 |
let title = new Title({ owner }); |
| 57 |
expect(title.owner).to.equal(owner); |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
describe('#label', () => { |
| 62 |
it('should default to an empty string', () => { |
| 63 |
let title = new Title({ owner }); |
| 64 |
expect(title.label).to.equal(''); |
| 65 |
}); |
| 66 |
|
| 67 |
it('should initialize from the options', () => { |
| 68 |
let title = new Title({ owner, label: 'foo' }); |
| 69 |
expect(title.label).to.equal('foo'); |
| 70 |
}); |
| 71 |
|
| 72 |
it('should be writable', () => { |
| 73 |
let title = new Title({ owner, label: 'foo' }); |
| 74 |
title.label = 'bar'; |
| 75 |
expect(title.label).to.equal('bar'); |
| 76 |
}); |
| 77 |
|
| 78 |
it('should emit the changed signal when the value changes', () => { |
| 79 |
let called = false; |
| 80 |
let title = new Title({ owner, label: 'foo' }); |
| 81 |
title.changed.connect((sender, arg) => { |
| 82 |
expect(sender).to.equal(title); |
| 83 |
expect(arg).to.equal(undefined); |
| 84 |
called = true; |
| 85 |
}); |
| 86 |
title.label = 'baz'; |
| 87 |
expect(called).to.equal(true); |
| 88 |
}); |
| 89 |
|
| 90 |
it('should not emit the changed signal when the value does not change', () => { |
| 91 |
let called = false; |
| 92 |
let title = new Title({ owner, label: 'foo' }); |
| 93 |
title.changed.connect((sender, arg) => { |
| 94 |
called = true; |
| 95 |
}); |
| 96 |
title.label = 'foo'; |
| 97 |
expect(called).to.equal(false); |
| 98 |
}); |
| 99 |
}); |
| 100 |
|
| 101 |
describe('#mnemonic', () => { |
| 102 |
it('should default to `-1', () => { |
| 103 |
let title = new Title({ owner }); |
| 104 |
expect(title.mnemonic).to.equal(-1); |
| 105 |
}); |
| 106 |
|
| 107 |
it('should initialize from the options', () => { |
| 108 |
let title = new Title({ owner, mnemonic: 1 }); |
| 109 |
expect(title.mnemonic).to.equal(1); |
| 110 |
}); |
| 111 |
|
| 112 |
it('should be writable', () => { |
| 113 |
let title = new Title({ owner, mnemonic: 1 }); |
| 114 |
title.mnemonic = 2; |
| 115 |
expect(title.mnemonic).to.equal(2); |
| 116 |
}); |
| 117 |
|
| 118 |
it('should emit the changed signal when the value changes', () => { |
| 119 |
let called = false; |
| 120 |
let title = new Title({ owner, mnemonic: 1 }); |
| 121 |
title.changed.connect((sender, arg) => { |
| 122 |
expect(sender).to.equal(title); |
| 123 |
expect(arg).to.equal(undefined); |
| 124 |
called = true; |
| 125 |
}); |
| 126 |
title.mnemonic = 0; |
| 127 |
expect(called).to.equal(true); |
| 128 |
}); |
| 129 |
|
| 130 |
it('should not emit the changed signal when the value does not change', () => { |
| 131 |
let called = false; |
| 132 |
let title = new Title({ owner, mnemonic: 1 }); |
| 133 |
title.changed.connect((sender, arg) => { |
| 134 |
called = true; |
| 135 |
}); |
| 136 |
title.mnemonic = 1; |
| 137 |
expect(called).to.equal(false); |
| 138 |
}); |
| 139 |
}); |
| 140 |
|
| 141 |
describe('#icon', () => { |
| 142 |
const iconRenderer = { |
| 143 |
render: (host: HTMLElement, options?: any) => { |
| 144 |
const renderNode = document.createElement('div'); |
| 145 |
renderNode.className = 'foo'; |
| 146 |
host.appendChild(renderNode); |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
it('should default to undefined', () => { |
| 151 |
let title = new Title({ owner }); |
| 152 |
expect(title.icon).to.equal(undefined); |
| 153 |
}); |
| 154 |
|
| 155 |
it('should initialize from the options', () => { |
| 156 |
let title = new Title({ owner, icon: iconRenderer }); |
| 157 |
expect(title.icon).to.equal(iconRenderer); |
| 158 |
}); |
| 159 |
|
| 160 |
it('should be writable', () => { |
| 161 |
let title = new Title({ owner }); |
| 162 |
expect(title.icon).to.equal(undefined); |
| 163 |
title.icon = iconRenderer; |
| 164 |
expect(title.icon).to.equal(iconRenderer); |
| 165 |
}); |
| 166 |
|
| 167 |
it('should emit the changed signal when the value changes', () => { |
| 168 |
let called = false; |
| 169 |
let title = new Title({ owner }); |
| 170 |
title.changed.connect((sender, arg) => { |
| 171 |
expect(sender).to.equal(title); |
| 172 |
expect(arg).to.equal(undefined); |
| 173 |
called = true; |
| 174 |
}); |
| 175 |
title.icon = iconRenderer; |
| 176 |
expect(called).to.equal(true); |
| 177 |
}); |
| 178 |
|
| 179 |
it('should not emit the changed signal when the value does not change', () => { |
| 180 |
let called = false; |
| 181 |
let title = new Title({ owner, icon: iconRenderer }); |
| 182 |
title.changed.connect((sender, arg) => { |
| 183 |
called = true; |
| 184 |
}); |
| 185 |
title.icon = iconRenderer; |
| 186 |
expect(called).to.equal(false); |
| 187 |
}); |
| 188 |
}); |
| 189 |
|
| 190 |
describe('#caption', () => { |
| 191 |
it('should default to an empty string', () => { |
| 192 |
let title = new Title({ owner }); |
| 193 |
expect(title.caption).to.equal(''); |
| 194 |
}); |
| 195 |
|
| 196 |
it('should initialize from the options', () => { |
| 197 |
let title = new Title({ owner, caption: 'foo' }); |
| 198 |
expect(title.caption).to.equal('foo'); |
| 199 |
}); |
| 200 |
|
| 201 |
it('should be writable', () => { |
| 202 |
let title = new Title({ owner, caption: 'foo' }); |
| 203 |
title.caption = 'bar'; |
| 204 |
expect(title.caption).to.equal('bar'); |
| 205 |
}); |
| 206 |
|
| 207 |
it('should emit the changed signal when the value changes', () => { |
| 208 |
let called = false; |
| 209 |
let title = new Title({ owner, caption: 'foo' }); |
| 210 |
title.changed.connect((sender, arg) => { |
| 211 |
expect(sender).to.equal(title); |
| 212 |
expect(arg).to.equal(undefined); |
| 213 |
called = true; |
| 214 |
}); |
| 215 |
title.caption = 'baz'; |
| 216 |
expect(called).to.equal(true); |
| 217 |
}); |
| 218 |
|
| 219 |
it('should not emit the changed signal when the value does not change', () => { |
| 220 |
let called = false; |
| 221 |
let title = new Title({ owner, caption: 'foo' }); |
| 222 |
title.changed.connect((sender, arg) => { |
| 223 |
called = true; |
| 224 |
}); |
| 225 |
title.caption = 'foo'; |
| 226 |
expect(called).to.equal(false); |
| 227 |
}); |
| 228 |
}); |
| 229 |
|
| 230 |
describe('#className', () => { |
| 231 |
it('should default to an empty string', () => { |
| 232 |
let title = new Title({ owner }); |
| 233 |
expect(title.className).to.equal(''); |
| 234 |
}); |
| 235 |
|
| 236 |
it('should initialize from the options', () => { |
| 237 |
let title = new Title({ owner, className: 'foo' }); |
| 238 |
expect(title.className).to.equal('foo'); |
| 239 |
}); |
| 240 |
|
| 241 |
it('should be writable', () => { |
| 242 |
let title = new Title({ owner, className: 'foo' }); |
| 243 |
title.className = 'bar'; |
| 244 |
expect(title.className).to.equal('bar'); |
| 245 |
}); |
| 246 |
|
| 247 |
it('should emit the changed signal when the value changes', () => { |
| 248 |
let called = false; |
| 249 |
let title = new Title({ owner, className: 'foo' }); |
| 250 |
title.changed.connect((sender, arg) => { |
| 251 |
expect(sender).to.equal(title); |
| 252 |
expect(arg).to.equal(undefined); |
| 253 |
called = true; |
| 254 |
}); |
| 255 |
title.className = 'baz'; |
| 256 |
expect(called).to.equal(true); |
| 257 |
}); |
| 258 |
|
| 259 |
it('should not emit the changed signal when the value does not change', () => { |
| 260 |
let called = false; |
| 261 |
let title = new Title({ owner, className: 'foo' }); |
| 262 |
title.changed.connect((sender, arg) => { |
| 263 |
called = true; |
| 264 |
}); |
| 265 |
title.className = 'foo'; |
| 266 |
expect(called).to.equal(false); |
| 267 |
}); |
| 268 |
}); |
| 269 |
|
| 270 |
describe('#closable', () => { |
| 271 |
it('should default to `false`', () => { |
| 272 |
let title = new Title({ owner }); |
| 273 |
expect(title.closable).to.equal(false); |
| 274 |
}); |
| 275 |
|
| 276 |
it('should initialize from the options', () => { |
| 277 |
let title = new Title({ owner, closable: true }); |
| 278 |
expect(title.closable).to.equal(true); |
| 279 |
}); |
| 280 |
|
| 281 |
it('should be writable', () => { |
| 282 |
let title = new Title({ owner, closable: true }); |
| 283 |
title.closable = false; |
| 284 |
expect(title.closable).to.equal(false); |
| 285 |
}); |
| 286 |
|
| 287 |
it('should emit the changed signal when the value changes', () => { |
| 288 |
let called = false; |
| 289 |
let title = new Title({ owner, closable: false }); |
| 290 |
title.changed.connect((sender, arg) => { |
| 291 |
expect(sender).to.equal(title); |
| 292 |
expect(arg).to.equal(undefined); |
| 293 |
called = true; |
| 294 |
}); |
| 295 |
title.closable = true; |
| 296 |
expect(called).to.equal(true); |
| 297 |
}); |
| 298 |
|
| 299 |
it('should not emit the changed signal when the value does not change', () => { |
| 300 |
let called = false; |
| 301 |
let title = new Title({ owner, closable: false }); |
| 302 |
title.changed.connect((sender, arg) => { |
| 303 |
called = true; |
| 304 |
}); |
| 305 |
title.closable = false; |
| 306 |
expect(called).to.equal(false); |
| 307 |
}); |
| 308 |
}); |
| 309 |
|
| 310 |
describe('#dataset', () => { |
| 311 |
it('should default to `{}`', () => { |
| 312 |
let title = new Title({ owner }); |
| 313 |
expect(title.dataset).to.deep.equal({}); |
| 314 |
}); |
| 315 |
|
| 316 |
it('should initialize from the options', () => { |
| 317 |
let title = new Title({ owner, dataset: { foo: '12' } }); |
| 318 |
expect(title.dataset).to.deep.equal({ foo: '12' }); |
| 319 |
}); |
| 320 |
|
| 321 |
it('should be writable', () => { |
| 322 |
let title = new Title({ owner, dataset: { foo: '12' } }); |
| 323 |
title.dataset = { bar: '42' }; |
| 324 |
expect(title.dataset).to.deep.equal({ bar: '42' }); |
| 325 |
}); |
| 326 |
|
| 327 |
it('should emit the changed signal when the value changes', () => { |
| 328 |
let called = false; |
| 329 |
let title = new Title({ owner, dataset: { foo: '12' } }); |
| 330 |
title.changed.connect((sender, arg) => { |
| 331 |
expect(sender).to.equal(title); |
| 332 |
expect(arg).to.equal(undefined); |
| 333 |
called = true; |
| 334 |
}); |
| 335 |
title.dataset = { bar: '42' }; |
| 336 |
expect(called).to.equal(true); |
| 337 |
}); |
| 338 |
|
| 339 |
it('should not emit the changed signal when the value does not change', () => { |
| 340 |
let called = false; |
| 341 |
let dataset = { foo: '12' }; |
| 342 |
let title = new Title({ owner, dataset }); |
| 343 |
title.changed.connect((sender, arg) => { |
| 344 |
called = true; |
| 345 |
}); |
| 346 |
title.dataset = dataset; |
| 347 |
expect(called).to.equal(false); |
| 348 |
}); |
| 349 |
}); |
| 350 |
}); |
| 351 |
}); |