UNPKG

@sports-alliance/sports-lib

Version:

A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc

140 lines (139 loc) 6.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const data_jump_event_1 = require("./data.jump-event"); const data_jump_distance_1 = require("./data.jump-distance"); const data_distance_1 = require("./data.distance"); const data_speed_1 = require("./data.speed"); const data_duration_1 = require("./data.duration"); const data_latitude_degrees_1 = require("./data.latitude-degrees"); const data_longitude_degrees_1 = require("./data.longitude-degrees"); describe('DataJumpEvent', () => { const timestamp = 1234567890; // Manual creation requires Data objects const jumpData = { distance: new data_jump_distance_1.DataJumpDistance(5.5), height: new data_distance_1.DataDistance(1.2), score: new data_jump_event_1.DataJumpScore(85), hang_time: new data_duration_1.DataDuration(0.8), position_lat: new data_latitude_degrees_1.DataLatitudeDegrees(40.7128), position_long: new data_longitude_degrees_1.DataLongitudeDegrees(-74.006), speed: new data_speed_1.DataSpeed(15.2), rotations: new data_jump_event_1.DataRotations(0) }; it('should be created using constructor with separate arguments (Manual)', () => { const jumpEvent = new data_jump_event_1.DataJumpEvent(timestamp, jumpData); expect(jumpEvent.getType()).toBe('Jump Event'); expect(jumpEvent.getValue()).toBe(timestamp); // Expect exact object match expect(jumpEvent.jumpData).toEqual(jumpData); }); it('should be created using constructor with object argument (Manual Object)', () => { const jumpEvent = new data_jump_event_1.DataJumpEvent({ timestamp, jumpData }); expect(jumpEvent.getType()).toBe('Jump Event'); expect(jumpEvent.getValue()).toBe(timestamp); expect(jumpEvent.jumpData).toEqual(jumpData); }); it('should serialize to JSON correctly', () => { const jumpEvent = new data_jump_event_1.DataJumpEvent(timestamp, jumpData); const json = jumpEvent.toJSON(); // toJSON output should be simple values expect(json).toEqual({ 'Jump Event': { timestamp: timestamp, jumpData: { distance: 5.5, height: 1.2, score: 85, hang_time: 0.8, position_lat: 40.7128, position_long: -74.006, speed: 15.2, rotations: 0 } } }); }); it('should hydrate from JSON object correctly (simulating generic importer)', () => { // Generic importer passes primitive values const jsonValue = { timestamp, jumpData: { distance: 5.5, height: 1.2, score: 85, hang_time: 0.8, position_lat: 40.7128, position_long: -74.006, speed: 15.2, rotations: 0 } }; // Constructor should hydrate primitives into Data objects const jumpEvent = new data_jump_event_1.DataJumpEvent(jsonValue); expect(jumpEvent.getType()).toBe('Jump Event'); expect(jumpEvent.getValue()).toBe(timestamp); // Assertions check if properties are converted to Data objects expect(jumpEvent.jumpData.distance).toBeInstanceOf(data_jump_distance_1.DataJumpDistance); expect(jumpEvent.jumpData.distance.getValue()).toBe(5.5); expect(jumpEvent.jumpData.score).toBeInstanceOf(data_jump_event_1.DataJumpScore); expect(jumpEvent.jumpData.score.getValue()).toBe(85); expect(jumpEvent.jumpData.speed).toBeInstanceOf(data_speed_1.DataSpeed); expect(jumpEvent.jumpData.speed.getValue()).toBe(15.2); }); it('should preserve optional zero values during hydration', () => { var _a, _b, _c, _d, _e, _f; const jsonValue = { timestamp, jumpData: { distance: 1.5, height: 0, score: 0, hang_time: 0, position_lat: 0, position_long: 0, speed: 0, rotations: 0 } }; const jumpEvent = new data_jump_event_1.DataJumpEvent(jsonValue); expect((_a = jumpEvent.jumpData.height) === null || _a === void 0 ? void 0 : _a.getValue()).toBe(0); expect((_b = jumpEvent.jumpData.hang_time) === null || _b === void 0 ? void 0 : _b.getValue()).toBe(0); expect((_c = jumpEvent.jumpData.position_lat) === null || _c === void 0 ? void 0 : _c.getValue()).toBe(0); expect((_d = jumpEvent.jumpData.position_long) === null || _d === void 0 ? void 0 : _d.getValue()).toBe(0); expect((_e = jumpEvent.jumpData.speed) === null || _e === void 0 ? void 0 : _e.getValue()).toBe(0); expect((_f = jumpEvent.jumpData.rotations) === null || _f === void 0 ? void 0 : _f.getValue()).toBe(0); }); it('should omit missing optional fields from hydration and JSON output', () => { const jsonValue = { timestamp, jumpData: { distance: 2.5, score: 42 } }; const jumpEvent = new data_jump_event_1.DataJumpEvent(jsonValue); const jumpData = jumpEvent.jumpData; expect(jumpData.height).toBeUndefined(); expect(Object.prototype.hasOwnProperty.call(jumpData, 'height')).toBeFalsy(); expect(jumpData.hang_time).toBeUndefined(); expect(Object.prototype.hasOwnProperty.call(jumpData, 'hang_time')).toBeFalsy(); const serializedJumpData = jumpEvent.toJSON()['Jump Event'].jumpData; expect(serializedJumpData).toEqual({ distance: 2.5, score: 42 }); expect(Object.prototype.hasOwnProperty.call(serializedJumpData, 'height')).toBeFalsy(); expect(Object.prototype.hasOwnProperty.call(serializedJumpData, 'hang_time')).toBeFalsy(); }); it('should format jump-event score with one decimal', () => { const jumpEvent = new data_jump_event_1.DataJumpEvent(timestamp, jumpData); expect(jumpEvent.jumpData.score.getDisplayValue()).toBe('85.0'); }); it('should keep DataScore alias compatible with DataJumpScore', () => { const scoreFromAlias = new data_jump_event_1.DataScore(6.28); expect(scoreFromAlias).toBeInstanceOf(data_jump_event_1.DataJumpScore); expect(scoreFromAlias.getDisplayValue()).toBe('6.3'); expect(data_jump_event_1.DataScore).toBe(data_jump_event_1.DataJumpScore); expect(data_jump_event_1.DataJumpScore.type).toBe('Jump Score'); }); });