idlize

Форк
0
/
prop-deep-copy.test.ts 
154 строки · 4.4 Кб
1
/*
2
 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import { observableProxy } from "@koalaui/arkui-common";
17
import { ObservableHandler } from "@koalaui/runtime";
18
import { propDeepCopy } from "language"
19
import { assert } from "chai"
20

21
suite("@Prop deep copy", () => {
22
    test("Complex object", () => {
23
        const initialNumber = 15
24
        const changedNumber = 25
25
        class A extends Set {
26
            private foo() {
27
                return initialNumber
28
            }
29
        }
30
        const object: any = new A()
31
        object.add(initialNumber)
32
        object.dynamicProperty = { nested: initialNumber }
33

34
        const copy = propDeepCopy(object)
35

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())
39

40
        copy.delete(initialNumber)
41
        copy.dynamicProperty.nested = changedNumber
42
        copy.foo = () => { return changedNumber }
43

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())
47
    })
48

49
    test("number", () => {
50
        const num: any = 15
51
        let copy = propDeepCopy(num)
52
        assert.equal(num, copy)
53

54
        copy += 1
55
        assert.notEqual(num, copy)
56
    })
57

58
    test("Date", () => {
59
        const date: Date = new Date("2023-01-01T12:00:00.000Z")
60
        date.setTime(date.getTime() + 5)
61

62
        const copy = propDeepCopy(date)
63
        assert.equal(date.getTime(), copy.getTime())
64

65
        copy.setHours(12)
66
        assert.notEqual(date.getTime(), copy.getTime())
67
    })
68

69
    test("__memo_state_observable_handler__", () => {
70
        const proxy = observableProxy({})
71
        const copy = propDeepCopy(proxy)
72

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)
77
    })
78

79
    class A {
80
        num = 1
81

82
        constructor(num: number = 1) {
83
            this.num = num
84
        }
85

86
        inc() {
87
            this.incOriginal()
88
        }
89

90
        incOriginal() {
91
            this.num += 1
92
        }
93
    }
94

95
    test("Mutating method", () => {
96
        const a: any = new A()
97
        a.originalInc = a.inc
98

99
        a['inc'] = function (this, ...args: any[]) {
100
            this.originalInc(...args)
101
        }
102

103
        const propCopy = propDeepCopy(a)
104

105
        propCopy.inc()
106

107
        assert.equal(a.num, 1)
108
        assert.equal(propCopy.num, 2)
109
    })
110

111
    test("Observable proxy", () => {
112
        const proxy = observableProxy(new A())
113
        const copy = propDeepCopy(proxy)
114

115
        copy.inc()
116

117
        assert.equal(proxy.num, 1)
118
        assert.equal(copy.num, 2)
119

120
        proxy.inc()
121

122
        assert.equal(proxy.num, 2)
123
        assert.equal(copy.num, 2)
124
    })
125

126
    test("Date observable proxy", () => {
127
        const proxy = observableProxy(new Date())
128
        const copy = propDeepCopy(proxy)
129

130
        copy.setTime(copy.getTime() + 8504040)
131
        assert.isTrue(proxy.getTime() < copy.getTime())
132
        assert.isFalse(copy.hasOwnProperty('__memo_state_observable_handler__'))
133

134
        const observedDeepCopy = observableProxy(copy)
135
        observedDeepCopy.setTime(12)
136
        assert.isTrue(copy.getTime() == observedDeepCopy.getTime())
137
    })
138

139
    test("Array observable proxy", () => {
140
        const proxy = observableProxy([1,2,3])
141
        const copy = propDeepCopy(proxy)
142

143
        copy.push(15)
144
        assert.isTrue(proxy.length < copy.length)
145

146
        const observedDeepCopy = observableProxy(copy)
147
        observedDeepCopy.push(15)
148
        assert.isTrue(copy.length < observedDeepCopy.length)
149
    })
150

151
    test("Primitive type terminates", () => {
152
        propDeepCopy(observableProxy("hello"))
153
    })
154
})
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.