forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual-array-tests.ts
More file actions
212 lines (160 loc) · 8.52 KB
/
virtual-array-tests.ts
File metadata and controls
212 lines (160 loc) · 8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as TKUnit from "../TKUnit";
import * as types from "tns-core-modules/utils/types";
// >> virtual-array-require
import * as virtualArrayModule from "tns-core-modules/data/virtual-array";
// << virtual-array-require
require("globals");
export var test_VirtualArray_shouldCreateArrayFromSpecifiedLength = function () {
var array = new virtualArrayModule.VirtualArray<number>(100);
TKUnit.assert(array.length === 100, "VirtualArray<T> should create array from specified length!");
};
export var test_VirtualArray_setItemShouldSetCorrectItem = function () {
var array = new virtualArrayModule.VirtualArray<number>(100);
array.setItem(0, 0);
TKUnit.assert(array.getItem(0) === 0, "VirtualArray<T> setItem() should set correct item!");
};
export var test_VirtualArray_setItemShouldRaiseChangeEventWhenYouSetDifferentItem = function () {
// >> virtual-array-itemsloading
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
// >> (hide)
var result: virtualArrayModule.ChangedData<number>;
var index = 0;
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
result = args;
});
array.setItem(index, 0);
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Update &&
result.removed.length === 1 && result.index === index && result.addedCount === 1, "VirtualArray<T> setItem() should raise 'change' event with correct args!");
result = undefined;
array.setItem(index, 1);
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Update &&
result.removed.length === 1 && result.index === index && result.addedCount === 1, "VirtualArray<T> setItem() should raise 'change' event with correct args!");
// << (hide)
array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
// Argument (args) is ItemsLoading.
// args.index is start index of the page where the requested index is located.
// args.count number of requested items.
//
// Note: Virtual array will divide total number of items to pages using "loadSize" property value. When you request an
// item at specific index the array will raise "itemsLoading" event with "ItemsLoading" argument index set to the first index of the requested page
// and count set to number of items in this page.
//
// Important: If you have already loaded items in the requested page the array will raise multiple times "itemsLoading" event to request
// all ranges of still not loaded items in this page.
var itemsToLoad = new Array<number>();
for (var i = 0; i < args.count; i++) {
itemsToLoad.push(i + args.index);
}
array.load(args.index, itemsToLoad);
});
// << virtual-array-itemsloading
};
export var test_VirtualArray_loadShouldRaiseChangeEventWithCorrectArgs = function () {
// >> virtual-array-change
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
// >> (hide)
var result: virtualArrayModule.ChangedData<number>;
var index = 0;
// << (hide)
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
// Argument (args) is ChangedData<T>.
// args.eventName is "change".
// args.action is "update".
// args.removed.length and result.addedCount are equal to number of loaded items with load() method.
// >> (hide)
result = args;
// << (hide)
});
var itemsToLoad = [0, 1, 2];
array.load(index, itemsToLoad);
// << virtual-array-change
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Update &&
result.removed.length === itemsToLoad.length && result.index === index && result.addedCount === itemsToLoad.length,
"VirtualArray<T> load() should raise 'change' event with correct args!");
};
export var test_VirtualArray_lengthIncreaseShouldRaiseChangeEventWithCorrectArgs = function () {
// >> virtual-array-lenght
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
// >> (hide)
var result: virtualArrayModule.ChangedData<number>;
var index = array.length;
// << (hide)
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
// Argument (args) is ChangedData<T>.
// args.eventName is "change".
// args.action is "add".
// args.removed.length is 0, result.addedCount is equal to the delta between new and old "length" property values.
// >> (hide)
result = args;
// << (hide)
});
array.length += array.loadSize;
// << virtual-array-lenght
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Add
&& result.index === index && result.addedCount === array.loadSize && result.removed.length === 0,
"VirtualArray<T> length++ should raise 'change' event with correct args!");
};
export var test_VirtualArray_lengthDecreaseShouldRaiseChangeEventWithCorrectArgs = function () {
// <snippet module="data/virtual-array" title="virtual-array">
// ### Handle "change" event when you increase "length" property.
// ``` JavaScript
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
// <hide>
var result: virtualArrayModule.ChangedData<number>;
var index = array.length;
// </hide>
array.on(virtualArrayModule.VirtualArray.changeEvent, (args: virtualArrayModule.ChangedData<number>) => {
// Argument (args) is ChangedData<T>.
// args.eventName is "change".
// args.action is "remove".
// result.addedCount is 0, args.removed.length is equal to the delta between new and old "length" property values.
// <hide>
result = args;
// </hide>
});
array.length -= array.loadSize;
TKUnit.assert(result && result.eventName === "change" && result.action === virtualArrayModule.ChangeType.Delete
&& result.index === index && result.removed.length === array.loadSize && result.addedCount === 0,
"VirtualArray<T> length++ should raise 'change' event with correct args!");
};
export var test_VirtualArray_shouldRaiseItemsLoadingIfIndexIsNotLoadedAndGetItemIsCalled = function () {
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
var result: virtualArrayModule.ItemsLoading;
array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
result = args;
});
array.getItem(0);
TKUnit.assert(result.eventName === virtualArrayModule.VirtualArray.itemsLoadingEvent && result.index === 0 &&
result.count === array.loadSize, "VirtualArray<T> getItem() should raise 'itemsLoading' event with correct args if item is not loaded!");
};
export var test_VirtualArray_shouldNotRaiseItemsLoadingIfIndexIsLoadedAndGetItemIsCalled = function () {
var array = new virtualArrayModule.VirtualArray<number>(100);
array.setItem(0, 0);
array.loadSize = 15;
var result: virtualArrayModule.ItemsLoading;
array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
result = args;
});
array.getItem(0);
TKUnit.assert(types.isUndefined(result), "VirtualArray<T> getItem() should not raise 'itemsLoading' event if item is loaded!");
};
export var test_VirtualArray_shouldRaiseItemsLoadingIfIndexIsNotLoadedAndGetItemIsCalledCorrectNumberOfTimesWithCorrectArgs = function () {
var array = new virtualArrayModule.VirtualArray<number>(100);
array.loadSize = 15;
array.setItem(0, 0);
array.setItem(5, 5);
var result = new Array<virtualArrayModule.ItemsLoading>();
array.on(virtualArrayModule.VirtualArray.itemsLoadingEvent, (args: virtualArrayModule.ItemsLoading) => {
result.push(args);
});
array.getItem(1);
TKUnit.assert(result.length === 2 &&
result[0].eventName === virtualArrayModule.VirtualArray.itemsLoadingEvent && result[0].index === 1 && result[0].count === 4 &&
result[1].eventName === virtualArrayModule.VirtualArray.itemsLoadingEvent && result[1].index === 6 && result[1].count === 9,
"VirtualArray<T> getItem() should raise 'itemsLoading' event with correct args!");
};