|
| 1 | +import { isDeepRef } from '../src/utils/isDeepRef' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { ref } from 'vue' |
| 4 | + |
| 5 | +describe('isDeepRef', () => { |
| 6 | + it('should return true for a Ref value', () => { |
| 7 | + const testRef = ref(1) |
| 8 | + |
| 9 | + expect(isDeepRef(testRef)).toBe(true) |
| 10 | + }) |
| 11 | + it('should return false for a non-object, non-Ref value', () => { |
| 12 | + const nonObject = 1 |
| 13 | + |
| 14 | + expect(isDeepRef(nonObject)).toBe(false) |
| 15 | + }) |
| 16 | + it('should return true for an object with a Ref value', () => { |
| 17 | + const testObject = { ref: ref(1) } |
| 18 | + |
| 19 | + expect(isDeepRef(testObject)).toBe(true) |
| 20 | + }) |
| 21 | + it('should return false for an object without a Ref value', () => { |
| 22 | + const testObject = { nonRef: 1 } |
| 23 | + |
| 24 | + expect(isDeepRef(testObject)).toBe(false) |
| 25 | + }) |
| 26 | + it('should return true for an array with a Ref value', () => { |
| 27 | + const arrayWithRef = [ref(1)] |
| 28 | + |
| 29 | + expect(isDeepRef(arrayWithRef)).toBe(true) |
| 30 | + }) |
| 31 | + it('should return false for an array without a Ref value', () => { |
| 32 | + const arrayWithoutRef = [1] |
| 33 | + |
| 34 | + expect(isDeepRef(arrayWithoutRef)).toBe(false) |
| 35 | + }) |
| 36 | + it('should return true for a nested object with a Ref value', () => { |
| 37 | + const nestedObject = { nested: { ref: ref(1) } } |
| 38 | + |
| 39 | + expect(isDeepRef(nestedObject)).toBe(true) |
| 40 | + }) |
| 41 | + it('should return false for a nested object without a Ref value', () => { |
| 42 | + const nestedObject = { nested: { nonRef: 1 } } |
| 43 | + |
| 44 | + expect(isDeepRef(nestedObject)).toBe(false) |
| 45 | + }) |
| 46 | + it('should return true for a nested array with a Ref value', () => { |
| 47 | + const nestedArray = [[ref(1)]] |
| 48 | + |
| 49 | + expect(isDeepRef(nestedArray)).toBe(true) |
| 50 | + }) |
| 51 | + it('should return false for a nested array without a Ref value', () => { |
| 52 | + const nestedArray = [[1]] |
| 53 | + |
| 54 | + expect(isDeepRef(nestedArray)).toBe(false) |
| 55 | + }) |
| 56 | + it('should return false for an object that has already been visited and does not contain a Ref', () => { |
| 57 | + const item = { parent: null as any } |
| 58 | + const parentItem = { children: [item] } |
| 59 | + item.parent = parentItem |
| 60 | + |
| 61 | + expect(isDeepRef(item)).toBe(false) |
| 62 | + }) |
| 63 | + it('should return true for an object that has already been visited and contains a Ref', () => { |
| 64 | + const item = { parent: ref<any>(null) } |
| 65 | + const parentItem = { children: [ref(item)] } |
| 66 | + item.parent.value = parentItem |
| 67 | + |
| 68 | + expect(isDeepRef(item)).toBe(true) |
| 69 | + }) |
| 70 | + it('should return false for an object with a dynamic circular reference', () => { |
| 71 | + const testObject = {} |
| 72 | + Object.defineProperty(testObject, 'circularReference', { |
| 73 | + get: function () { |
| 74 | + delete this.circularReference |
| 75 | + this.circularReference = testObject |
| 76 | + return this.circularReference |
| 77 | + } |
| 78 | + }) |
| 79 | + expect(() => isDeepRef(testObject)).not.toThrow() |
| 80 | + expect(isDeepRef(testObject)).toBe(false) |
| 81 | + }) |
| 82 | +}) |
0 commit comments