Minor cleanups

This commit is contained in:
Bartek Fabiszewski 2019-12-05 22:55:36 +01:00
parent d3b1fa8937
commit cd101b5120
6 changed files with 38 additions and 22 deletions

View File

@ -38,7 +38,7 @@ export default class uObserve {
} else if (typeof p1 === 'function') { } else if (typeof p1 === 'function') {
this.observeRecursive(obj, p1); this.observeRecursive(obj, p1);
} else { } else {
throw new Error('Invalid arguments'); throw new Error('Invalid argument for observe');
} }
} }
@ -198,7 +198,7 @@ export default class uObserve {
this.unobserveRecursive(obj, p1); this.unobserveRecursive(obj, p1);
} }
} else { } else {
throw new Error('Invalid arguments'); throw new Error('Invalid argument for unobserve');
} }
} }
@ -295,10 +295,10 @@ export default class uObserve {
return; return;
} }
observers = obj._observers[property]; observers = obj._observers[property];
console.log(`Removing observer for ${property}`) console.log(`Removing observer for ${property}`);
} else { } else {
observers = obj._observers; observers = obj._observers;
console.log('Removing observer for object…') console.log('Removing observer for object…');
} }
observers.forEach((obs) => { observers.forEach((obs) => {
if (obs === observer) { if (obs === observer) {

View File

@ -35,10 +35,10 @@ import uUtils from './utils.js';
* @property {string} trackname * @property {string} trackname
* @property {number} trackid * @property {number} trackid
* @property {number} timestamp * @property {number} timestamp
* @property {number} distance * @property {number} meters Distance to previous position
* @property {number} seconds * @property {number} seconds Time difference to previous position
* @property {number} totalDistance * @property {number} totalMeters Distance to first position
* @property {number} totalSeconds * @property {number} totalSeconds Time difference to first position
*/ */
export default class uPosition { export default class uPosition {
@ -63,9 +63,9 @@ export default class uPosition {
position.trackname = uUtils.getString(pos.trackname); position.trackname = uUtils.getString(pos.trackname);
position.trackid = uUtils.getInteger(pos.trackid); position.trackid = uUtils.getInteger(pos.trackid);
position.timestamp = uUtils.getInteger(pos.timestamp); position.timestamp = uUtils.getInteger(pos.timestamp);
position.distance = uUtils.getInteger(pos.distance); position.meters = uUtils.getInteger(pos.meters);
position.seconds = uUtils.getInteger(pos.seconds); position.seconds = uUtils.getInteger(pos.seconds);
position.totalDistance = 0; position.totalMeters = 0;
position.totalSeconds = 0; position.totalSeconds = 0;
return position; return position;
} }

View File

@ -99,7 +99,7 @@ describe('Ajax tests', () => {
expect(result).toEqual(validResponse); expect(result).toEqual(validResponse);
done(); done();
}) })
.catch(() => done.fail('reject callback called')); .catch((e) => done.fail(`reject callback called (${e})`));
}); });
it('should make successful request and return error with message', (done) => { it('should make successful request and return error with message', (done) => {

View File

@ -34,7 +34,7 @@ describe('Observe tests', () => {
describe('when object is observed', () => { describe('when object is observed', () => {
it('should throw error if observer is missing', () => { it('should throw error if observer is missing', () => {
expect(() => { uObserve.observe(object, 'observed'); }).toThrow(new Error('Invalid arguments')); expect(() => { uObserve.observe(object, 'observed'); }).toThrow(new Error('Invalid argument for observe'));
}); });
it('should notify observers when observed property is modified', () => { it('should notify observers when observed property is modified', () => {
@ -181,7 +181,7 @@ describe('Observe tests', () => {
it('should throw error if removed observer is missing', () => { it('should throw error if removed observer is missing', () => {
expect(() => { expect(() => {
uObserve.unobserve(object, 'unobserved'); uObserve.unobserve(object, 'unobserved');
}).toThrow(new Error('Invalid arguments')); }).toThrow(new Error('Invalid argument for unobserve'));
}); });
it('should not notify observers when unobserved property is modified', () => { it('should not notify observers when unobserved property is modified', () => {

View File

@ -38,7 +38,7 @@ describe('Position tests', () => {
'username', 'username',
'trackid', 'trackid',
'trackname', 'trackname',
'distance', 'meters',
'seconds' 'seconds'
]; ];
const properties = nullableProperties.concat(nonNullableProperties); const properties = nullableProperties.concat(nonNullableProperties);
@ -57,7 +57,7 @@ describe('Position tests', () => {
let username; let username;
let trackid; let trackid;
let trackname; let trackname;
let distance; let meters;
let seconds; let seconds;
let jsonPosition; let jsonPosition;
@ -77,7 +77,7 @@ describe('Position tests', () => {
username = 'test'; username = 'test';
trackid = 134; trackid = 134;
trackname = 'Test name'; trackname = 'Test name';
distance = 0; meters = 0;
seconds = 0; seconds = 0;
jsonPosition = { jsonPosition = {
@ -95,7 +95,7 @@ describe('Position tests', () => {
'username': username, 'username': username,
'trackid': trackid, 'trackid': trackid,
'trackname': trackname, 'trackname': trackname,
'distance': distance, 'meters': meters,
'seconds': seconds 'seconds': seconds
}; };
}); });
@ -117,7 +117,7 @@ describe('Position tests', () => {
expect(position.username).toBe(username); expect(position.username).toBe(username);
expect(position.trackid).toBe(trackid); expect(position.trackid).toBe(trackid);
expect(position.trackname).toBe(trackname); expect(position.trackname).toBe(trackname);
expect(position.distance).toBe(distance); expect(position.meters).toBe(meters);
expect(position.seconds).toBe(seconds); expect(position.seconds).toBe(seconds);
}); });
@ -129,7 +129,7 @@ describe('Position tests', () => {
// when // when
delete posCopy[prop]; delete posCopy[prop];
// then // then
expect(() => { uPosition.fromJson(posCopy); }).toThrow(new Error('Illegal value')); expect(() => { uPosition.fromJson(posCopy); }).toThrow(new Error('Invalid value'));
}); });
}); });
}); });
@ -142,7 +142,7 @@ describe('Position tests', () => {
// when // when
posCopy[prop] = null; posCopy[prop] = null;
// then // then
expect(() => { uPosition.fromJson(posCopy); }).toThrow(new Error('Illegal value')); expect(() => { uPosition.fromJson(posCopy); }).toThrow(new Error('Invalid value'));
}); });
}); });
}); });
@ -156,7 +156,7 @@ describe('Position tests', () => {
posCopy[prop] = null; posCopy[prop] = null;
let pos = {}; let pos = {};
// then // then
expect(() => { pos = uPosition.fromJson(posCopy); }).not.toThrow(new Error('Illegal value')); expect(() => { pos = uPosition.fromJson(posCopy); }).not.toThrow(new Error('Invalid value'));
expect(pos[prop]).toBeNull(); expect(pos[prop]).toBeNull();
}); });
}); });

View File

@ -60,6 +60,7 @@ describe('User tests', () => {
describe('ajax tests', () => { describe('ajax tests', () => {
const validResponse = [ { 'id': 1, 'login': 'test' }, { 'id': 2, 'login': 'test2' }, { 'id': 18, 'login': 'demo' } ]; const validResponse = [ { 'id': 1, 'login': 'test' }, { 'id': 2, 'login': 'test2' }, { 'id': 18, 'login': 'demo' } ];
const invalidResponse = [ { 'login': 'test' }, { 'id': 2, 'login': 'test2' }, { 'id': 18, 'login': 'demo' } ];
beforeEach(() => { beforeEach(() => {
spyOn(XMLHttpRequest.prototype, 'open').and.callThrough(); spyOn(XMLHttpRequest.prototype, 'open').and.callThrough();
@ -75,11 +76,26 @@ describe('User tests', () => {
// then // then
uUser.fetchList() uUser.fetchList()
.then((result) => { .then((result) => {
expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('GET', 'utils/getusers.php', true);
expect(result).toEqual(jasmine.arrayContaining([ new uUser(1, 'test') ])); expect(result).toEqual(jasmine.arrayContaining([ new uUser(1, 'test') ]));
expect(result.length).toBe(3); expect(result.length).toBe(3);
done(); done();
}) })
.catch(() => done.fail('reject callback called')); .catch((e) => done.fail(`reject callback called (${e})`));
});
it('should throw error on invalid data in JSON', (done) => {
// when
spyOnProperty(XMLHttpRequest.prototype, 'responseText').and.returnValue(JSON.stringify(invalidResponse));
// then
uUser.fetchList()
.then(() => {
done.fail('resolve callback called');
})
.catch((e) => {
expect(e).toEqual(jasmine.any(Error));
done();
});
}); });
}); });