|
| 1 | +import type { EventId, ItemIds, NodeId, StringId, StyleSheetId } from './itemIds' |
| 2 | +import { |
| 3 | + createEventIds, |
| 4 | + createNodeIds, |
| 5 | + createStringIds, |
| 6 | + createStyleSheetIds, |
| 7 | + EventIdConstants, |
| 8 | + NodeIdConstants, |
| 9 | + StringIdConstants, |
| 10 | + StyleSheetIdConstants, |
| 11 | +} from './itemIds' |
| 12 | + |
| 13 | +describe('ItemIds', () => { |
| 14 | + const describeItemIdVariant = <ItemType, ItemId extends number>( |
| 15 | + name: string, |
| 16 | + createIdMap: () => ItemIds<ItemType, ItemId>, |
| 17 | + createItem: () => ItemType, |
| 18 | + firstId: ItemId |
| 19 | + ) => { |
| 20 | + describe(name, () => { |
| 21 | + let itemIds = createIdMap() |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + itemIds = createIdMap() |
| 25 | + }) |
| 26 | + |
| 27 | + describe('clear', () => { |
| 28 | + it('removes all id mappings', () => { |
| 29 | + const item = createItem() |
| 30 | + itemIds.getOrInsert(item) |
| 31 | + expect(itemIds.get(item)).toBe(firstId) |
| 32 | + |
| 33 | + itemIds.clear() |
| 34 | + expect(itemIds.get(item)).toBeUndefined() |
| 35 | + }) |
| 36 | + |
| 37 | + it('restarts the id sequence', () => { |
| 38 | + for (let id = firstId; id < firstId + 3; id++) { |
| 39 | + const item = createItem() |
| 40 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 41 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 42 | + } |
| 43 | + |
| 44 | + itemIds.clear() |
| 45 | + |
| 46 | + for (let id = firstId; id < firstId + 3; id++) { |
| 47 | + const item = createItem() |
| 48 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 49 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 50 | + } |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('get', () => { |
| 55 | + it('returns undefined for items that have not been assigned an id', () => { |
| 56 | + expect(itemIds.get(createItem())).toBe(undefined) |
| 57 | + }) |
| 58 | + |
| 59 | + it('returns the assigned id if one exists', () => { |
| 60 | + const item = createItem() |
| 61 | + itemIds.getOrInsert(item) |
| 62 | + expect(itemIds.get(item)).toBe(firstId) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('getOrInsert', () => { |
| 67 | + it('assigns ids in order', () => { |
| 68 | + for (let id = firstId; id < firstId + 3; id++) { |
| 69 | + const item = createItem() |
| 70 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 71 | + expect(itemIds.getOrInsert(item)).toBe(id) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + it('reuses any existing id', () => { |
| 76 | + itemIds.getOrInsert(createItem()) |
| 77 | + itemIds.getOrInsert(createItem()) |
| 78 | + const item = createItem() |
| 79 | + const itemId = itemIds.getOrInsert(item) |
| 80 | + expect(itemIds.getOrInsert(item)).toBe(itemId) |
| 81 | + expect(itemIds.get(item)).toBe(itemId) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + describe('size', () => { |
| 86 | + it('increments when an id is assigned', () => { |
| 87 | + expect(itemIds.size).toBe(0) |
| 88 | + itemIds.getOrInsert(createItem()) |
| 89 | + expect(itemIds.size).toBe(1) |
| 90 | + itemIds.getOrInsert(createItem()) |
| 91 | + expect(itemIds.size).toBe(2) |
| 92 | + }) |
| 93 | + }) |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + describeItemIdVariant( |
| 98 | + 'EventIds', |
| 99 | + createEventIds, |
| 100 | + () => new Event('someCustomEvent'), |
| 101 | + EventIdConstants.FIRST_ID as EventId |
| 102 | + ) |
| 103 | + |
| 104 | + describeItemIdVariant( |
| 105 | + 'NodeIds', |
| 106 | + createNodeIds, |
| 107 | + () => document.createElement('div'), |
| 108 | + NodeIdConstants.FIRST_ID as NodeId |
| 109 | + ) |
| 110 | + |
| 111 | + let nextString = 0 |
| 112 | + describeItemIdVariant( |
| 113 | + 'StringIds', |
| 114 | + createStringIds, |
| 115 | + () => `string${nextString++}`, |
| 116 | + StringIdConstants.FIRST_ID as StringId |
| 117 | + ) |
| 118 | + |
| 119 | + describeItemIdVariant( |
| 120 | + 'StyleSheetIds', |
| 121 | + createStyleSheetIds, |
| 122 | + // The CSSStyleSheet constructor is not available on older browsers. |
| 123 | + () => ({ type: 'CSSStyleSheet' }), |
| 124 | + StyleSheetIdConstants.FIRST_ID as StyleSheetId |
| 125 | + ) |
| 126 | +}) |
0 commit comments