|
| 1 | +'use strict'; |
| 2 | +import { ANIMATION_NAME_PREFIX } from '../../../constants'; |
| 3 | +import type { CSSAnimationEventType } from '../CSSEventHandlersRegistry'; |
| 4 | +import cssEventHandlersRegistry from '../CSSEventHandlersRegistry'; |
| 5 | + |
| 6 | +const animationName = (id: number) => `${ANIMATION_NAME_PREFIX}${id}`; |
| 7 | + |
| 8 | +function animationEvent( |
| 9 | + viewTag: number, |
| 10 | + type: CSSAnimationEventType, |
| 11 | + elapsedTime: number, |
| 12 | + nameId = 0 |
| 13 | +) { |
| 14 | + return { |
| 15 | + viewTag, |
| 16 | + type, |
| 17 | + animationName: animationName(nameId), |
| 18 | + elapsedTime, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +describe('CSSEventHandlersRegistry', () => { |
| 23 | + const viewTag1 = 1; |
| 24 | + const viewTag2 = 2; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + cssEventHandlersRegistry.clear(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('addListener', () => { |
| 31 | + test('registers a handler for a view and event type', () => { |
| 32 | + const handler = jest.fn(); |
| 33 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationstart', handler); |
| 34 | + |
| 35 | + const event = animationEvent(viewTag1, 'animationstart', 0); |
| 36 | + cssEventHandlersRegistry.handleEvents([event]); |
| 37 | + |
| 38 | + expect(handler).toHaveBeenCalledTimes(1); |
| 39 | + expect(handler).toHaveBeenCalledWith(event); |
| 40 | + }); |
| 41 | + |
| 42 | + test('replaces existing handler for the same view and event type', () => { |
| 43 | + const handler1 = jest.fn(); |
| 44 | + const handler2 = jest.fn(); |
| 45 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationend', handler1); |
| 46 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationend', handler2); |
| 47 | + |
| 48 | + const event = animationEvent(viewTag1, 'animationend', 1); |
| 49 | + cssEventHandlersRegistry.handleEvents([event]); |
| 50 | + |
| 51 | + expect(handler1).not.toHaveBeenCalled(); |
| 52 | + expect(handler2).toHaveBeenCalledTimes(1); |
| 53 | + expect(handler2).toHaveBeenCalledWith(event); |
| 54 | + }); |
| 55 | + |
| 56 | + test('supports multiple event types for the same view', () => { |
| 57 | + const startHandler = jest.fn(); |
| 58 | + const endHandler = jest.fn(); |
| 59 | + cssEventHandlersRegistry.addListener( |
| 60 | + viewTag1, |
| 61 | + 'animationstart', |
| 62 | + startHandler |
| 63 | + ); |
| 64 | + cssEventHandlersRegistry.addListener( |
| 65 | + viewTag1, |
| 66 | + 'animationend', |
| 67 | + endHandler |
| 68 | + ); |
| 69 | + |
| 70 | + const startEvent = animationEvent(viewTag1, 'animationstart', 0); |
| 71 | + const endEvent = animationEvent(viewTag1, 'animationend', 1); |
| 72 | + cssEventHandlersRegistry.handleEvents([startEvent, endEvent]); |
| 73 | + |
| 74 | + expect(startHandler).toHaveBeenCalledTimes(1); |
| 75 | + expect(startHandler).toHaveBeenCalledWith(startEvent); |
| 76 | + expect(endHandler).toHaveBeenCalledTimes(1); |
| 77 | + expect(endHandler).toHaveBeenCalledWith(endEvent); |
| 78 | + }); |
| 79 | + |
| 80 | + test('supports handlers for different views', () => { |
| 81 | + const handler1 = jest.fn(); |
| 82 | + const handler2 = jest.fn(); |
| 83 | + cssEventHandlersRegistry.addListener( |
| 84 | + viewTag1, |
| 85 | + 'animationstart', |
| 86 | + handler1 |
| 87 | + ); |
| 88 | + cssEventHandlersRegistry.addListener( |
| 89 | + viewTag2, |
| 90 | + 'animationstart', |
| 91 | + handler2 |
| 92 | + ); |
| 93 | + |
| 94 | + const event = animationEvent(viewTag1, 'animationstart', 0); |
| 95 | + cssEventHandlersRegistry.handleEvents([event]); |
| 96 | + |
| 97 | + expect(handler1).toHaveBeenCalledTimes(1); |
| 98 | + expect(handler1).toHaveBeenCalledWith(event); |
| 99 | + expect(handler2).not.toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('removeListener', () => { |
| 104 | + test('stops the handler from being called', () => { |
| 105 | + const handler = jest.fn(); |
| 106 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationstart', handler); |
| 107 | + cssEventHandlersRegistry.removeListener(viewTag1, 'animationstart'); |
| 108 | + |
| 109 | + cssEventHandlersRegistry.handleEvents([ |
| 110 | + animationEvent(viewTag1, 'animationstart', 0), |
| 111 | + ]); |
| 112 | + |
| 113 | + expect(handler).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + test('does not throw for non-existent view', () => { |
| 117 | + expect(() => { |
| 118 | + cssEventHandlersRegistry.removeListener(999, 'animationstart'); |
| 119 | + }).not.toThrow(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('handleEvents', () => { |
| 124 | + test('ignores events for views with no handlers', () => { |
| 125 | + const handler = jest.fn(); |
| 126 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationstart', handler); |
| 127 | + |
| 128 | + cssEventHandlersRegistry.handleEvents([ |
| 129 | + animationEvent(viewTag2, 'animationstart', 0), |
| 130 | + ]); |
| 131 | + |
| 132 | + expect(handler).not.toHaveBeenCalled(); |
| 133 | + }); |
| 134 | + |
| 135 | + test('ignores events for unregistered event types', () => { |
| 136 | + const handler = jest.fn(); |
| 137 | + cssEventHandlersRegistry.addListener(viewTag1, 'animationstart', handler); |
| 138 | + |
| 139 | + cssEventHandlersRegistry.handleEvents([ |
| 140 | + animationEvent(viewTag1, 'animationend', 1), |
| 141 | + ]); |
| 142 | + |
| 143 | + expect(handler).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + test('dispatches multiple events in batch order', () => { |
| 147 | + const calls: string[] = []; |
| 148 | + const startHandler = () => calls.push('start'); |
| 149 | + const iterationHandler = () => calls.push('iteration'); |
| 150 | + const endHandler = () => calls.push('end'); |
| 151 | + |
| 152 | + cssEventHandlersRegistry.addListener( |
| 153 | + viewTag1, |
| 154 | + 'animationstart', |
| 155 | + startHandler |
| 156 | + ); |
| 157 | + cssEventHandlersRegistry.addListener( |
| 158 | + viewTag1, |
| 159 | + 'animationiteration', |
| 160 | + iterationHandler |
| 161 | + ); |
| 162 | + cssEventHandlersRegistry.addListener( |
| 163 | + viewTag1, |
| 164 | + 'animationend', |
| 165 | + endHandler |
| 166 | + ); |
| 167 | + |
| 168 | + cssEventHandlersRegistry.handleEvents([ |
| 169 | + animationEvent(viewTag1, 'animationstart', 0), |
| 170 | + animationEvent(viewTag1, 'animationiteration', 1), |
| 171 | + animationEvent(viewTag1, 'animationend', 2), |
| 172 | + ]); |
| 173 | + |
| 174 | + expect(calls).toEqual(['start', 'iteration', 'end']); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments