|
1 | | -import { isEqual } from 'packages/vee-validate/src/utils'; |
| 1 | +import { isEqual, deepCopy } from 'packages/vee-validate/src/utils'; |
2 | 2 |
|
3 | 3 | describe('assertions', () => { |
4 | 4 | test('equal objects are equal', () => { |
@@ -228,4 +228,126 @@ describe('assertions', () => { |
228 | 228 | expect(isEqual(a6, b1)).toBe(false); |
229 | 229 | expect(isEqual(a6, b2)).toBe(false); |
230 | 230 | }); |
| 231 | + |
| 232 | + test('class instances with private properties do not throw in isEqual (#4977)', () => { |
| 233 | + class MyClass { |
| 234 | + #secret: string; |
| 235 | + public name: string; |
| 236 | + |
| 237 | + constructor(name: string, secret: string) { |
| 238 | + this.name = name; |
| 239 | + this.#secret = secret; |
| 240 | + } |
| 241 | + |
| 242 | + getSecret() { |
| 243 | + return this.#secret; |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + const a = new MyClass('test', 'secret1'); |
| 248 | + const b = new MyClass('test', 'secret2'); |
| 249 | + |
| 250 | + // Same reference should be equal |
| 251 | + expect(isEqual(a, a)).toBe(true); |
| 252 | + // Different instances should not be equal (reference equality for class instances) |
| 253 | + expect(isEqual(a, b)).toBe(false); |
| 254 | + }); |
| 255 | + |
| 256 | + test('class instances with private properties nested in plain objects (#4977)', () => { |
| 257 | + class DataObj { |
| 258 | + #value: number; |
| 259 | + |
| 260 | + constructor(value: number) { |
| 261 | + this.#value = value; |
| 262 | + } |
| 263 | + |
| 264 | + getValue() { |
| 265 | + return this.#value; |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + const obj1 = { name: 'test', data: new DataObj(1) }; |
| 270 | + const obj2 = { name: 'test', data: obj1.data }; |
| 271 | + const obj3 = { name: 'test', data: new DataObj(1) }; |
| 272 | + |
| 273 | + // Same nested reference should be equal |
| 274 | + expect(isEqual(obj1, obj2)).toBe(true); |
| 275 | + // Different class instance references should not be equal |
| 276 | + expect(isEqual(obj1, obj3)).toBe(false); |
| 277 | + }); |
| 278 | + |
| 279 | + test('deepCopy does not throw on class instances with private properties (#4977)', () => { |
| 280 | + class MyClass { |
| 281 | + #secret: string; |
| 282 | + public name: string; |
| 283 | + |
| 284 | + constructor(name: string, secret: string) { |
| 285 | + this.name = name; |
| 286 | + this.#secret = secret; |
| 287 | + } |
| 288 | + |
| 289 | + getSecret() { |
| 290 | + return this.#secret; |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + const instance = new MyClass('test', 'secret'); |
| 295 | + const values = { field1: 'hello', myObj: instance }; |
| 296 | + |
| 297 | + // Should not throw |
| 298 | + const cloned = deepCopy(values); |
| 299 | + |
| 300 | + // The plain object should be cloned |
| 301 | + expect(cloned).not.toBe(values); |
| 302 | + expect(cloned.field1).toBe('hello'); |
| 303 | + // The class instance should be returned by reference, not cloned |
| 304 | + expect(cloned.myObj).toBe(instance); |
| 305 | + expect(cloned.myObj.getSecret()).toBe('secret'); |
| 306 | + }); |
| 307 | + |
| 308 | + test('deepCopy handles class instances nested in arrays (#4977)', () => { |
| 309 | + class Item { |
| 310 | + #id: number; |
| 311 | + |
| 312 | + constructor(id: number) { |
| 313 | + this.#id = id; |
| 314 | + } |
| 315 | + |
| 316 | + getId() { |
| 317 | + return this.#id; |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + const item1 = new Item(1); |
| 322 | + const item2 = new Item(2); |
| 323 | + const values = { items: [item1, item2] }; |
| 324 | + |
| 325 | + const cloned = deepCopy(values); |
| 326 | + |
| 327 | + expect(cloned).not.toBe(values); |
| 328 | + expect(cloned.items).not.toBe(values.items); |
| 329 | + // Class instances should be the same references |
| 330 | + expect(cloned.items[0]).toBe(item1); |
| 331 | + expect(cloned.items[1]).toBe(item2); |
| 332 | + expect(cloned.items[0].getId()).toBe(1); |
| 333 | + expect(cloned.items[1].getId()).toBe(2); |
| 334 | + }); |
| 335 | + |
| 336 | + test('deepCopy still clones plain objects and primitive values', () => { |
| 337 | + const values = { |
| 338 | + name: 'test', |
| 339 | + nested: { a: 1, b: 2 }, |
| 340 | + arr: [1, 2, 3], |
| 341 | + date: new Date('2024-01-01'), |
| 342 | + }; |
| 343 | + |
| 344 | + const cloned = deepCopy(values); |
| 345 | + |
| 346 | + expect(cloned).not.toBe(values); |
| 347 | + expect(cloned.name).toBe('test'); |
| 348 | + expect(cloned.nested).not.toBe(values.nested); |
| 349 | + expect(cloned.nested).toEqual({ a: 1, b: 2 }); |
| 350 | + expect(cloned.arr).not.toBe(values.arr); |
| 351 | + expect(cloned.arr).toEqual([1, 2, 3]); |
| 352 | + }); |
231 | 353 | }); |
0 commit comments