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"
17
import { UniqueId, KoalaCallsiteKey } from "@koalaui/common"
31
const collector = new Array<string>()
35
readonly page: () => void
36
readonly id: KoalaCallsiteKey
39
function createPage(name: string): Page {
41
id: parseInt(new UniqueId().addString(name).compute().slice(0, 10), 16),
44
(): number => rememberDisposable(() => collector.push("+" + name), () => collector.push("-" + name))
48
function createPages(...names: string[]): ReadonlyArray<Page> {
49
return names.map(createPage)
52
function testExpected(root: State<TestNode>, ...expected: string[]) {
55
assert.deepEqual(collector, expected)
56
if (expected.length > 0) testExpected(root)
61
content: (array: ReadonlyArray<Page>) => void
63
GlobalStateManager.reset()
64
const state = mutableState(createPages())
65
const root = TestNode.create(() => content(state.value))
67
state.value = createPages("three")
68
testExpected(root, "+three")
69
state.value = createPages("one", "three")
70
testExpected(root, "+one")
71
state.value = createPages("one", "three", "five")
72
testExpected(root, "+five")
73
state.value = createPages("one", "two", "three", "four", "five")
74
testExpected(root, "+two", "+four")
79
content: (array: ReadonlyArray<Page>) => void
81
GlobalStateManager.reset()
82
const state = mutableState(createPages("one", "two", "three", "four", "five"))
83
const root = TestNode.create(() => content(state.value))
84
testExpected(root, "+one", "+two", "+three", "+four", "+five")
85
state.value = createPages("one", "three", "five")
86
testExpected(root, "-two", "-four")
87
state.value = createPages("three", "five")
88
testExpected(root, "-one")
89
state.value = createPages("three")
90
testExpected(root, "-five")
91
state.value = createPages()
92
testExpected(root, "-three")
97
content: (array: ReadonlyArray<Page>) => void
99
GlobalStateManager.reset()
100
const state = mutableState(createPages("one", "two", "three", "four", "five"))
101
const root = TestNode.create(() => content(state.value))
102
testExpected(root, "+one", "+two", "+three", "+four", "+five")
103
state.value = createPages("two", "one", "three", "four", "five")
104
testExpected(root, "-one", "+one")
105
state.value = createPages("two", "four", "one", "three", "five")
106
testExpected(root, "-one", "-three", "+one", "+three")
109
suite("repeat tests", () => {
111
test("Repeat", () => {
112
GlobalStateManager.reset()
113
const state = mutableState(createPages())
114
const root = TestNode.create(() => Repeat(state.value.length, index => state.value[index].page())) // index-based key
116
state.value = createPages("one")
117
testExpected(root, "+one")
118
state.value = createPages("one", "two", "three")
119
testExpected(root, "+two", "+three")
120
state.value = createPages("one", "two")
121
testExpected(root, "-three")
122
state.value = createPages("two")
123
testExpected(root, "-two") // because of index-based key; should be "-one"
124
state.value = createPages("one", "two")
125
testExpected(root, "+two") // because of index-based key; should be "+one"
129
test("RepeatWithKey.insert", () => testInsert(array => RepeatWithKey(
131
index => array[index].id,
132
index => array[index].page())))
134
test("RepeatByArray.insert", () => testInsert(array => RepeatByArray(
136
element => element.id,
137
element => element.page())))
139
test("RepeatRange.insert", () => testInsert(array => RepeatRange(
142
index => array[index],
143
element => element.id,
144
element => element.page())))
147
test("RepeatWithKey.remove", () => testRemove(array => RepeatWithKey(
149
index => array[index].id,
150
index => array[index].page())))
152
test("RepeatByArray.remove", () => testRemove(array => RepeatByArray(
154
element => element.id,
155
element => element.page())))
157
test("RepeatRange.remove", () => testRemove(array => RepeatRange(
160
index => array[index],
161
element => element.id,
162
element => element.page())))
165
test("RepeatWithKey.swap", () => testSwap(array => RepeatWithKey(
167
index => array[index].id,
168
index => array[index].page())))
170
test("RepeatByArray.swap", () => testSwap(array => RepeatByArray(
172
element => element.id,
173
element => element.page())))
175
test("RepeatRange.swap", () => testSwap(array => RepeatRange(
178
index => array[index],
179
element => element.id,
180
element => element.page())))