]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
fix: Data.js must not overwrite exising element instance
authorGeorge Sotiropoulos <geo.sotis@gmail.com>
Mon, 7 Nov 2022 16:40:07 +0000 (18:40 +0200)
committerGeoSot <geo.sotis@gmail.com>
Wed, 9 Nov 2022 08:24:51 +0000 (10:24 +0200)
js/src/dom/data.js
js/tests/unit/dom/data.spec.js

index 76c5e0a29bab62409c73c9f32bfa22b265ab05cf..f3aedbe267fc7f1bb6008d2c851da73a20ccdc19 100644 (file)
@@ -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
index e898cbba21d26614d89359e5e0b4ea3515373864..b8c7b0ad9a2c293b7377f5b375ed3ccf83a81d16 100644 (file)
@@ -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 */
 })