Skip to content

Commit

Permalink
test: 🩹 fix mock date object being modified
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jan 15, 2024
1 parent b2eaaab commit 0adde7a
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 63 deletions.
4 changes: 0 additions & 4 deletions src/coordinate/Geodetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,10 @@ export class Geodetic {

constructor(latitude: Radians, longitude: Radians, altitude: Kilometers) {
if (Math.abs(latitude) > Math.PI / 2) {
// eslint-disable-next-line no-console
console.warn(longitude * RAD2DEG);
throw new RangeError('Latitude must be between -90° and 90° in Radians.');
}

if (Math.abs(longitude) > Math.PI) {
// eslint-disable-next-line no-console
console.warn(longitude * RAD2DEG);
throw new RangeError('Longitude must be between -180° and 180° in Radians.');
}

Expand Down
4 changes: 2 additions & 2 deletions test/body/Celestial.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Degrees, Radians } from './../../src/types/types';
import { Celestial } from './../../src/body/Celestial';
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';

describe('Celestial', () => {
/*
Expand All @@ -13,7 +13,7 @@ describe('Celestial', () => {
const ra = 1.3963 as Radians; // right ascension of a celestial object
const dec = 0.7854 as Radians; // declination of a celestial object

const azEl = Celestial.azEl(exampleDate, lat, lon, ra, dec);
const azEl = Celestial.azEl(mockExampleDate, lat, lon, ra, dec);

expect(azEl).toMatchSnapshot();
});
Expand Down
14 changes: 7 additions & 7 deletions test/body/Earth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Earth, EpochUTC, Vector3D } from '../../src/main';
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';

describe('Earth', () => {
// can calculate mean motion from semimajor axis
Expand All @@ -12,7 +12,7 @@ describe('Earth', () => {

// can calculate precession angles from epoch
it('should calculate precession angles when given an epoch', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const precessionAngles = Earth.precession(epoch);

expect(precessionAngles.zeta).toMatchSnapshot();
Expand All @@ -22,7 +22,7 @@ describe('Earth', () => {

// can calculate nutation angles from epoch
it('should calculate nutation angles when given an epoch', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const nutationAngles = Earth.nutation(epoch);

expect(nutationAngles.dPsi).toMatchSnapshot();
Expand Down Expand Up @@ -75,15 +75,15 @@ describe('Earth', () => {

// can handle epoch before 1900 when calculating precession angles
it('should handle epoch before 1900 when calculating precession angles', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const precessionAngles = Earth.precession(epoch);

expect(precessionAngles.zeta).toMatchSnapshot();
});

// can handle epoch after 2150 when calculating precession angles
it('should handle epoch after 2150 when calculating precession angles', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const precessionAngles = Earth.precession(epoch);

expect(precessionAngles.zeta).not.toBeNaN();
Expand All @@ -92,7 +92,7 @@ describe('Earth', () => {

// can handle epoch before 1900 when calculating nutation angles
it('should handle epoch before 1900 when calculating nutation angles', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const nutationAngles = Earth.nutation(epoch);

expect(nutationAngles.dPsi).not.toBeNaN();
Expand All @@ -101,7 +101,7 @@ describe('Earth', () => {

// can handle epoch after 2150 when calculating nutation angles
it('should handle epoch after 2150 when calculating nutation angles', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const nutationAngles = Earth.nutation(epoch);

expect(nutationAngles.dPsi).not.toBeNaN();
Expand Down
18 changes: 12 additions & 6 deletions test/body/Moon.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Degrees, EpochUTC, Moon, Vector3D } from '../../src/main';
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';

describe('Moon', () => {
let exampleDate: Date;

beforeEach(() => {
exampleDate = new Date(mockExampleDate.getTime());
});

// The static property 'mu' should be accessible and have a value of 4902.799.
it('should have a static property "mu" with value 4902.799', () => {
expect(Moon.mu).toBe(4902.799);
Expand Down Expand Up @@ -99,7 +105,7 @@ describe('Moon', () => {
* 'Degrees' parameters and return an object with 'az', 'el', 'rng', and
* 'parallacticAngle' properties.
*/
it("should return an object with 'az', 'el', 'rng', and 'parallacticAngle' properties", () => {
it('should return an object with \'az\', \'el\', \'rng\', and \'parallacticAngle\' properties', () => {
const date = new Date(1635724800000); // November 1, 2021
const lat = 37.7749 as Degrees; // San Francisco latitude
const lon = -122.4194 as Degrees; // San Francisco longitude
Expand All @@ -113,7 +119,7 @@ describe('Moon', () => {
* 'Degrees', 'Degrees', and 'boolean' parameters and return an object with
* 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', and 'highest' properties.
*/
it("should return an object with 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', and 'highest' properties", () => {
it('should return with \'rise\', \'set\', \'ye\', \'alwaysUp\', \'alwaysDown\', \'highest\' properties', () => {
const date = exampleDate;
const lat = 37.7749 as Degrees; // San Francisco latitude
const lon = -122.4194 as Degrees; // San Francisco longitude
Expand All @@ -128,7 +134,7 @@ describe('Moon', () => {
* 'Degrees' parameters and return an object with 'az', 'el', 'rng', and
* 'parallacticAngle' properties.
*/
it("should return an object with 'az', 'el', 'rng', and 'parallacticAngle' properties", () => {
it('should return an object with \'az\', \'el\', \'rng\', and \'parallacticAngle\' properties', () => {
const date = new Date(exampleDate); // November 1, 2021
const lat = 37.7749 as Degrees; // San Francisco latitude
const lon = -122.4194 as Degrees; // San Francisco longitude
Expand All @@ -142,7 +148,7 @@ describe('Moon', () => {
* 'Degrees', 'Degrees', and 'boolean' parameters and return an object with
* 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', and 'highest' properties.
*/
it("should return an object with 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', and 'highest' properties", () => {
it('should return with \'rise\', \'set\', \'ye\', \'alwaysUp\', \'alwaysDown\', and \'highest\' properties', () => {
const date = exampleDate;
const lat = 37.7749 as Degrees; // San Francisco latitude
const lon = -122.4194 as Degrees; // San Francisco longitude
Expand All @@ -156,7 +162,7 @@ describe('Moon', () => {
* The private method 'moonCoords' should be callable with a 'number'
* parameter and return a 'RaDec' object.
*/
it("should call the private method 'moonCoords' with a number parameter and return a RaDec object", () => {
it('should call the private method \'moonCoords\' with a number parameter and return a RaDec object', () => {
const d = 123456789;
const result = Moon.moonCoords(d);

Expand Down
36 changes: 24 additions & 12 deletions test/body/Sun.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Degrees, EpochUTC, Meters, Sun, Vector3D } from '../../src/main';
import { exampleDate } from '../lib/mockData';
import { Degrees, EpochUTC, Kilometers, Meters, Sun, Vector3D } from '../../src/main';
import { mockExampleDate } from '../lib/mockData';

describe('Sun', () => {
let exampleDate: Date;

beforeEach(() => {
exampleDate = new Date(mockExampleDate.getTime());
});

/*
* The 'azEl' method should return the azimuth and elevation of the sun given
* a date, latitude, and longitude.
Expand Down Expand Up @@ -29,8 +35,8 @@ describe('Sun', () => {
* the observer's position and the sun's position.
*/
it('should return the angular diameter of the sun given the observers position and the suns position', () => {
const obsPos = new Vector3D(0, 0, 0);
const sunPos = new Vector3D(1, 1, 1);
const obsPos = new Vector3D<Kilometers>(0 as Kilometers, 0 as Kilometers, 0 as Kilometers);
const sunPos = new Vector3D<Kilometers>(1 as Kilometers, 1 as Kilometers, 1 as Kilometers);
const result = Sun.diameter(obsPos, sunPos);

expect(result).toMatchSnapshot();
Expand All @@ -41,8 +47,8 @@ describe('Sun', () => {
* if a satellite is in the shadow of the earth.
*/
it('should return the angles necessary to determine if a satellite is in the shadow of the earth', () => {
const satPos = new Vector3D(0, 0, 0);
const sunPos = new Vector3D(1, 1, 1);
const satPos = new Vector3D<Kilometers>(0 as Kilometers, 0 as Kilometers, 0 as Kilometers);
const sunPos = new Vector3D<Kilometers>(1 as Kilometers, 1 as Kilometers, 1 as Kilometers);
const result = Sun.eclipseAngles(satPos, sunPos);

expect(result).toMatchSnapshot();
Expand Down Expand Up @@ -83,8 +89,8 @@ describe('Sun', () => {
* the same as the sun's position.
*/
it('should handle cases where the observers position is the same as the suns position', () => {
const obsPos = new Vector3D(1, 1, 1);
const sunPos = new Vector3D(1, 1, 1);
const obsPos = new Vector3D<Kilometers>(1 as Kilometers, 1 as Kilometers, 1 as Kilometers);
const sunPos = new Vector3D<Kilometers>(1 as Kilometers, 1 as Kilometers, 1 as Kilometers);
const result = Sun.diameter(obsPos, sunPos);

expect(result).toMatchSnapshot();
Expand All @@ -95,8 +101,8 @@ describe('Sun', () => {
* directly above or below the earth.
*/
it('should handle cases where the satellite is directly above or below the earth', () => {
const satPos = new Vector3D(0, 0, 0);
const sunPos = new Vector3D(0, 0, 1);
const satPos = new Vector3D<Kilometers>(0 as Kilometers, 0 as Kilometers, 0 as Kilometers);
const sunPos = new Vector3D<Kilometers>(0 as Kilometers, 0 as Kilometers, 1 as Kilometers);
const result = Sun.eclipseAngles(satPos, sunPos);

expect(result).toMatchSnapshot();
Expand Down Expand Up @@ -171,7 +177,10 @@ describe('Sun', () => {
});

it('should return the same shadow result given the same time and position', () => {
const result = Sun.shadow(EpochUTC.fromDateTime(exampleDate), new Vector3D(8000, 0, 0));
const result = Sun.shadow(
EpochUTC.fromDateTime(exampleDate),
new Vector3D<Kilometers>(8000 as Kilometers, 0 as Kilometers, 0 as Kilometers),
);

expect(result).toMatchSnapshot();
});
Expand All @@ -192,7 +201,10 @@ describe('Sun', () => {

// lightingRatio
it('should return the same lighting ratio result given the same positions', () => {
const result = Sun.lightingRatio(new Vector3D(8000, 0, 0), new Vector3D(1000000, 0, 0));
const result = Sun.lightingRatio(
new Vector3D<Kilometers>(8000 as Kilometers, 0 as Kilometers, 0 as Kilometers),
new Vector3D<Kilometers>(1000000 as Kilometers, 0 as Kilometers, 0 as Kilometers),
);

expect(result).toMatchSnapshot();
});
Expand Down
22 changes: 22 additions & 0 deletions test/body/__snapshots__/Moon.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,25 @@ Object {
"ye": null,
}
`;

exports[`Moon should return with 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', 'highest' properties 1`] = `
Object {
"alwaysDown": false,
"alwaysUp": false,
"highest": 2024-01-12T21:42:31.618Z,
"rise": 2024-01-12T16:43:59.167Z,
"set": 2024-01-13T02:41:04.069Z,
"ye": null,
}
`;

exports[`Moon should return with 'rise', 'set', 'ye', 'alwaysUp', 'alwaysDown', and 'highest' properties 1`] = `
Object {
"alwaysDown": false,
"alwaysUp": false,
"highest": 2024-01-12T21:42:31.618Z,
"rise": 2024-01-12T16:43:59.167Z,
"set": 2024-01-13T02:41:04.069Z,
"ye": null,
}
`;
8 changes: 4 additions & 4 deletions test/coordinate/ITRF.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';
import { Earth, EpochUTC, ITRF, Kilometers, Vector3D } from './../../src/main';
// Generated by CodiumAI

Expand All @@ -7,7 +7,7 @@ describe('ITRF', () => {
let itrf: ITRF;

beforeEach(() => {
epochUtc = EpochUTC.fromDateTime(exampleDate);
epochUtc = EpochUTC.fromDateTime(mockExampleDate);
itrf = new ITRF(
epochUtc,
new Vector3D<Kilometers>(1000 as Kilometers, 2000 as Kilometers, 3000 as Kilometers),
Expand Down Expand Up @@ -191,8 +191,8 @@ describe('ITRF', () => {
it('should handle position at different epochs', () => {
const position = new Vector3D<Kilometers>(1000 as Kilometers, 2000 as Kilometers, 3000 as Kilometers);
const velocity = new Vector3D<Kilometers>(10 as Kilometers, 20 as Kilometers, 30 as Kilometers);
const epoch1 = EpochUTC.fromDateTime(exampleDate);
const epoch2 = EpochUTC.fromDateTime(new Date(exampleDate.getTime() + 1000));
const epoch1 = EpochUTC.fromDateTime(mockExampleDate);
const epoch2 = EpochUTC.fromDateTime(new Date(mockExampleDate.getTime() + 1000));
const itrf1 = new ITRF(epoch1, position, velocity);
const itrf2 = new ITRF(epoch2, position, velocity);

Expand Down
4 changes: 2 additions & 2 deletions test/coordinate/J2000.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Generated by CodiumAI

import { EpochUTC, ClassicalElements, Kilometers, Radians, J2000, Vector3D } from '../../src/main';
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';

describe('J2000', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);

// can be created from classical elements
it('should create a J2000 coordinate from classical elements', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/coordinate/TEME.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';
import { ClassicalElements, EpochUTC, J2000, Kilometers, Radians, TEME, Vector3D } from './../../src/main';
describe('TEME', () => {
let stateVector: TEME;
Expand Down Expand Up @@ -27,7 +27,7 @@ describe('TEME', () => {

// fromClassicalElements
it('should create a TEME coordinate from classical orbital elements', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const elements = new ClassicalElements({
epoch,
semimajorAxis: 6943.547853722985 as Kilometers,
Expand Down
6 changes: 3 additions & 3 deletions test/coordinate/Tle.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';
import { ClassicalElements, EpochUTC, Kilometers, Radians, Tle, TleLine1, TleLine2 } from '../../src/main';

describe('Tle', () => {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Tle', () => {

// propagate
it('should propagate the TLE', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const propagated = tle.propagate(epoch);

expect(propagated).toMatchSnapshot();
Expand All @@ -67,7 +67,7 @@ describe('Tle', () => {

// fromClassicalElements
it('should create a TLE from classical orbital elements', () => {
const epoch = EpochUTC.fromDateTime(exampleDate);
const epoch = EpochUTC.fromDateTime(mockExampleDate);
const elements = new ClassicalElements({
epoch,
semimajorAxis: 6943.547853722985 as Kilometers,
Expand Down
2 changes: 1 addition & 1 deletion test/lib/mockData.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const exampleDate = new Date(1705109326817);
export const mockExampleDate = new Date(1705109326817);
4 changes: 2 additions & 2 deletions test/objects/GroundObject.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exampleDate } from '../lib/mockData';
import { mockExampleDate } from '../lib/mockData';
import { Degrees, Geodetic, GroundObject, Kilometers, Radians } from '../../src/main';

describe('GroundObject', () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('GroundObject', () => {
});

it('should calculate eci', () => {
expect(groundObject.eci(exampleDate)).toMatchSnapshot();
expect(groundObject.eci(mockExampleDate)).toMatchSnapshot();
});

it('should calculate llaRad', () => {
Expand Down
Loading

0 comments on commit 0adde7a

Please sign in to comment.