| 1 |
import { DummyManager } from './dummy-manager'; |
| 2 |
|
| 3 |
import { expect } from 'chai'; |
| 4 |
|
| 5 |
import * as widgets from '../../lib'; |
| 6 |
|
| 7 |
describe('ComboboxView', function () { |
| 8 |
beforeEach(async function () { |
| 9 |
this.manager = new DummyManager(); |
| 10 |
const modelId = 'u-u-i-d'; |
| 11 |
this.model = await this.manager.new_model( |
| 12 |
{ |
| 13 |
model_name: 'ComboboxModel', |
| 14 |
model_module: '@jupyter-widgets/controls', |
| 15 |
model_module_version: '1.0.0', |
| 16 |
model_id: modelId, |
| 17 |
}, |
| 18 |
{ description: 'test-combo-model' } |
| 19 |
); |
| 20 |
}); |
| 21 |
|
| 22 |
it('construction', function () { |
| 23 |
const options = { model: this.model }; |
| 24 |
const view = new widgets.ComboboxView(options); |
| 25 |
expect(view).to.not.be.undefined; |
| 26 |
}); |
| 27 |
|
| 28 |
it('no invalid flag when not checking', function () { |
| 29 |
this.model.set({ |
| 30 |
value: 'ABC', |
| 31 |
options: ['ABCDEF', '123', 'foobar'], |
| 32 |
ensure_option: false, |
| 33 |
}); |
| 34 |
const options = { model: this.model }; |
| 35 |
const view = new widgets.ComboboxView(options); |
| 36 |
view.render(); |
| 37 |
expect( |
| 38 |
view.textbox.classList.contains('jpwidgets-invalidComboValue') |
| 39 |
).to.equal(false); |
| 40 |
}); |
| 41 |
|
| 42 |
it('no invalid flag with valid value', function () { |
| 43 |
this.model.set({ |
| 44 |
value: 'ABCDEF', |
| 45 |
options: ['ABCDEF', '123', 'foobar'], |
| 46 |
ensure_option: true, |
| 47 |
}); |
| 48 |
const options = { model: this.model }; |
| 49 |
const view = new widgets.ComboboxView(options); |
| 50 |
view.render(); |
| 51 |
expect( |
| 52 |
view.textbox.classList.contains('jpwidgets-invalidComboValue') |
| 53 |
).to.equal(false); |
| 54 |
}); |
| 55 |
|
| 56 |
it('sets invalid flag when it should', function () { |
| 57 |
this.model.set({ |
| 58 |
value: 'ABC', |
| 59 |
options: ['ABCDEF', '123', 'foobar'], |
| 60 |
ensure_option: true, |
| 61 |
}); |
| 62 |
const options = { model: this.model }; |
| 63 |
const view = new widgets.ComboboxView(options); |
| 64 |
view.render(); |
| 65 |
expect( |
| 66 |
view.textbox.classList.contains('jpwidgets-invalidComboValue') |
| 67 |
).to.equal(true); |
| 68 |
}); |
| 69 |
|
| 70 |
it('escapes characters in options', function () { |
| 71 |
const input = [ |
| 72 |
'foo"', |
| 73 |
'"><script>alert("foo")</script><a "', |
| 74 |
'" onmouseover=alert(1) "', |
| 75 |
]; |
| 76 |
this.model.set({ |
| 77 |
value: 'ABC', |
| 78 |
options: input, |
| 79 |
ensure_option: true, |
| 80 |
}); |
| 81 |
const options = { model: this.model }; |
| 82 |
const view = new widgets.ComboboxView(options); |
| 83 |
view.render(); |
| 84 |
expect(view.datalist!.children.length).to.equal(3); |
| 85 |
for (let i = 0; i < view.datalist!.children.length; ++i) { |
| 86 |
const el = view.datalist!.children[i]; |
| 87 |
expect(el.tagName.toLowerCase()).to.equal('option'); |
| 88 |
expect(el.getAttributeNames()).to.eqls(['value']); |
| 89 |
expect(el.getAttribute('value')).to.equal(input[i]); |
| 90 |
} |
| 91 |
}); |
| 92 |
|
| 93 |
it('updates datalist children when options are updated', function () { |
| 94 |
this.model.set({ |
| 95 |
value: 'ABC', |
| 96 |
options: ['option1', 'option2', 'option3'], |
| 97 |
ensure_option: true, |
| 98 |
}); |
| 99 |
const options = { model: this.model }; |
| 100 |
const view = new widgets.ComboboxView(options); |
| 101 |
view.render(); |
| 102 |
expect(view.datalist!.children.length).to.equal(3); |
| 103 |
for (let i = 0; i < view.datalist!.children.length; ++i) { |
| 104 |
const el = view.datalist!.children[i]; |
| 105 |
expect(el.tagName.toLowerCase()).to.equal('option'); |
| 106 |
expect(el.getAttributeNames()).to.eqls(['value']); |
| 107 |
expect(el.getAttribute('value')).to.equal(`option${i + 1}`); |
| 108 |
} |
| 109 |
|
| 110 |
this.model.set({ options: ['option4', 'option5'] }); |
| 111 |
expect(view.datalist!.children.length).to.equal(2); |
| 112 |
for (let i = 0; i < view.datalist!.children.length; ++i) { |
| 113 |
const el = view.datalist!.children[i]; |
| 114 |
expect(el.tagName.toLowerCase()).to.equal('option'); |
| 115 |
expect(el.getAttributeNames()).to.eqls(['value']); |
| 116 |
expect(el.getAttribute('value')).to.equal(`option${i + 4}`); |
| 117 |
} |
| 118 |
}); |
| 119 |
}); |