|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
| 2 | +import { VectorMap } from '../src/vector-map' |
| 3 | + |
| 4 | +// Mock DOM environment for testing |
| 5 | +function mockDOM() { |
| 6 | + // Mock Node if it doesn't exist |
| 7 | + if (typeof globalThis.Node === 'undefined') { |
| 8 | + globalThis.Node = class Node { |
| 9 | + static ELEMENT_NODE = 1 |
| 10 | + static TEXT_NODE = 3 |
| 11 | + } as any |
| 12 | + } |
| 13 | + |
| 14 | + // Create a mock container element |
| 15 | + const container = { |
| 16 | + id: 'test-container', |
| 17 | + style: { width: '800px', height: '600px' }, |
| 18 | + appendChild: () => {}, |
| 19 | + removeChild: () => {}, |
| 20 | + classList: { |
| 21 | + add: () => {}, |
| 22 | + remove: () => {}, |
| 23 | + contains: () => false, |
| 24 | + }, |
| 25 | + setAttribute: () => {}, |
| 26 | + getAttribute: () => null, |
| 27 | + getBoundingClientRect: () => ({ width: 800, height: 600, left: 0, top: 0 }), |
| 28 | + addEventListener: () => {}, |
| 29 | + removeEventListener: () => {}, |
| 30 | + } as any |
| 31 | + |
| 32 | + // Mock document methods |
| 33 | + if (typeof globalThis.document === 'undefined') { |
| 34 | + globalThis.document = { |
| 35 | + createElement: () => container, |
| 36 | + createElementNS: () => container, |
| 37 | + getElementById: () => container, |
| 38 | + querySelector: () => container, |
| 39 | + body: { appendChild: () => {} }, |
| 40 | + readyState: 'complete', |
| 41 | + } as any |
| 42 | + } |
| 43 | + |
| 44 | + // Mock window if it doesn't exist |
| 45 | + if (typeof globalThis.window === 'undefined') { |
| 46 | + globalThis.window = globalThis as any |
| 47 | + } |
| 48 | + |
| 49 | + return container |
| 50 | +} |
| 51 | + |
| 52 | +function cleanupDOM() { |
| 53 | + // Clean up any global mocks if needed |
| 54 | +} |
| 55 | + |
| 56 | +describe('VectorMap', () => { |
| 57 | + beforeEach(async () => { |
| 58 | + mockDOM() |
| 59 | + |
| 60 | + // Mock the Map.maps static property with test map data |
| 61 | + const { Map } = await import('../src/map') |
| 62 | + Map.maps['test-map'] = { |
| 63 | + name: 'test-map', |
| 64 | + width: 800, |
| 65 | + height: 600, |
| 66 | + paths: { |
| 67 | + 'US-CA': { path: 'M 100 100 L 200 100 L 200 200 L 100 200 Z', name: 'California' }, |
| 68 | + 'US-TX': { path: 'M 300 300 L 400 300 L 400 400 L 300 400 Z', name: 'Texas' }, |
| 69 | + }, |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + afterEach(() => { |
| 74 | + cleanupDOM() |
| 75 | + }) |
| 76 | + |
| 77 | + test('should throw error when no selector is provided', () => { |
| 78 | + expect(() => { |
| 79 | + const _ = new VectorMap({} as any) |
| 80 | + }).toThrow('Selector is not given.') |
| 81 | + }) |
| 82 | + |
| 83 | + test('should accept valid options with selector', () => { |
| 84 | + const options = { |
| 85 | + selector: '#test-container', |
| 86 | + map: { name: 'test-map', projection: 'mercator' as const }, |
| 87 | + } |
| 88 | + |
| 89 | + // Just test that the constructor doesn't throw immediately |
| 90 | + expect(() => { |
| 91 | + const _vectorMap = new VectorMap(options) |
| 92 | + // The constructor might throw later during initialization, but that's okay for this test |
| 93 | + }).not.toThrow('Selector is not given.') |
| 94 | + }) |
| 95 | + |
| 96 | + test('should have addMap static method', () => { |
| 97 | + expect(typeof VectorMap.addMap).toBe('function') |
| 98 | + }) |
| 99 | +}) |
0 commit comments