Fix error when deleting last element in list
This commit is contained in:
parent
874ce4fc37
commit
972f84d9bf
@ -277,11 +277,14 @@ export default class TrackViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onTrackDeleted() {
|
onTrackDeleted() {
|
||||||
const index = this.model.trackList.indexOf(this.state.currentTrack);
|
let index = this.model.trackList.indexOf(this.state.currentTrack);
|
||||||
this.state.currentTrack = null;
|
this.state.currentTrack = null;
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.model.trackList.splice(index, 1);
|
this.model.trackList.splice(index, 1);
|
||||||
if (this.model.trackList.length) {
|
if (this.model.trackList.length) {
|
||||||
|
if (index >= this.model.trackList.length) {
|
||||||
|
index = this.model.trackList.length - 1;
|
||||||
|
}
|
||||||
this.model.currentTrackId = this.model.trackList[index].listValue;
|
this.model.currentTrackId = this.model.trackList[index].listValue;
|
||||||
} else {
|
} else {
|
||||||
this.model.currentTrackId = '';
|
this.model.currentTrackId = '';
|
||||||
|
@ -119,11 +119,14 @@ export default class UserViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onUserDeleted() {
|
onUserDeleted() {
|
||||||
const index = this.model.userList.indexOf(this.state.currentUser);
|
let index = this.model.userList.indexOf(this.state.currentUser);
|
||||||
this.state.currentUser = null;
|
this.state.currentUser = null;
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.model.userList.splice(index, 1);
|
this.model.userList.splice(index, 1);
|
||||||
if (this.model.userList.length) {
|
if (this.model.userList.length) {
|
||||||
|
if (index >= this.model.userList.length) {
|
||||||
|
index = this.model.userList.length - 1;
|
||||||
|
}
|
||||||
this.model.currentUserId = this.model.userList[index].listValue;
|
this.model.currentUserId = this.model.userList[index].listValue;
|
||||||
} else {
|
} else {
|
||||||
this.model.currentUserId = '0';
|
this.model.currentUserId = '0';
|
||||||
|
@ -632,6 +632,19 @@ describe('TrackViewModel tests', () => {
|
|||||||
expect(vm.state.currentTrack).toBe(null);
|
expect(vm.state.currentTrack).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should remove current last track from track list and set new current track id', () => {
|
||||||
|
// given
|
||||||
|
vm.model.trackList = [ track1, track2 ];
|
||||||
|
vm.state.currentTrack = track2;
|
||||||
|
vm.model.currentTrackId = track2.listValue;
|
||||||
|
// when
|
||||||
|
vm.onTrackDeleted();
|
||||||
|
// then
|
||||||
|
expect(vm.model.trackList.length).toBe(1);
|
||||||
|
expect(vm.model.currentTrackId).toBe(track1.listValue);
|
||||||
|
expect(vm.state.currentTrack).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
it('should remove last remaining element from track list and set empty track id', () => {
|
it('should remove last remaining element from track list and set empty track id', () => {
|
||||||
// given
|
// given
|
||||||
vm.model.trackList = [ track1 ];
|
vm.model.trackList = [ track1 ];
|
||||||
|
@ -281,6 +281,19 @@ describe('UserViewModel tests', () => {
|
|||||||
expect(vm.state.currentUser).toBe(null);
|
expect(vm.state.currentUser).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should remove current last user from user list and set new current user id', () => {
|
||||||
|
// given
|
||||||
|
vm.model.userList = [ user1, user2 ];
|
||||||
|
vm.state.currentUser = user2;
|
||||||
|
vm.model.currentUserId = user2.listValue;
|
||||||
|
// when
|
||||||
|
vm.onUserDeleted();
|
||||||
|
// then
|
||||||
|
expect(vm.model.userList.length).toBe(1);
|
||||||
|
expect(vm.model.currentUserId).toBe(user1.listValue);
|
||||||
|
expect(vm.state.currentUser).toBe(null);
|
||||||
|
});
|
||||||
|
|
||||||
it('should remove last remaining element from user list and set empty user id', () => {
|
it('should remove last remaining element from user list and set empty user id', () => {
|
||||||
// given
|
// given
|
||||||
vm.model.userList = [ user1 ];
|
vm.model.userList = [ user1 ];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user