idlize

Форк
0
/
contextLocal.test.ts 
63 строки · 2.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
    State,
19
    TestNode,
20
    contextLocalScope,
21
    contextLocalValue,
22
    mutableState,
23
    testTick,
24
} from "../../src"
25

26
const collector = new Array<string>()
27

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

35
suite("contextLocal tests", () => {
36

37
    test("contextLocalScope ensures name is not changed", () => {
38
        const state = mutableState("first")
39
        const root = TestNode.create(() => {
40
            contextLocalScope(state.value, "value", () => {
41
                collector.push(contextLocalValue(state.value))
42
            })
43
        })
44
        testExpected(root, "value")
45
        state.value = "second"
46
        assert.throws(() => testTick(root))
47
    })
48

49
    test("contextLocalScope propagates state immediately", () => {
50
        const state = mutableState("first")
51
        const root = TestNode.create(() => {
52
            collector.push("ui:" + state.value)
53
            contextLocalScope("parameter", state.value, () => {
54
                collector.push("state:" + contextLocalValue("parameter"))
55
            })
56
        })
57
        testExpected(root, "ui:first", "state:first")
58
        state.value = "second"
59
        testExpected(root, "ui:second", "state:second")
60
        state.value = "third"
61
        testExpected(root, "ui:third", "state:third")
62
    })
63
})
64

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

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

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

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