From 972f84d9bf6cefd89caa651ae5ab001f32e638dd Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Sun, 12 Apr 2020 19:49:03 +0200 Subject: [PATCH] Fix error when deleting last element in list --- js/src/trackviewmodel.js | 5 ++++- js/src/userviewmodel.js | 5 ++++- js/test/trackviewmodel.test.js | 13 +++++++++++++ js/test/userviewmodel.test.js | 13 +++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/js/src/trackviewmodel.js b/js/src/trackviewmodel.js index 10b00bb..b2ef298 100644 --- a/js/src/trackviewmodel.js +++ b/js/src/trackviewmodel.js @@ -277,11 +277,14 @@ export default class TrackViewModel extends ViewModel { } onTrackDeleted() { - const index = this.model.trackList.indexOf(this.state.currentTrack); + let index = this.model.trackList.indexOf(this.state.currentTrack); this.state.currentTrack = null; if (index !== -1) { this.model.trackList.splice(index, 1); 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; } else { this.model.currentTrackId = ''; diff --git a/js/src/userviewmodel.js b/js/src/userviewmodel.js index 59d5bd8..f4a7869 100644 --- a/js/src/userviewmodel.js +++ b/js/src/userviewmodel.js @@ -119,11 +119,14 @@ export default class UserViewModel extends ViewModel { } onUserDeleted() { - const index = this.model.userList.indexOf(this.state.currentUser); + let index = this.model.userList.indexOf(this.state.currentUser); this.state.currentUser = null; if (index !== -1) { this.model.userList.splice(index, 1); 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; } else { this.model.currentUserId = '0'; diff --git a/js/test/trackviewmodel.test.js b/js/test/trackviewmodel.test.js index f8994cb..67474b6 100644 --- a/js/test/trackviewmodel.test.js +++ b/js/test/trackviewmodel.test.js @@ -632,6 +632,19 @@ describe('TrackViewModel tests', () => { 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', () => { // given vm.model.trackList = [ track1 ]; diff --git a/js/test/userviewmodel.test.js b/js/test/userviewmodel.test.js index 3a7fbac..9dedeac 100644 --- a/js/test/userviewmodel.test.js +++ b/js/test/userviewmodel.test.js @@ -281,6 +281,19 @@ describe('UserViewModel tests', () => { 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', () => { // given vm.model.userList = [ user1 ];