From 2d56711f482d2a9b2e0d23b581579cafb47c76f5 Mon Sep 17 00:00:00 2001 From: George Sotiropoulos Date: Mon, 7 Nov 2022 18:40:07 +0200 Subject: [PATCH] fix: Data.js must not overwrite exising element instance --- js/src/dom/data.js | 2 +- js/tests/unit/dom/data.spec.js | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/js/src/dom/data.js b/js/src/dom/data.js index 76c5e0a29b..f3aedbe267 100644 --- a/js/src/dom/data.js +++ b/js/src/dom/data.js @@ -21,7 +21,7 @@ export default { // make it clear we only want one instance per element // can be removed later when multiple key/instances are fine to be used - if (!instanceMap.has(key) && instanceMap.size !== 0) { + if (instanceMap.size !== 0) { // eslint-disable-next-line no-console console.error(`Bootstrap doesn't allow more than one instance per element. Bound instance: ${Array.from(instanceMap.keys())[0]}.`) return diff --git a/js/tests/unit/dom/data.spec.js b/js/tests/unit/dom/data.spec.js index e898cbba21..b8c7b0ad9a 100644 --- a/js/tests/unit/dom/data.spec.js +++ b/js/tests/unit/dom/data.spec.js @@ -53,18 +53,6 @@ describe('Data', () => { expect(Data.get(div, TEST_KEY)).toEqual(data) }) - it('should overwrite data if something is already stored', () => { - const data = { ...TEST_DATA } - const copy = { ...data } - - Data.set(div, TEST_KEY, data) - Data.set(div, TEST_KEY, copy) - - // Using `toBe` since spread creates a shallow copy - expect(Data.get(div, TEST_KEY)).not.toBe(data) - expect(Data.get(div, TEST_KEY)).toBe(copy) - }) - it('should do nothing when an element has nothing stored', () => { Data.remove(div, TEST_KEY) @@ -102,5 +90,17 @@ describe('Data', () => { expect(console.error).toHaveBeenCalled() expect(Data.get(div, UNKNOWN_KEY)).toBeNull() }) + + it('should console.error a message if "set" is for the same element', () => { + console.error = jasmine.createSpy('console.error') + + const data = { ...TEST_DATA } + + Data.set(div, TEST_KEY, data) + Data.set(div, TEST_KEY, data) + + expect(console.error).toHaveBeenCalled() + expect(Data.get(div, TEST_KEY)).toEqual(data) + }) /* eslint-enable no-console */ }) -- 2.47.2