Extract static constants

This commit is contained in:
Bartek Fabiszewski 2019-11-18 13:20:32 +01:00
parent e04c999264
commit 12f8e1245d
2 changed files with 20 additions and 12 deletions

View File

@ -64,16 +64,16 @@ export default class uSelect {
} }
this.hasAllOption = true; this.hasAllOption = true;
const index = this.hasHead ? 1 : 0; const index = this.hasHead ? 1 : 0;
this.element.add(new Option(this.allText, 'all'), index); this.element.add(new Option(this.allText, uSelect.allValue), index);
} }
hideAllOption() { hideAllOption() {
this.hasAllOption = false; this.hasAllOption = false;
this.remove('all'); this.remove(uSelect.allValue);
} }
addHead() { addHead() {
const head = new Option(this.headText, '0', true, true); const head = new Option(this.headText, uSelect.headValue, true, true);
head.disabled = true; head.disabled = true;
this.element.options.add(head, 0); this.element.options.add(head, 0);
} }
@ -115,10 +115,18 @@ export default class uSelect {
this.addHead(); this.addHead();
} }
if (this.hasAllOption) { if (this.hasAllOption) {
this.element.add(new Option(this.allText, 'all', false, selected === 'all')); this.element.add(new Option(this.allText, uSelect.allValue, false, selected === uSelect.allValue));
} }
for (const option of options) { for (const option of options) {
this.element.add(new Option(option.listText, option.listValue, false, selected === option.listValue)); this.element.add(new Option(option.listText, option.listValue, false, selected === option.listValue));
} }
} }
static get allValue() {
return 'all';
}
static get headValue() {
return '0';
}
} }

View File

@ -67,7 +67,7 @@ describe('Select tests', () => {
expect(select.element.options[0].defaultSelected).toBe(true); expect(select.element.options[0].defaultSelected).toBe(true);
expect(select.element.options[0].selected).toBe(true); expect(select.element.options[0].selected).toBe(true);
expect(select.element.options[0].text).toBe(head); expect(select.element.options[0].text).toBe(head);
expect(select.element.options[0].value).toBe('0'); expect(select.element.options[0].value).toBe(uSelect.headValue);
}); });
it('should construct class instance and set options', () => { it('should construct class instance and set options', () => {
@ -119,7 +119,7 @@ describe('Select tests', () => {
expect(select.element.options[0].defaultSelected).toBe(true); expect(select.element.options[0].defaultSelected).toBe(true);
expect(select.element.options[0].selected).toBe(true); expect(select.element.options[0].selected).toBe(true);
expect(select.element.options[0].text).toBe(head); expect(select.element.options[0].text).toBe(head);
expect(select.element.options[0].value).toBe('0'); expect(select.element.options[0].value).toBe(uSelect.headValue);
expect(select.element.options[1].disabled).toBe(false); expect(select.element.options[1].disabled).toBe(false);
expect(select.element.options[1].defaultSelected).toBe(false); expect(select.element.options[1].defaultSelected).toBe(false);
expect(select.element.options[1].selected).toBe(false); expect(select.element.options[1].selected).toBe(false);
@ -143,7 +143,7 @@ describe('Select tests', () => {
expect(select.element.options[0].defaultSelected).toBe(true); expect(select.element.options[0].defaultSelected).toBe(true);
expect(select.element.options[0].selected).toBe(false); expect(select.element.options[0].selected).toBe(false);
expect(select.element.options[0].text).toBe(head); expect(select.element.options[0].text).toBe(head);
expect(select.element.options[0].value).toBe('0'); expect(select.element.options[0].value).toBe(uSelect.headValue);
expect(select.element.options[1].disabled).toBe(false); expect(select.element.options[1].disabled).toBe(false);
expect(select.element.options[1].defaultSelected).toBe(false); expect(select.element.options[1].defaultSelected).toBe(false);
expect(select.element.options[1].selected).toBe(false); expect(select.element.options[1].selected).toBe(false);
@ -179,7 +179,7 @@ describe('Select tests', () => {
select.setOptions(options, options[1].listValue); select.setOptions(options, options[1].listValue);
// then // then
expect(select.element.options.length).toBe(3); expect(select.element.options.length).toBe(3);
expect(select.element.options[0].value).toBe('all'); expect(select.element.options[0].value).toBe(uSelect.allValue);
expect(select.element.options[0].text).toBe(allText); expect(select.element.options[0].text).toBe(allText);
expect(select.element.options[2].selected).toBe(true); expect(select.element.options[2].selected).toBe(true);
}); });
@ -195,7 +195,7 @@ describe('Select tests', () => {
select.showAllOption(allText); select.showAllOption(allText);
// then // then
expect(select.element.options.length).toBe(3); expect(select.element.options.length).toBe(3);
expect(select.element.options[0].value).toBe('all'); expect(select.element.options[0].value).toBe(uSelect.allValue);
expect(select.element.options[0].text).toBe(allText); expect(select.element.options[0].text).toBe(allText);
expect(select.element.options[2].selected).toBe(true); expect(select.element.options[2].selected).toBe(true);
// when // when
@ -219,9 +219,9 @@ describe('Select tests', () => {
select.setOptions(options, options[1].listValue); select.setOptions(options, options[1].listValue);
// then // then
expect(select.element.options.length).toBe(4); expect(select.element.options.length).toBe(4);
expect(select.element.options[0].value).toBe('0'); expect(select.element.options[0].value).toBe(uSelect.headValue);
expect(select.element.options[0].text).toBe(head); expect(select.element.options[0].text).toBe(head);
expect(select.element.options[1].value).toBe('all'); expect(select.element.options[1].value).toBe(uSelect.allValue);
expect(select.element.options[1].text).toBe(allText); expect(select.element.options[1].text).toBe(allText);
expect(select.element.options[3].selected).toBe(true); expect(select.element.options[3].selected).toBe(true);
}); });
@ -237,7 +237,7 @@ describe('Select tests', () => {
select.showAllOption(allText); select.showAllOption(allText);
// then // then
expect(select.element.options.length).toBe(4); expect(select.element.options.length).toBe(4);
expect(select.element.options[1].value).toBe('all'); expect(select.element.options[1].value).toBe(uSelect.allValue);
expect(select.element.options[1].text).toBe(allText); expect(select.element.options[1].text).toBe(allText);
expect(select.element.options[3].selected).toBe(true); expect(select.element.options[3].selected).toBe(true);
// when // when