mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
fix(core): ObservableArray tests and a typo (#8968)
This commit is contained in:

committed by
Nathan Walker

parent
0afea8681c
commit
5c1b7f6d76
@ -360,6 +360,85 @@ export const test_ObservableArray_spliceShouldRemoveSpecifiedNumberOfElementsSta
|
||||
export const test_ObservableArray_spliceShouldRemoveSpecifiedNumberOfElementsStartingFromSpecifiedIndexAndRaiseChangeEventWithCorrectArgs = function () {
|
||||
let result: ChangedData<number>;
|
||||
|
||||
// >> observable-array-splice-change
|
||||
const array = new ObservableArray([1, 2, 3, 4]);
|
||||
|
||||
array.on(ObservableArray.changeEvent, (args: ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
// args.eventName is "change".
|
||||
// args.action is "splice".
|
||||
// args.index is the start index.
|
||||
// args.removed.length is equal to the number of deleted items.
|
||||
// args.addedCount is 0.
|
||||
|
||||
// >> (hide)
|
||||
result = args;
|
||||
// << (hide)
|
||||
});
|
||||
|
||||
array.splice(1, 2);
|
||||
// << observable-array-splice-change
|
||||
|
||||
TKUnit.assert(result.eventName === ObservableArray.changeEvent && result.action === ChangeType.Splice && result.removed.length === 2 && result.index === 1 && result.addedCount === 0, "ObservableArray splice() should raise 'change' event with correct args!");
|
||||
};
|
||||
|
||||
export const test_ObservableArray_spliceShouldAddSpecifiedNumberOfElementsStartingFromSpecifiedIndexAndRaiseChangeEventWithCorrectArgs = function () {
|
||||
let result: ChangedData<number>;
|
||||
|
||||
// >> observable-array-splice-change
|
||||
const array = new ObservableArray([0]);
|
||||
|
||||
array.on(ObservableArray.changeEvent, (args: ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
// args.eventName is "change".
|
||||
// args.action is "splice".
|
||||
// args.index is the start index.
|
||||
// args.removed.length is equal to the number of deleted items.
|
||||
// args.addedCount is 0.
|
||||
|
||||
// >> (hide)
|
||||
result = args;
|
||||
// << (hide)
|
||||
});
|
||||
|
||||
// Because their is only one item in the above array the item index should be
|
||||
// normalized to Index 1.
|
||||
array.splice(2, 0, 1);
|
||||
// << observable-array-splice-change
|
||||
|
||||
TKUnit.assert(result.eventName === ObservableArray.changeEvent && result.action === ChangeType.Splice && result.removed.length === 0 && result.index === 1 && result.addedCount === 1, "ObservableArray splice() should raise 'change' event with correct args!");
|
||||
};
|
||||
|
||||
export const test_ObservableArray_spliceShouldAddDeleteSpecifiedNumberOfElementsStartingFromSpecifiedIndexAndRaiseChangeEventWithCorrectArgs = function () {
|
||||
let result: ChangedData<number>;
|
||||
|
||||
// >> observable-array-splice-change
|
||||
const array = new ObservableArray([0]);
|
||||
|
||||
array.on(ObservableArray.changeEvent, (args: ChangedData<number>) => {
|
||||
// Argument (args) is ChangedData<T>.
|
||||
// args.eventName is "change".
|
||||
// args.action is "splice".
|
||||
// args.index is the start index.
|
||||
// args.removed.length is equal to the number of deleted items.
|
||||
// args.addedCount is 1.
|
||||
|
||||
// >> (hide)
|
||||
result = args;
|
||||
// << (hide)
|
||||
});
|
||||
|
||||
// Because we are starting at index 2, their is NOTHING to delete
|
||||
// So the Starting index should actually be normalized to Index 1
|
||||
array.splice(2, 2, 1);
|
||||
// << observable-array-splice-change
|
||||
|
||||
TKUnit.assert(result.eventName === ObservableArray.changeEvent && result.action === ChangeType.Splice && result.removed.length === 0 && result.index === 1 && result.addedCount === 1, "ObservableArray splice() should raise 'change' event with correct args!");
|
||||
};
|
||||
|
||||
export const test_ObservableArray_spliceShouldRemoveSpecifiedNumberOfElementsStartingFromSpecifiedIndexAndRaiseChangeEventWithCorrectedArgs = function () {
|
||||
let result: ChangedData<number>;
|
||||
|
||||
// >> observable-array-splice-change
|
||||
const array = new ObservableArray([1, 2, 3]);
|
||||
|
||||
@ -526,6 +605,28 @@ export const test_ObservableArray_settingLengthToSomethingPerformsSplice = funct
|
||||
TKUnit.assertTrue(changeRaised);
|
||||
};
|
||||
|
||||
export const test_ObservableArray_settingLengthToSomethingPerformsSpliceAdded = function () {
|
||||
const array = new ObservableArray([1, 2, 3]);
|
||||
let changeRaised = false;
|
||||
|
||||
array.on('change', (args: ChangedData<number>) => {
|
||||
changeRaised = true;
|
||||
TKUnit.assertEqual(args.object, array);
|
||||
TKUnit.assertEqual(args.eventName, 'change');
|
||||
TKUnit.assertEqual(args.action, ChangeType.Splice);
|
||||
|
||||
// Because the array only has 3 elements, the index it starts the change at is #2
|
||||
TKUnit.assertEqual(args.index, 3);
|
||||
TKUnit.assertEqual(args.addedCount, 2);
|
||||
TKUnit.arrayAssert(args.removed, []);
|
||||
});
|
||||
|
||||
array.length = 5;
|
||||
|
||||
TKUnit.assertEqual(array.length, 5);
|
||||
TKUnit.assertTrue(changeRaised);
|
||||
};
|
||||
|
||||
const array = new ObservableArray();
|
||||
|
||||
// We do not have indexer!
|
||||
|
Reference in New Issue
Block a user