Add track update methods

This commit is contained in:
Bartek Fabiszewski 2019-12-28 21:50:16 +01:00
parent 76f3855c87
commit 869d34dae1
2 changed files with 65 additions and 0 deletions

View File

@ -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<void, Error>}
*/
static update(data) {
return uAjax.post('utils/handletrack.php', data);
}
}

View File

@ -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})`));
});
});
});