From 869d34dae1b9fa8d99168720e2222e907d8a78e1 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Sat, 28 Dec 2019 21:50:16 +0100 Subject: [PATCH] Add track update methods --- js/src/track.js | 29 +++++++++++++++++++++++++++++ js/test/track.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/js/src/track.js b/js/src/track.js index bacc700..96f2971 100644 --- a/js/src/track.js +++ b/js/src/track.js @@ -52,6 +52,11 @@ export default class uTrack extends uPositionSet { this.listItem(id, name); } + setName(name) { + this.name = name; + this.listText = name; + } + clear() { super.clear(); this.maxId = 0; @@ -214,4 +219,28 @@ export default class uTrack extends uPositionSet { }); } + delete() { + return uTrack.update({ + action: 'delete', + trackid: this.id + }); + } + + saveMeta() { + return uTrack.update({ + action: 'update', + trackid: this.id, + trackname: this.name + }); + } + + /** + * Save track data + * @param {Object} data + * @return {Promise} + */ + static update(data) { + return uAjax.post('utils/handletrack.php', data); + } + } diff --git a/js/test/track.test.js b/js/test/track.test.js index 1e78152..3685e89 100644 --- a/js/test/track.test.js +++ b/js/test/track.test.js @@ -116,6 +116,16 @@ describe('Track tests', () => { expect(track.listText).toBe(name); }); + it('should set track name', () => { + // given + const newName = 'newName'; + // when + track.setName(newName); + // then + expect(track.name).toBe(newName); + expect(track.listText).toBe(newName); + }); + it('should clear positions data', () => { // given track.positions.push(new uPosition()); @@ -408,6 +418,32 @@ describe('Track tests', () => { expect(uUtils.openUrl).toHaveBeenCalledWith(`utils/export.php?type=${type}&userid=${track.user.id}&trackid=${track.id}`); }); + it('should delete track', (done) => { + // given + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify([])); + // when + track.delete() + .then(() => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('POST', 'utils/handletrack.php', true); + expect(XMLHttpRequest.prototype.send).toHaveBeenCalledWith(`action=delete&trackid=${track.id}`); + done(); + }) + .catch((e) => done.fail(`reject callback called (${e})`)); + }); + + it('should save track meta', (done) => { + // given + spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify([])); + // when + track.saveMeta() + .then(() => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('POST', 'utils/handletrack.php', true); + expect(XMLHttpRequest.prototype.send).toHaveBeenCalledWith(`action=update&trackid=${track.id}&trackname=${track.name}`); + done(); + }) + .catch((e) => done.fail(`reject callback called (${e})`)); + }); + }); });