16
import { observableProxy } from "@koalaui/arkui-common";
17
import { ObservableHandler } from "@koalaui/runtime";
18
import { propDeepCopy } from "language"
19
import { assert } from "chai"
21
suite("@Prop deep copy", () => {
22
test("Complex object", () => {
23
const initialNumber = 15
24
const changedNumber = 25
30
const object: any = new A()
31
object.add(initialNumber)
32
object.dynamicProperty = { nested: initialNumber }
34
const copy = propDeepCopy(object)
36
assert.equal(object.dynamicProperty.nested, copy.dynamicProperty.nested)
37
assert.equal(object.has(initialNumber), copy.has(initialNumber))
38
assert.equal(object.foo(), copy.foo())
40
copy.delete(initialNumber)
41
copy.dynamicProperty.nested = changedNumber
42
copy.foo = () => { return changedNumber }
44
assert.notEqual(object.dynamicProperty.nested, copy.dynamicProperty.nested)
45
assert.notEqual(object.has(initialNumber), copy.has(initialNumber))
46
assert.notEqual(object.foo(), copy.foo())
49
test("number", () => {
51
let copy = propDeepCopy(num)
52
assert.equal(num, copy)
55
assert.notEqual(num, copy)
59
const date: Date = new Date("2023-01-01T12:00:00.000Z")
60
date.setTime(date.getTime() + 5)
62
const copy = propDeepCopy(date)
63
assert.equal(date.getTime(), copy.getTime())
66
assert.notEqual(date.getTime(), copy.getTime())
69
test("__memo_state_observable_handler__", () => {
70
const proxy = observableProxy({})
71
const copy = propDeepCopy(proxy)
73
assert.notEqual(proxy, copy)
74
assert.isFalse(copy.hasOwnProperty('__memo_state_observable_handler__'))
75
assert.notEqual(ObservableHandler.find(proxy), undefined)
76
assert.equal(ObservableHandler.find(copy), undefined)
82
constructor(num: number = 1) {
95
test("Mutating method", () => {
96
const a: any = new A()
99
a['inc'] = function (this, ...args: any[]) {
100
this.originalInc(...args)
103
const propCopy = propDeepCopy(a)
107
assert.equal(a.num, 1)
108
assert.equal(propCopy.num, 2)
111
test("Observable proxy", () => {
112
const proxy = observableProxy(new A())
113
const copy = propDeepCopy(proxy)
117
assert.equal(proxy.num, 1)
118
assert.equal(copy.num, 2)
122
assert.equal(proxy.num, 2)
123
assert.equal(copy.num, 2)
126
test("Date observable proxy", () => {
127
const proxy = observableProxy(new Date())
128
const copy = propDeepCopy(proxy)
130
copy.setTime(copy.getTime() + 8504040)
131
assert.isTrue(proxy.getTime() < copy.getTime())
132
assert.isFalse(copy.hasOwnProperty('__memo_state_observable_handler__'))
134
const observedDeepCopy = observableProxy(copy)
135
observedDeepCopy.setTime(12)
136
assert.isTrue(copy.getTime() == observedDeepCopy.getTime())
139
test("Array observable proxy", () => {
140
const proxy = observableProxy([1,2,3])
141
const copy = propDeepCopy(proxy)
144
assert.isTrue(proxy.length < copy.length)
146
const observedDeepCopy = observableProxy(copy)
147
observedDeepCopy.push(15)
148
assert.isTrue(copy.length < observedDeepCopy.length)
151
test("Primitive type terminates", () => {
152
propDeepCopy(observableProxy("hello"))