Fix JS error upon changing addablePresetIDs after initial load

Accept addablePresetIDs as an array
Add code tests for invalid preset IDs in addablePresetIDs
This commit is contained in:
Quincy Morgan
2020-06-02 11:19:30 -04:00
parent 7fe82444c2
commit 49526a7a67
2 changed files with 50 additions and 2 deletions

View File

@@ -338,11 +338,19 @@ export function presetIndex() {
_this.addablePresetIDs = function(val) {
if (!arguments.length) return _addablePresetIDs;
// accept and convert arrays
if (Array.isArray(val)) val = new Set(val);
_addablePresetIDs = val;
if (_addablePresetIDs) { // reset all presets
_this.collection.forEach(p => p.addable(_addablePresetIDs.has(p.id)));
_this.collection.forEach(p => {
// categories aren't addable
if (p.addable) p.addable(_addablePresetIDs.has(p.id));
});
} else {
_this.collection.forEach(p => p.addable(true));
_this.collection.forEach(p => {
if (p.addable) p.addable(true);
});
}
return _this;

View File

@@ -198,6 +198,10 @@ describe('iD.presetIndex', function () {
iD.fileFetcher.cache().preset_presets = testPresets;
var presets = iD.presetIndex();
presets.ensureLoaded().then(function() {
expect(presets.item('residential').addable()).to.be.true;
expect(presets.item('park').addable()).to.be.true;
var ids = new Set(['residential']); // can only add preset with this ID
presets.addablePresetIDs(ids);
@@ -208,6 +212,42 @@ describe('iD.presetIndex', function () {
presets.addablePresetIDs(null);
expect(presets.item('residential').addable()).to.be.true;
expect(presets.item('park').addable()).to.be.true;
done();
});
});
it('ignores invalid IDs in addablePresetIDs', function (done) {
iD.fileFetcher.cache().preset_presets = testPresets;
var presets = iD.presetIndex();
presets.ensureLoaded().then(function() {
expect(presets.item(null)).to.eql(undefined);
expect(presets.item(undefined)).to.eql(undefined);
expect(presets.item('')).to.eql(undefined);
expect(presets.item('garbage')).to.eql(undefined);
expect(presets.item('residential').addable()).to.be.true;
expect(presets.item('park').addable()).to.be.true;
var ids = new Set([null, undefined, '', 'garbage', 'residential']); // can only add preset with these IDs
presets.addablePresetIDs(ids);
expect(presets.item(null)).to.eql(undefined);
expect(presets.item(undefined)).to.eql(undefined);
expect(presets.item('')).to.eql(undefined);
expect(presets.item('garbage')).to.eql(undefined);
expect(presets.item('residential').addable()).to.be.true;
expect(presets.item('park').addable()).to.be.false;
expect(presets.addablePresetIDs()).to.eql(ids);
presets.addablePresetIDs(null);
expect(presets.item(null)).to.eql(undefined);
expect(presets.item(undefined)).to.eql(undefined);
expect(presets.item('')).to.eql(undefined);
expect(presets.item('garbage')).to.eql(undefined);
expect(presets.item('residential').addable()).to.be.true;
expect(presets.item('park').addable()).to.be.true;
done();
});
});