|
1 | | -import { describe, it, expect } from 'vitest'; |
2 | | -import { convertDDToDMS } from './utils'; |
| 1 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 2 | +import { |
| 3 | + convertDDToDMS, |
| 4 | + mapStyleSchema, |
| 5 | + getMapStyleUrl, |
| 6 | + getInitialMapStyle, |
| 7 | + getCustomStyle, |
| 8 | +} from './utils'; |
| 9 | +import { MAP_STYLE_STORAGE_KEY, CUSTOM_STYLE_STORAGE_KEY } from './constants'; |
3 | 10 |
|
4 | 11 | describe('convertDDToDMS', () => { |
5 | 12 | it('should convert zero degrees correctly', () => { |
@@ -93,10 +100,142 @@ describe('convertDDToDMS', () => { |
93 | 100 | }); |
94 | 101 |
|
95 | 102 | it('should handle typical geographic coordinates', () => { |
96 | | - // Testing some realistic coordinate examples |
97 | | - expect(convertDDToDMS(52.5167)).toBe('52° 31\' 0"'); // Example: Berlin |
98 | | - expect(convertDDToDMS(13.3777)).toBe('13° 22\' 40"'); // Example: Berlin longitude |
99 | | - expect(convertDDToDMS(-33.8688)).toBe('-33° 52\' 8"'); // Example: Sydney |
100 | | - expect(convertDDToDMS(151.2093)).toBe('151° 12\' 33"'); // Example: Sydney longitude |
| 103 | + expect(convertDDToDMS(52.5167)).toBe('52° 31\' 0"'); |
| 104 | + expect(convertDDToDMS(13.3777)).toBe('13° 22\' 40"'); |
| 105 | + expect(convertDDToDMS(-33.8688)).toBe('-33° 52\' 8"'); |
| 106 | + expect(convertDDToDMS(151.2093)).toBe('151° 12\' 33"'); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('mapStyleSchema', () => { |
| 111 | + it('should validate built-in style ids', () => { |
| 112 | + expect(mapStyleSchema.safeParse('shortbread').success).toBe(true); |
| 113 | + expect(mapStyleSchema.safeParse('carto').success).toBe(true); |
| 114 | + expect(mapStyleSchema.safeParse('alidade-smooth').success).toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should validate custom style', () => { |
| 118 | + expect(mapStyleSchema.safeParse('custom').success).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should reject invalid style ids', () => { |
| 122 | + expect(mapStyleSchema.safeParse('invalid').success).toBe(false); |
| 123 | + expect(mapStyleSchema.safeParse('').success).toBe(false); |
| 124 | + expect(mapStyleSchema.safeParse(null).success).toBe(false); |
| 125 | + expect(mapStyleSchema.safeParse(undefined).success).toBe(false); |
| 126 | + expect(mapStyleSchema.safeParse(123).success).toBe(false); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe('getMapStyleUrl', () => { |
| 131 | + it('should return correct URL for shortbread style', () => { |
| 132 | + expect(getMapStyleUrl('shortbread')).toBe( |
| 133 | + '/styles/versatiles-colorful.json' |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should return correct URL for carto style', () => { |
| 138 | + expect(getMapStyleUrl('carto')).toBe('/styles/carto.json'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should return correct URL for alidade-smooth style', () => { |
| 142 | + expect(getMapStyleUrl('alidade-smooth')).toBe( |
| 143 | + 'https://tiles.stadiamaps.com/styles/alidade_smooth.json' |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should return default style URL for custom style', () => { |
| 148 | + expect(getMapStyleUrl('custom')).toBe('/styles/versatiles-colorful.json'); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe('getInitialMapStyle', () => { |
| 153 | + beforeEach(() => { |
| 154 | + localStorage.clear(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should return urlValue if it is a valid built-in style', () => { |
| 158 | + expect(getInitialMapStyle('shortbread')).toBe('shortbread'); |
| 159 | + expect(getInitialMapStyle('carto')).toBe('carto'); |
| 160 | + expect(getInitialMapStyle('alidade-smooth')).toBe('alidade-smooth'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should return custom if urlValue is custom and custom style exists in localStorage', () => { |
| 164 | + localStorage.setItem(CUSTOM_STYLE_STORAGE_KEY, '{"version":8}'); |
| 165 | + expect(getInitialMapStyle('custom')).toBe('custom'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should return default if urlValue is custom but no custom style in localStorage', () => { |
| 169 | + expect(getInitialMapStyle('custom')).toBe('shortbread'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should fallback to localStorage if urlValue is undefined', () => { |
| 173 | + localStorage.setItem(MAP_STYLE_STORAGE_KEY, 'carto'); |
| 174 | + expect(getInitialMapStyle()).toBe('carto'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should return custom from localStorage if custom style exists', () => { |
| 178 | + localStorage.setItem(MAP_STYLE_STORAGE_KEY, 'custom'); |
| 179 | + localStorage.setItem(CUSTOM_STYLE_STORAGE_KEY, '{"version":8}'); |
| 180 | + expect(getInitialMapStyle()).toBe('custom'); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should return default if localStorage has custom but no custom style data', () => { |
| 184 | + localStorage.setItem(MAP_STYLE_STORAGE_KEY, 'custom'); |
| 185 | + expect(getInitialMapStyle()).toBe('shortbread'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should return default if localStorage has invalid value', () => { |
| 189 | + localStorage.setItem(MAP_STYLE_STORAGE_KEY, 'invalid-style'); |
| 190 | + expect(getInitialMapStyle()).toBe('shortbread'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should return default if no urlValue and no localStorage', () => { |
| 194 | + expect(getInitialMapStyle()).toBe('shortbread'); |
| 195 | + }); |
| 196 | + |
| 197 | + it('should return default for invalid urlValue', () => { |
| 198 | + expect(getInitialMapStyle('invalid')).toBe('shortbread'); |
| 199 | + expect(getInitialMapStyle('')).toBe('shortbread'); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +describe('getCustomStyle', () => { |
| 204 | + beforeEach(() => { |
| 205 | + localStorage.clear(); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should return null if no custom style in localStorage', () => { |
| 209 | + expect(getCustomStyle()).toBeNull(); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should return parsed style if valid JSON in localStorage', () => { |
| 213 | + const styleData = { version: 8, name: 'Test', sources: {}, layers: [] }; |
| 214 | + localStorage.setItem(CUSTOM_STYLE_STORAGE_KEY, JSON.stringify(styleData)); |
| 215 | + expect(getCustomStyle()).toEqual(styleData); |
| 216 | + }); |
| 217 | + |
| 218 | + it('should return null if invalid JSON in localStorage', () => { |
| 219 | + localStorage.setItem(CUSTOM_STYLE_STORAGE_KEY, 'invalid-json{'); |
| 220 | + expect(getCustomStyle()).toBeNull(); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should return complex style object correctly', () => { |
| 224 | + const styleData = { |
| 225 | + version: 8, |
| 226 | + name: 'Complex Style', |
| 227 | + sources: { |
| 228 | + osm: { type: 'vector', url: 'https://example.com' }, |
| 229 | + }, |
| 230 | + layers: [ |
| 231 | + { |
| 232 | + id: 'background', |
| 233 | + type: 'background', |
| 234 | + paint: { 'background-color': '#fff' }, |
| 235 | + }, |
| 236 | + ], |
| 237 | + }; |
| 238 | + localStorage.setItem(CUSTOM_STYLE_STORAGE_KEY, JSON.stringify(styleData)); |
| 239 | + expect(getCustomStyle()).toEqual(styleData); |
101 | 240 | }); |
102 | 241 | }); |
0 commit comments