|
| 1 | +import { expect, describe, it } from 'vitest' |
| 2 | +import { getLongitude, getLatitude, getCoordinates } from './commonUtils' |
| 3 | + |
| 4 | +describe('getLongitude', () => { |
| 5 | + const mapCoordinates = { lonW: -10, lonT: 20 }; |
| 6 | + const graphWidth = 1000; |
| 7 | + |
| 8 | + it('should calculate longitude at the left edge (x=0)', () => { |
| 9 | + expect(getLongitude(0, mapCoordinates, graphWidth, 2)).toBe(-10); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should calculate longitude at the right edge (x=graphWidth)', () => { |
| 13 | + expect(getLongitude(1000, mapCoordinates, graphWidth, 2)).toBe(10); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should calculate longitude at the center (x=graphWidth/2)', () => { |
| 17 | + expect(getLongitude(500, mapCoordinates, graphWidth, 2)).toBe(0); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should respect decimal precision', () => { |
| 21 | + // 333/1000 * 20 = 6.66, -10 + 6.66 = -3.34 |
| 22 | + expect(getLongitude(333, mapCoordinates, graphWidth, 4)).toBe(-3.34); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle different map coordinate ranges', () => { |
| 26 | + const wideMap = { lonW: -180, lonT: 360 }; |
| 27 | + expect(getLongitude(500, wideMap, graphWidth, 2)).toBe(0); |
| 28 | + expect(getLongitude(0, wideMap, graphWidth, 2)).toBe(-180); |
| 29 | + expect(getLongitude(1000, wideMap, graphWidth, 2)).toBe(180); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +describe('getLatitude', () => { |
| 34 | + const mapCoordinates = { latN: 60, latT: 40 }; |
| 35 | + const graphHeight = 800; |
| 36 | + |
| 37 | + it('should calculate latitude at the top edge (y=0)', () => { |
| 38 | + expect(getLatitude(0, mapCoordinates, graphHeight, 2)).toBe(60); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should calculate latitude at the bottom edge (y=graphHeight)', () => { |
| 42 | + expect(getLatitude(800, mapCoordinates, graphHeight, 2)).toBe(20); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should calculate latitude at the center (y=graphHeight/2)', () => { |
| 46 | + expect(getLatitude(400, mapCoordinates, graphHeight, 2)).toBe(40); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should respect decimal precision', () => { |
| 50 | + // 60 - (333/800 * 40) = 60 - 16.65 = 43.35 |
| 51 | + expect(getLatitude(333, mapCoordinates, graphHeight, 4)).toBe(43.35); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should handle equator-centered maps', () => { |
| 55 | + const equatorMap = { latN: 45, latT: 90 }; |
| 56 | + expect(getLatitude(400, equatorMap, graphHeight, 2)).toBe(0); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe('getCoordinates', () => { |
| 61 | + const mapCoordinates = { lonW: -10, lonT: 20, latN: 60, latT: 40 }; |
| 62 | + const graphWidth = 1000; |
| 63 | + const graphHeight = 800; |
| 64 | + |
| 65 | + it('should return [longitude, latitude] tuple', () => { |
| 66 | + const result = getCoordinates(500, 400, mapCoordinates, graphWidth, graphHeight, 2); |
| 67 | + expect(result).toEqual([0, 40]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should calculate coordinates at top-left corner', () => { |
| 71 | + const result = getCoordinates(0, 0, mapCoordinates, graphWidth, graphHeight, 2); |
| 72 | + expect(result).toEqual([-10, 60]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should calculate coordinates at bottom-right corner', () => { |
| 76 | + const result = getCoordinates(1000, 800, mapCoordinates, graphWidth, graphHeight, 2); |
| 77 | + expect(result).toEqual([10, 20]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should respect decimal precision for both coordinates', () => { |
| 81 | + const result = getCoordinates(333, 333, mapCoordinates, graphWidth, graphHeight, 4); |
| 82 | + expect(result[0]).toBe(-3.34); // longitude |
| 83 | + expect(result[1]).toBe(43.35); // latitude |
| 84 | + }); |
| 85 | + |
| 86 | + it('should use default precision of 2 decimals', () => { |
| 87 | + const result = getCoordinates(333, 333, mapCoordinates, graphWidth, graphHeight); |
| 88 | + expect(result[0]).toBe(-3.34); |
| 89 | + expect(result[1]).toBe(43.35); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should handle global map coordinates', () => { |
| 93 | + const globalMap = { lonW: -180, lonT: 360, latN: 90, latT: 180 }; |
| 94 | + const result = getCoordinates(500, 400, globalMap, graphWidth, graphHeight, 2); |
| 95 | + expect(result).toEqual([0, 0]); // center of the world |
| 96 | + }); |
| 97 | +}); |
0 commit comments