From 60d2298a16b4862b6d1c48f56a546d68958caf67 Mon Sep 17 00:00:00 2001 From: marley Date: Thu, 18 May 2023 21:00:40 -0700 Subject: [PATCH] Improve Body test coverage. --- src/models/Body.ts | 37 +++++++++--------- test/Body.test.ts | 97 +++++++++++++++++++++++++++++++++------------- 2 files changed, 88 insertions(+), 46 deletions(-) diff --git a/src/models/Body.ts b/src/models/Body.ts index c0aa01a..f8e7f04 100755 --- a/src/models/Body.ts +++ b/src/models/Body.ts @@ -10,8 +10,8 @@ export class Body { mappedValue: number; constructor( - journalLine: Scan|SAAScanComplete|valuableBody|null = null, - DSS: boolean = false, + journalLine: Scan | SAAScanComplete | valuableBody | null = null, + DSS: boolean = false, ) { this.DSSDone = DSS; @@ -59,13 +59,11 @@ export class Body { /* ---------------------------------------------------------------------------- simpleName ---- */ simpleName(): string { - let name: string; + let name: string = this.BodyName; - if (typeof this.StarSystem === 'string') { - name = this.BodyName.replace(this.StarSystem, ''); - } else { - name = this.BodyName; - } + if (typeof this.StarSystem === 'string') name = this.BodyName.replace(this.StarSystem, ''); + + if (name === '') name = 'Star'; return name; } @@ -78,7 +76,7 @@ export class Body { if (this.isStar() || this.isAsteroid()) { typeIcon = this.nameIcon(); } else { - const planetClass: string|undefined = this.PlanetClass?.toLowerCase(); + const planetClass: string | undefined = this.PlanetClass?.toLowerCase(); if (planetClass?.includes('metal')) { typeIcon = 'ingot'; @@ -122,8 +120,8 @@ export class Body { /* ----------------------------------------------------------------- #getNumericalBodyType ---- */ - #getNumericalBodyType(): number|undefined { - let code: number|undefined; + #getNumericalBodyType(): number | undefined { + let code: number | undefined; if (this.isStar()) { // Typescript feels so stupid sometimes. @@ -137,8 +135,8 @@ export class Body { /* ----------------------------------------------------------- #getNumericalTerraformState ---- */ - #getNumericalTerraformState(): number|undefined { - let terraformState: number|undefined; + #getNumericalTerraformState(): number | undefined { + let terraformState: number | undefined; if ('TerraformState' in this) { terraformState = @@ -149,7 +147,7 @@ export class Body { /* ------------------------------------------------------------------------- #appraiseStar ---- */ - #appraiseStar(bodyType: number|undefined, mass: number): number { + #appraiseStar(bodyType: number | undefined, mass: number): number { let value: number = 1200; if (bodyType) { @@ -173,9 +171,9 @@ export class Body { /* ----------------------------------------------------------------------- #appraisePlanet ---- */ #appraisePlanet( - bodyType: number|undefined, + bodyType: number | undefined, mass: number, - terraformState: number|undefined, + terraformState: number | undefined, ): number { let value: number = this.#calculatePlanetBaseValue(bodyType); let terraformBonus: number = this.#calculateTerraformBonus(bodyType, terraformState); @@ -197,7 +195,7 @@ export class Body { /* ------------------------------------------------------------- #calculatePlanetBaseValue ---- */ - #calculatePlanetBaseValue(bodyType: number|undefined): number { + #calculatePlanetBaseValue(bodyType: number | undefined): number { let value: number = 300; if (bodyType) { @@ -222,7 +220,10 @@ export class Body { /* -------------------------------------------------------------- #calculateTerraformBonus ---- */ - #calculateTerraformBonus(bodyType: number|undefined, terraformState: number|undefined): number { + #calculateTerraformBonus( + bodyType: number | undefined, + terraformState: number | undefined, + ): number { let bonus: number = 0; if (terraformState && terraformState > 0) { diff --git a/test/Body.test.ts b/test/Body.test.ts index 97c588d..6c4cbc5 100644 --- a/test/Body.test.ts +++ b/test/Body.test.ts @@ -8,91 +8,132 @@ describe('Body', () => { const asteroid = mockObjects.asteroid as Scan; const planet = mockObjects.planet as Scan; - const body = new Body(planet); + const bodyStar = new Body(star); + const bodyAsteroid = new Body(asteroid); + const bodyPlanet = new Body(planet); describe('constructor()', () => { it('should create Body', () => { - expect(body).toMatchObject(planet as any); + expect(bodyPlanet).toMatchObject(planet as any); }); it('should appraise body', () => { - expect(body.mappedValue).toBeDefined(); - expect(typeof body.mappedValue).toBe('number'); + expect(bodyPlanet.mappedValue).toBeDefined(); + expect(typeof bodyPlanet.mappedValue).toBe('number'); }); }); describe('isAsteroid()', () => { it('should return boolean', () => { - expect(typeof body.isAsteroid()).toBe('boolean'); + expect(typeof bodyPlanet.isAsteroid()).toBe('boolean'); }); it('should return true for asteroids', () => { - const asteroidBody = new Body(asteroid); - expect(asteroidBody.isAsteroid()).toBe(true); + expect(bodyAsteroid.isAsteroid()).toBe(true); }); it('should return false for non-asteroids', () => { - const starBody = new Body(star); - expect(starBody.isAsteroid()).toBe(false); + expect(bodyStar.isAsteroid()).toBe(false); - const planetBody = new Body(planet); - expect(planetBody.isAsteroid()).toBe(false); + expect(bodyPlanet.isAsteroid()).toBe(false); }); }); describe('isPlanet()', () => { it('should return boolean', () => { - expect(typeof body.isPlanet()).toBe('boolean'); + expect(typeof bodyPlanet.isPlanet()).toBe('boolean'); }); it('should return true for planets', () => { - const planetBody = new Body(planet); - expect(planetBody.isPlanet()).toBe(true); + expect(bodyPlanet.isPlanet()).toBe(true); }); it('should return false for non-planets', () => { - const starBody = new Body(star); - expect(starBody.isPlanet()).toBe(false); + expect(bodyStar.isPlanet()).toBe(false); - const asteroidBody = new Body(asteroid); - expect(asteroidBody.isPlanet()).toBe(false); + expect(bodyAsteroid.isPlanet()).toBe(false); }); }); describe('isStar()', () => { it('should return boolean', () => { - expect(typeof body.isStar()).toBe('boolean'); + expect(typeof bodyPlanet.isStar()).toBe('boolean'); }); it('should return true for stars', () => { - const starBody = new Body(star); - expect(starBody.isStar()).toBe(true); + expect(bodyStar.isStar()).toBe(true); }); it('should return false for non-stars', () => { - const planetBody = new Body(planet); - expect(planetBody.isStar()).toBe(false); + expect(bodyPlanet.isStar()).toBe(false); - const asteroidBody = new Body(asteroid); - expect(asteroidBody.isStar()).toBe(false); + expect(bodyAsteroid.isStar()).toBe(false); }); }); describe('nameIcon()', () => { it('should return string', () => { - expect(typeof body.nameIcon()).toBe('string'); + expect(typeof bodyPlanet.nameIcon()).toBe('string'); + }); + + const tests = [ + { body: bodyStar, expects: 'star', title: 'star' }, + { body: bodyAsteroid, expects: 'asteroid-4', title: 'asteroid' }, + { body: bodyPlanet, expects: 'jupiter-3', title: 'planet' }, + ]; + + it.each(tests)('should return $expects for $title', ({ body, expects }) => { + expect(body.nameIcon()).toBe(expects); }); }); describe('simpleName()', () => { it('should return string', () => { - expect(typeof body.simpleName()).toBe('string'); + expect(typeof bodyPlanet.simpleName()).toBe('string'); + }); + + it('should return Star for lonely stars', () => { + const bodyCopy = bodyStar; + bodyCopy.BodyName = 'LHS 119'; + expect(bodyCopy.simpleName()).toBe('Star'); }); }); describe('typeIcon()', () => { it('should return string', () => { - expect(typeof body.typeIcon()).toBe('string'); + expect(typeof bodyPlanet.typeIcon()).toBe('string'); + }); + + const tests = [ + { body: bodyStar, expects: 'star', title: 'star' }, + { body: bodyAsteroid, expects: 'asteroid-4', title: 'asteroid' }, + { body: bodyPlanet, expects: 'ingot', title: 'Metal rich body' }, + { body: bodyPlanet, expects: 'ingot', title: 'High metal content body' }, + { body: bodyPlanet, expects: 'snowflake', title: 'Icy body' }, + { body: bodyPlanet, expects: 'earth', title: 'Earthlike body' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Gas giant with water based life' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Gas giant with ammonia based life' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Sudarsky class I gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Sudarsky class II gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Sudarsky class III gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Sudarsky class IV gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Sudarsky class V gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: 'Helium rich gas giant' }, + { body: bodyPlanet, expects: 'jupiter-1', title: ' Helium gas giant' }, + { body: bodyPlanet, expects: 'asteroid-3', title: 'Rocky body' }, + { body: bodyPlanet, expects: 'asteroid-3', title: 'Rocky ice world' }, + { body: bodyPlanet, expects: 'water-drops', title: 'Water world' }, + { body: bodyPlanet, expects: 'water-drops', title: 'Ammonia world' }, + { body: bodyPlanet, expects: 'water-drops', title: 'Water giant' }, + { body: bodyPlanet, expects: 'water-drops', title: 'Water giant with life' }, + ]; + + it.each(tests)('should return $expects for $title', ({ body, expects, title }) => { + const copy = body; + if (title !== 'star' && title !== 'asteroid') copy.PlanetClass = title; + expect(copy.typeIcon()).toBe(expects); }); }); + + // TODO: body appraisal tests });