2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-env qunit */
|
2016-04-22 20:31:12 +02:00
|
|
|
import TrackList from '../../../src/js/tracks/track-list.js';
|
2020-11-11 01:11:53 +02:00
|
|
|
import EventTarget from '../../../src/js/event-target.js';
|
2016-04-22 20:31:12 +02:00
|
|
|
|
|
|
|
const newTrack = function(id) {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
addEventListener() {},
|
2016-08-03 21:27:03 +02:00
|
|
|
off() {}
|
2016-04-22 20:31:12 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-03 21:27:03 +02:00
|
|
|
QUnit.module('Track List', {
|
2016-04-22 20:31:12 +02:00
|
|
|
beforeEach() {
|
|
|
|
this.tracks = [newTrack('1'), newTrack('2'), newTrack('3')];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('TrackList\'s length is set correctly', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList.length, this.tracks.length, 'length is ' + this.tracks.length);
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('can get tracks by int and string id', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList.getTrackById('1').id, '1', 'id "1" has id of "1"');
|
|
|
|
assert.equal(trackList.getTrackById('2').id, '2', 'id "2" has id of "2"');
|
|
|
|
assert.equal(trackList.getTrackById('3').id, '3', 'id "3" has id of "3"');
|
2016-04-22 20:31:12 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('length is updated when new tracks are added or removed', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('100'));
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.equal(
|
|
|
|
trackList.length,
|
|
|
|
this.tracks.length + 1,
|
|
|
|
'the length is ' + (this.tracks.length + 1)
|
|
|
|
);
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('101'));
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.equal(
|
|
|
|
trackList.length,
|
|
|
|
this.tracks.length + 2,
|
|
|
|
'the length is ' + (this.tracks.length + 2)
|
|
|
|
);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('101'));
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.equal(
|
|
|
|
trackList.length,
|
|
|
|
this.tracks.length + 1,
|
|
|
|
'the length is ' + (this.tracks.length + 1)
|
|
|
|
);
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('100'));
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.equal(
|
|
|
|
trackList.length,
|
|
|
|
this.tracks.length,
|
|
|
|
'the length is ' + this.tracks.length
|
|
|
|
);
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('can access items by index', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
|
|
|
const length = trackList.length;
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.expect(length);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
2018-09-28 20:58:15 +02:00
|
|
|
assert.equal(
|
|
|
|
trackList[i].id,
|
|
|
|
String(i + 1),
|
|
|
|
'the id of a track matches the index + 1'
|
|
|
|
);
|
2016-04-22 20:31:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('can access new items by index', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('100'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList[3].id, '100', 'id of item at index 3 is 100');
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('101'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList[4].id, '101', 'id of item at index 4 is 101');
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('cannot access removed items by index', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('100'));
|
|
|
|
trackList.addTrack(newTrack('101'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList[3].id, '100', 'id of item at index 3 is 100');
|
|
|
|
assert.equal(trackList[4].id, '101', 'id of item at index 4 is 101');
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('101'));
|
|
|
|
trackList.removeTrack(trackList.getTrackById('100'));
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!trackList[3], 'nothing at index 3');
|
|
|
|
assert.ok(!trackList[4], 'nothing at index 4');
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('new item available at old index', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('100'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList[3].id, '100', 'id of item at index 3 is 100');
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('100'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(!trackList[3], 'nothing at index 3');
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('101'));
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(trackList[3].id, '101', 'id of new item at index 3 is now 101');
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('a "addtrack" event is triggered when new tracks are added', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
let tracks = 0;
|
|
|
|
let adds = 0;
|
2016-08-03 21:27:03 +02:00
|
|
|
const addHandler = (e) => {
|
2016-04-22 20:31:12 +02:00
|
|
|
if (e.track) {
|
|
|
|
tracks++;
|
|
|
|
}
|
|
|
|
adds++;
|
|
|
|
};
|
|
|
|
|
|
|
|
trackList.on('addtrack', addHandler);
|
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('100'));
|
|
|
|
trackList.addTrack(newTrack('101'));
|
2016-04-22 20:31:12 +02:00
|
|
|
|
|
|
|
trackList.off('addtrack', addHandler);
|
|
|
|
trackList.onaddtrack = addHandler;
|
|
|
|
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.addTrack(newTrack('102'));
|
|
|
|
trackList.addTrack(newTrack('103'));
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(adds, 4, 'we got ' + adds + ' "addtrack" events');
|
|
|
|
assert.equal(tracks, 4, 'we got a track with every event');
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('a "removetrack" event is triggered when tracks are removed', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const trackList = new TrackList(this.tracks);
|
2016-04-22 20:31:12 +02:00
|
|
|
let tracks = 0;
|
|
|
|
let rms = 0;
|
2016-08-03 21:27:03 +02:00
|
|
|
const rmHandler = (e) => {
|
2016-04-22 20:31:12 +02:00
|
|
|
if (e.track) {
|
|
|
|
tracks++;
|
|
|
|
}
|
|
|
|
rms++;
|
|
|
|
};
|
|
|
|
|
|
|
|
trackList.on('removetrack', rmHandler);
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('1'));
|
|
|
|
trackList.removeTrack(trackList.getTrackById('2'));
|
2016-04-22 20:31:12 +02:00
|
|
|
|
|
|
|
trackList.off('removetrack', rmHandler);
|
|
|
|
trackList.onremovetrack = rmHandler;
|
2017-01-19 22:16:28 +02:00
|
|
|
trackList.removeTrack(trackList.getTrackById('3'));
|
2016-04-22 20:31:12 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.equal(rms, 3, 'we got ' + rms + ' "removetrack" events');
|
|
|
|
assert.equal(tracks, 3, 'we got a track with every event');
|
2016-04-22 20:31:12 +02:00
|
|
|
});
|
2020-11-11 01:11:53 +02:00
|
|
|
|
|
|
|
QUnit.test('labelchange event is fired for the list when a child track fires labelchange', function(assert) {
|
|
|
|
const trackList = new TrackList([new EventTarget()]);
|
|
|
|
let labelchanges = 0;
|
|
|
|
const labelchangeHandler = (e) => {
|
|
|
|
labelchanges++;
|
|
|
|
};
|
|
|
|
|
|
|
|
trackList.on('labelchange', labelchangeHandler);
|
|
|
|
trackList[0].trigger('labelchange');
|
|
|
|
assert.equal(labelchanges, '1', 'labelchange event is fired on tracklist');
|
|
|
|
});
|
|
|
|
|