diff --git a/server/src/classes/flight.js b/server/src/classes/flight.js index da12cc161..742e108c8 100644 --- a/server/src/classes/flight.js +++ b/server/src/classes/flight.js @@ -1,7 +1,7 @@ import uuid from "uuid"; import randomWords from "random-words"; export default class Flight { - constructor(params) { + constructor(params = {}) { this.class = "Flight"; this.id = params.id || uuid.v4(); this.name = params.name || randomWords(3).join("-"); diff --git a/server/tests/classes/flight.test.js b/server/tests/classes/flight.test.js new file mode 100644 index 000000000..be73b1994 --- /dev/null +++ b/server/tests/classes/flight.test.js @@ -0,0 +1,53 @@ +import Flight from '../../src/classes/flight'; +import Simulator from '../../src/classes/simulator'; + +describe('Flight', () => { + + test('should throw if called without the \'new\' operator', () => { + expect(() => { const c = Flight(); }).toThrow(/Cannot call a class as a function/); + }); + + describe('constructor', () => { + test('should set default parameters', () => { + const f = new Flight(); + expect(f.class).toBe('Flight'); + expect(f.id).toEqual(expect.stringMatching(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/)); + expect(f.name).toBeDefined(); + expect(f.date).toBeDefined(); + expect(f.running).toBe(false); + expect(f.simulators).toEqual([]); + }); + }); + + describe('addSimulator', () => { + test('should add a simulator', () => { + const f = new Flight(); + const s = new Simulator(); + f.addSimulator(s); + expect(f.simulators[0]).toBe(s.id); + }); + }); + + describe('removeSimulator', () => { + test('should remove a simulator', () => { + const f = new Flight(); + const s = new Simulator(); + f.addSimulator(s); + f.removeSimulator(s.id); + expect(f.simulators.length).toBe(0); + }); + }) + + describe('stopFlight, pause, and resume', () => { + test('should stop, pause, or resume a flight', () => { + const f = new Flight({ running: true }); + expect(f.running).toBe(true); + f.pause(); + expect(f.running).toBe(false); + f.resume(); + expect(f.running).toBe(true); + f.stopFlight(); + expect(f.running).toBe(false); + }); + }); +}); \ No newline at end of file