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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
16
import { assert } from "chai"
26
const collector = new Array<string>()
28
function testExpected(root: State<TestNode>, ...expected: string[]) {
31
assert.deepEqual(collector, expected)
32
if (expected.length > 0) testExpected(root)
35
suite("contextLocal tests", () => {
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))
44
testExpected(root, "value")
45
state.value = "second"
46
assert.throws(() => testTick(root))
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"))
57
testExpected(root, "ui:first", "state:first")
58
state.value = "second"
59
testExpected(root, "ui:second", "state:second")
61
testExpected(root, "ui:third", "state:third")