Skip to content

Commit 3ddfb5c

Browse files
fix(core): ObservableArray splice with start only (NativeScript#9159)
1 parent c4972d7 commit 3ddfb5c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ObservableArray } from '@nativescript/core/data/observable-array';
2+
import { assert } from 'chai';
3+
4+
describe('observable-array', () => {
5+
describe('splice', () => {
6+
it('removes an item', () => {
7+
const _array = new ObservableArray();
8+
9+
_array.push(1);
10+
_array.push(2);
11+
12+
_array.splice(0, 1);
13+
14+
assert.equal(2, _array.getItem(0));
15+
});
16+
17+
it('replaces an item', () => {
18+
const _array = new ObservableArray();
19+
20+
_array.push(1);
21+
_array.push(2);
22+
23+
_array.splice(0, 1, 3);
24+
25+
assert.equal(3, _array.getItem(0));
26+
});
27+
28+
it('empties on start zero and no delete count', () => {
29+
const _array = new ObservableArray();
30+
31+
_array.push(1);
32+
33+
_array.splice(0);
34+
assert.equal(0, _array.length);
35+
});
36+
37+
it('empties on length set to zero', () => {
38+
const _array = new ObservableArray();
39+
40+
_array.push(1);
41+
_array.push(2);
42+
43+
_array.length = 0;
44+
assert.equal(0, _array.length);
45+
});
46+
});
47+
});

packages/core/data/observable-array/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class ObservableArray<T> extends Observable {
246246
*/
247247
splice(start: number, deleteCount?: number, ...items: any): T[] {
248248
const length = this._array.length;
249-
const result = this._array.splice(start, deleteCount, ...items);
249+
const result = arguments.length === 1 ? this._array.splice(start) : this._array.splice(start, deleteCount, ...items);
250250

251251
this.notify(<ChangedData<T>>{
252252
eventName: CHANGE,

0 commit comments

Comments
 (0)