|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { autoPrefix } from './index.js'; |
| 3 | + |
| 4 | +describe('autoPrefix', () => { |
| 5 | + it('returns "unknown" if tns is falsy', () => { |
| 6 | + const nsMap = {}; |
| 7 | + expect(autoPrefix('', nsMap)).toBe('unknown'); |
| 8 | + expect(autoPrefix(null, nsMap)).toBe('unknown'); |
| 9 | + expect(autoPrefix(undefined, nsMap)).toBe('unknown'); |
| 10 | + }); |
| 11 | + |
| 12 | + it('returns existing prefix if tns is already in nsMap', () => { |
| 13 | + const nsMap = { 'http://example.com': 'ex' }; |
| 14 | + const result = autoPrefix('http://example.com', nsMap); |
| 15 | + expect(result).toBe('ex'); |
| 16 | + expect(nsMap).toEqual({ 'http://example.com': 'ex' }); // unchanged |
| 17 | + }); |
| 18 | + |
| 19 | + it('assigns a new prefix if tns is not in nsMap', () => { |
| 20 | + const nsMap = {}; |
| 21 | + const result = autoPrefix('http://new.com', nsMap); |
| 22 | + expect(result).toBe('g0'); |
| 23 | + expect(nsMap).toEqual({ 'http://new.com': 'g0' }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('assigns incrementing prefixes based on nsMap size', () => { |
| 27 | + const nsMap = { 'http://one.com': 'g0', 'http://two.com': 'g1' }; |
| 28 | + const result = autoPrefix('http://three.com', nsMap); |
| 29 | + expect(result).toBe('g2'); |
| 30 | + expect(nsMap).toEqual({ |
| 31 | + 'http://one.com': 'g0', |
| 32 | + 'http://two.com': 'g1', |
| 33 | + 'http://three.com': 'g2', |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not overwrite existing mapping for different tns', () => { |
| 38 | + const nsMap = { 'http://foo.com': 'g0' }; |
| 39 | + autoPrefix('http://bar.com', nsMap); |
| 40 | + expect(nsMap).toHaveProperty('http://foo.com', 'g0'); |
| 41 | + expect(nsMap).toHaveProperty('http://bar.com', 'g1'); |
| 42 | + }); |
| 43 | +}); |
0 commit comments