|
| 1 | +import {expectTypeOf, it} from 'vitest'; |
| 2 | +import ManyKeysMap from './index.js'; |
| 3 | + |
| 4 | +it('should be a class that extends Map', () => { |
| 5 | + // Use toBeConstructibleWith() for checking if a class is constructible |
| 6 | + expectTypeOf(ManyKeysMap).toBeConstructibleWith(); |
| 7 | + expectTypeOf(ManyKeysMap.prototype).toMatchTypeOf(Map.prototype); |
| 8 | +}); |
| 9 | + |
| 10 | +it('should correctly infer key and value types', () => { |
| 11 | + type KeyTuple = [string, number]; |
| 12 | + type ValueType = {id: string; name: string}; |
| 13 | + |
| 14 | + const map = new ManyKeysMap<KeyTuple, ValueType>(); |
| 15 | + |
| 16 | + // Use toEqualTypeOf() for checking the type of an instance |
| 17 | + expectTypeOf(map).toEqualTypeOf<ManyKeysMap<KeyTuple, ValueType>>(); |
| 18 | + expectTypeOf(map).not.toEqualTypeOf<ManyKeysMap<[number], string>>(); |
| 19 | + |
| 20 | + // Test set method |
| 21 | + expectTypeOf(map.set).parameter(0).toEqualTypeOf<KeyTuple>(); |
| 22 | + expectTypeOf(map.set).parameter(1).toEqualTypeOf<ValueType>(); |
| 23 | + expectTypeOf(map.set(['a', 1], {id: 'x', name: 'y'})).toEqualTypeOf<typeof map>(); |
| 24 | + |
| 25 | + // Test get method |
| 26 | + expectTypeOf(map.get).parameter(0).toEqualTypeOf<KeyTuple>(); |
| 27 | + expectTypeOf(map.get(['a', 1])).toEqualTypeOf<ValueType | undefined>(); |
| 28 | + |
| 29 | + // Test has method |
| 30 | + expectTypeOf(map.has).parameter(0).toEqualTypeOf<KeyTuple>(); |
| 31 | + expectTypeOf(map.has(['a', 1])).toEqualTypeOf<boolean>(); |
| 32 | + |
| 33 | + // Test delete method |
| 34 | + expectTypeOf(map.delete).parameter(0).toEqualTypeOf<KeyTuple>(); |
| 35 | + expectTypeOf(map.delete(['a', 1])).toEqualTypeOf<boolean>(); |
| 36 | +}); |
| 37 | + |
| 38 | +it('should handle different key and value types', () => { |
| 39 | + type DiverseKey = [boolean, number, string]; |
| 40 | + type DiverseValue = {status: 'active' | 'inactive'}; |
| 41 | + |
| 42 | + const map = new ManyKeysMap<DiverseKey, DiverseValue>(); |
| 43 | + |
| 44 | + expectTypeOf(map.set([true, 123, 'hello'], {status: 'active'})).toEqualTypeOf<typeof map>(); |
| 45 | + expectTypeOf(map.get([false, 456, 'world'])).toEqualTypeOf<DiverseValue | undefined>(); |
| 46 | +}); |
0 commit comments