idlize

Форк
0
126 строк · 4.0 Кб
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 { assert } from "chai"
17
import {
18
    GlobalStateManager,
19
    State,
20
    TestNode,
21
    mutableState,
22
    remember,
23
    rememberDisposable,
24
    rememberMutableState,
25
    testTick,
26
} from "../../src"
27

28
const collector = new Array<string>()
29

30
function testExpected(root: State<TestNode>, ...expected: string[]) {
31
    collector.length = 0
32
    testTick(root)
33
    assert.deepEqual(collector, expected)
34
    if (expected.length > 0) testExpected(root)
35
}
36

37
suite("remember tests", () => {
38

39
    test("remember computes once", () => {
40
        GlobalStateManager.reset()
41
        const root = TestNode.create(() => {
42
            remember(() => collector.push("inner"))
43
            collector.push("outer")
44
        })
45
        testExpected(root, "inner", "outer")
46
    })
47

48
    test("remember computes once even if inner state changed", () => {
49
        GlobalStateManager.reset()
50
        const state = mutableState(false)
51
        const root = TestNode.create(() => {
52
            remember(() => {
53
                state.value // not depended
54
                collector.push("inner")
55
            })
56
            collector.push("outer")
57
        })
58
        testExpected(root, "inner", "outer")
59
        state.value = true
60
        testExpected(root)
61
    })
62

63
    test("remember computes once even if outer state changed", () => {
64
        GlobalStateManager.reset()
65
        const state = mutableState(false)
66
        const root = TestNode.create(() => {
67
            remember(() => collector.push("inner"))
68
            state.value // depended
69
            collector.push("outer")
70
        })
71
        testExpected(root, "inner", "outer")
72
        state.value = true
73
        testExpected(root, "outer")
74
    })
75

76
    test("intrinsic remember should not conflict each other", () => {
77
        GlobalStateManager.reset()
78
        const state = mutableState(false)
79
        const root = TestNode.create(() => {
80
            state.value
81
                ? remember(() => collector.push("positive"))
82
                : remember(() => collector.push("negative"))
83
        })
84
        testExpected(root, "negative")
85
        state.value = true
86
        testExpected(root, "positive")
87
        state.value = false
88
        testExpected(root, "negative")
89
        state.value = false
90
        testExpected(root)
91
    })
92

93
    test("rememberDisposable created and disposed accordingly", () => {
94
        GlobalStateManager.reset()
95
        const state = mutableState(false)
96
        const root = TestNode.create(() => {
97
            if (state.value) {
98
                rememberDisposable(
99
                    () => collector.push("create"),
100
                    () => collector.push("dispose"))
101
            }
102
        })
103
        testExpected(root)
104
        state.value = true
105
        testExpected(root, "create")
106
        state.value = false
107
        testExpected(root, "dispose")
108
        state.value = false
109
        testExpected(root)
110
    })
111

112
    test("rememberMutableState computes once even if initial state changed", () => {
113
        GlobalStateManager.reset()
114
        const global = mutableState(0)
115
        const root = TestNode.create(() => {
116
            const local = rememberMutableState(global.value)
117
            collector.push("global=" + global.value)
118
            collector.push("local=" + local.value)
119
        })
120
        testExpected(root, "global=0", "local=0")
121
        global.value++
122
        testExpected(root, "global=1", "local=0")
123
        global.value++
124
        testExpected(root, "global=2", "local=0")
125
    })
126
})
127

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

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

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

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