From cd101b5120c23421c9931cbbd3fb672d530353a3 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Thu, 5 Dec 2019 22:55:36 +0100 Subject: [PATCH] Minor cleanups --- js/src/observe.js | 8 ++++---- js/src/position.js | 12 ++++++------ js/test/ajax.test.js | 2 +- js/test/observe.test.js | 4 ++-- js/test/position.test.js | 16 ++++++++-------- js/test/user.test.js | 18 +++++++++++++++++- 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/js/src/observe.js b/js/src/observe.js index 9e1dc11..65a8415 100644 --- a/js/src/observe.js +++ b/js/src/observe.js @@ -38,7 +38,7 @@ export default class uObserve { } else if (typeof p1 === 'function') { this.observeRecursive(obj, p1); } 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); } } else { - throw new Error('Invalid arguments'); + throw new Error('Invalid argument for unobserve'); } } @@ -295,10 +295,10 @@ export default class uObserve { return; } observers = obj._observers[property]; - console.log(`Removing observer for ${property}…`) + console.log(`Removing observer for ${property}…`); } else { observers = obj._observers; - console.log('Removing observer for object…') + console.log('Removing observer for object…'); } observers.forEach((obs) => { if (obs === observer) { diff --git a/js/src/position.js b/js/src/position.js index 96d4064..951a3f6 100644 --- a/js/src/position.js +++ b/js/src/position.js @@ -35,10 +35,10 @@ import uUtils from './utils.js'; * @property {string} trackname * @property {number} trackid * @property {number} timestamp - * @property {number} distance - * @property {number} seconds - * @property {number} totalDistance - * @property {number} totalSeconds + * @property {number} meters Distance to previous position + * @property {number} seconds Time difference to previous position + * @property {number} totalMeters Distance to first position + * @property {number} totalSeconds Time difference to first position */ export default class uPosition { @@ -63,9 +63,9 @@ export default class uPosition { position.trackname = uUtils.getString(pos.trackname); position.trackid = uUtils.getInteger(pos.trackid); 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.totalDistance = 0; + position.totalMeters = 0; position.totalSeconds = 0; return position; } diff --git a/js/test/ajax.test.js b/js/test/ajax.test.js index adae208..de6ad1d 100644 --- a/js/test/ajax.test.js +++ b/js/test/ajax.test.js @@ -99,7 +99,7 @@ describe('Ajax tests', () => { expect(result).toEqual(validResponse); 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) => { diff --git a/js/test/observe.test.js b/js/test/observe.test.js index ec36e32..37be38c 100644 --- a/js/test/observe.test.js +++ b/js/test/observe.test.js @@ -34,7 +34,7 @@ describe('Observe tests', () => { describe('when object is observed', () => { 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', () => { @@ -181,7 +181,7 @@ describe('Observe tests', () => { it('should throw error if removed observer is missing', () => { expect(() => { 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', () => { diff --git a/js/test/position.test.js b/js/test/position.test.js index 6678b0b..21193b5 100644 --- a/js/test/position.test.js +++ b/js/test/position.test.js @@ -38,7 +38,7 @@ describe('Position tests', () => { 'username', 'trackid', 'trackname', - 'distance', + 'meters', 'seconds' ]; const properties = nullableProperties.concat(nonNullableProperties); @@ -57,7 +57,7 @@ describe('Position tests', () => { let username; let trackid; let trackname; - let distance; + let meters; let seconds; let jsonPosition; @@ -77,7 +77,7 @@ describe('Position tests', () => { username = 'test'; trackid = 134; trackname = 'Test name'; - distance = 0; + meters = 0; seconds = 0; jsonPosition = { @@ -95,7 +95,7 @@ describe('Position tests', () => { 'username': username, 'trackid': trackid, 'trackname': trackname, - 'distance': distance, + 'meters': meters, 'seconds': seconds }; }); @@ -117,7 +117,7 @@ describe('Position tests', () => { expect(position.username).toBe(username); expect(position.trackid).toBe(trackid); expect(position.trackname).toBe(trackname); - expect(position.distance).toBe(distance); + expect(position.meters).toBe(meters); expect(position.seconds).toBe(seconds); }); @@ -129,7 +129,7 @@ describe('Position tests', () => { // when delete posCopy[prop]; // 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 posCopy[prop] = null; // 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; let pos = {}; // 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(); }); }); diff --git a/js/test/user.test.js b/js/test/user.test.js index 9d3f40a..804f6d2 100644 --- a/js/test/user.test.js +++ b/js/test/user.test.js @@ -60,6 +60,7 @@ describe('User tests', () => { describe('ajax tests', () => { 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(() => { spyOn(XMLHttpRequest.prototype, 'open').and.callThrough(); @@ -75,11 +76,26 @@ describe('User tests', () => { // then uUser.fetchList() .then((result) => { + expect(XMLHttpRequest.prototype.open).toHaveBeenCalledWith('GET', 'utils/getusers.php', true); expect(result).toEqual(jasmine.arrayContaining([ new uUser(1, 'test') ])); expect(result.length).toBe(3); 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(); + }); }); });