From 244425d2c5adc37229a2ae0b0af0f97cacc3cea0 Mon Sep 17 00:00:00 2001 From: Victor Mus Date: Wed, 28 Aug 2019 13:10:42 -0400 Subject: [PATCH] fix(clear): Avoid error when clear is called after destroy (#287) * dataset-clear-npe-fix * rename test for clarity --- src/autocomplete/dataset.js | 8 +++++--- test/unit/dataset_spec.js | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index 6d809c90c..35365574d 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -256,9 +256,11 @@ _.mixin(Dataset.prototype, EventEmitter, { }, clear: function clear() { - this.cancel(); - this.$el.empty(); - this.trigger('rendered', ''); + if (this.$el) { + this.cancel(); + this.$el.empty(); + this.trigger('rendered', ''); + } }, isEmpty: function isEmpty() { diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index 0e853a30d..1bec373b1 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -462,6 +462,14 @@ describe('Dataset', function() { this.dataset.clear(); expect(spy).toHaveBeenCalled(); }); + + it('should not throw if called right after destroy()', function() { + this.source.and.callFake(fakeGetWithSyncResults); + this.dataset.update('woah'); + this.dataset.destroy(); + this.dataset.clear(); + expect(this.dataset.getRoot()).toBeNull(); + }); }); describe('#isEmpty', function() {