No test coverage existed before this. Adds vitest + jsdom (2 new devDependencies) and a suite that runs entirely without a browser: real Leaflet objects work fine under jsdom for everything this library needs to verify (option/attribute wiring, event forwarding), given a ResizeObserver stub in test/setup.ts (jsdom's only real gap here). - test/core/with-props.test.ts: the WithProps mixin itself, against a fake Leaflet-like class -- attribute<->setter dispatch, get/attribute/default fallback order, event-driven attribute sync-back, positional/recreate/ attach modes, and the generic fire()-patch event forwarding. Child registration (popup/tooltip/layer binding) uses real Popup/Tooltip/Marker instances since #onChildRegister discriminates by instanceof. - test/core/props.test.ts: the codec functions in isolation. - test/components/*.test.ts: grouped smoke tests across all components. - test/integration.test.ts: full tree wiring (map + tile-layer + feature-group + marker + popup). Caught and fixed one real bug along the way: urlProp's "live url getter" from the last refactor was dead code -- neither ImageOverlay nor VideoOverlay actually expose getUrl(). Removed it and the now-pointless getUrl?() from the Sourced interface in shared-props.ts. tsconfig.test.json keeps test/ out of the tsc build (dist/ stays test-free) while still typechecking it; oxlint.config.ts now covers test/ too, with max-classes-per-file relaxed there since testing a class factory means many small one-off element classes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { bool, choice, disabled, json, kebab, num, positional, str } from '../../src/core/props.ts';
|
|
|
|
const noopGet = () => 1;
|
|
|
|
describe('kebab', () => {
|
|
it('converts camelCase to kebab-case', () => {
|
|
expect(kebab('fillColor')).toBe('fill-color');
|
|
expect(kebab('zIndex')).toBe('z-index');
|
|
expect(kebab('zIndexOffset')).toBe('z-index-offset');
|
|
});
|
|
|
|
it('leaves single lowercase words unchanged', () => {
|
|
expect(kebab('opacity')).toBe('opacity');
|
|
});
|
|
});
|
|
|
|
describe('num', () => {
|
|
it('defaults to 0 and decodes/encodes via Number/String', () => {
|
|
const def = num();
|
|
expect(def.default).toBe(0);
|
|
expect(def.decode('42')).toBe(42);
|
|
expect(def.encode(42)).toBe('42');
|
|
});
|
|
|
|
it('accepts a custom default and passes through extra opts', () => {
|
|
const def = num(10, { get: noopGet });
|
|
expect(def.default).toBe(10);
|
|
expect(def.get).toBe(noopGet);
|
|
});
|
|
});
|
|
|
|
describe('str', () => {
|
|
it('defaults to empty string and decode/encode are identity', () => {
|
|
const def = str();
|
|
expect(def.default).toBe('');
|
|
expect(def.decode('hello')).toBe('hello');
|
|
expect(def.encode('hello')).toBe('hello');
|
|
});
|
|
});
|
|
|
|
describe('choice', () => {
|
|
it('decode/encode are identity, typed as the literal union', () => {
|
|
const def = choice<'a' | 'b'>('a');
|
|
expect(def.default).toBe('a');
|
|
expect(def.decode('b')).toBe('b');
|
|
expect(def.encode('b')).toBe('b');
|
|
});
|
|
});
|
|
|
|
describe('bool', () => {
|
|
it('decodes any value other than the literal string "false" as true', () => {
|
|
const def = bool();
|
|
expect(def.decode('')).toBe(true);
|
|
expect(def.decode('true')).toBe(true);
|
|
expect(def.decode('anything')).toBe(true);
|
|
expect(def.decode('false')).toBe(false);
|
|
});
|
|
|
|
it('encode returns null when the value matches the default (removes the attribute)', () => {
|
|
const defFalse = bool(false);
|
|
expect(defFalse.encode(false)).toBeNull();
|
|
expect(defFalse.encode(true)).toBe('');
|
|
|
|
const defTrue = bool(true);
|
|
expect(defTrue.encode(true)).toBeNull();
|
|
expect(defTrue.encode(false)).toBe('false');
|
|
});
|
|
});
|
|
|
|
describe('disabled', () => {
|
|
it('names its attribute disable-<kebab-name>', () => {
|
|
const def = disabled();
|
|
expect(def.attribute).toBeTypeOf('function');
|
|
expect((def.attribute as (name: string) => string)('dragging')).toBe('disable-dragging');
|
|
});
|
|
|
|
it('defaults to true (enabled); presence of the attribute disables unless its value is "false"', () => {
|
|
const def = disabled();
|
|
expect(def.default).toBe(true);
|
|
// Bare `disable-foo` (raw === '') means "disable this" -> false.
|
|
expect(def.decode('')).toBe(false);
|
|
expect(def.decode('anything')).toBe(false);
|
|
// `disable-foo="false"` is a double-negative escape hatch -> stays enabled.
|
|
expect(def.decode('false')).toBe(true);
|
|
});
|
|
|
|
it('encodes false as a present attribute, true as absent', () => {
|
|
const def = disabled();
|
|
expect(def.encode(true)).toBeNull();
|
|
expect(def.encode(false)).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('json', () => {
|
|
it('decodes via JSON.parse and encodes via JSON.stringify', () => {
|
|
const def = json<{ a: number }>({ a: 0 });
|
|
expect(def.decode('{"a":5}')).toEqual({ a: 5 });
|
|
expect(def.encode({ a: 5 })).toBe('{"a":5}');
|
|
});
|
|
});
|
|
|
|
describe('positional', () => {
|
|
it('adds option: false while preserving every other field', () => {
|
|
const base = num(5, { get: () => 1 });
|
|
const pos = positional(base);
|
|
expect(pos.option).toBe(false);
|
|
expect(pos.default).toBe(5);
|
|
expect(pos.get).toBe(base.get);
|
|
expect(pos.decode).toBe(base.decode);
|
|
});
|
|
});
|