|
| 1 | +import type { CompatibilityContext } from '../../../shared/context.ts'; |
| 2 | + |
| 3 | +export function register(context: CompatibilityContext): void { |
| 4 | + const { expect } = context.runner; |
| 5 | + |
| 6 | + context.test( |
| 7 | + 'Removing all listeners and then adding a new one should work.', |
| 8 | + ({ document, Event }) => { |
| 9 | + const type = 'foo'; |
| 10 | + const target = document.createElement('div'); |
| 11 | + |
| 12 | + let listener1CallCount = 0; |
| 13 | + let listener2CallCount = 0; |
| 14 | + let listener3CallCount = 0; |
| 15 | + function listener1(): void { |
| 16 | + listener1CallCount++; |
| 17 | + target.removeEventListener(type, listener1); |
| 18 | + target.removeEventListener(type, listener2); |
| 19 | + target.addEventListener(type, listener3); |
| 20 | + } |
| 21 | + function listener2(): void { |
| 22 | + listener2CallCount++; |
| 23 | + } |
| 24 | + function listener3(): void { |
| 25 | + listener3CallCount++; |
| 26 | + } |
| 27 | + |
| 28 | + target.addEventListener(type, listener1); |
| 29 | + target.addEventListener(type, listener2); |
| 30 | + |
| 31 | + // Dispatch the event. Only listener1 should be called because |
| 32 | + // it removes listener2. And listener3 is added when we've already |
| 33 | + // started iterating, so it shouldn't be called either. |
| 34 | + target.dispatchEvent(new Event(type)); |
| 35 | + expect(listener1CallCount).toBe(1); |
| 36 | + expect(listener2CallCount).toBe(0); |
| 37 | + expect(listener3CallCount).toBe(0); |
| 38 | + |
| 39 | + // Now that only listener3 is set, dispatch another event. Only |
| 40 | + // listener3 should be called. |
| 41 | + target.dispatchEvent(new Event(type)); |
| 42 | + expect(listener1CallCount).toBe(1); |
| 43 | + expect(listener2CallCount).toBe(0); |
| 44 | + expect(listener3CallCount).toBe(1); |
| 45 | + }, |
| 46 | + ); |
| 47 | + |
| 48 | + context.test('Nested usage of once listeners should work.', ({ document, Event, step }) => { |
| 49 | + const type = 'foo'; |
| 50 | + const target = document.createElement('div'); |
| 51 | + |
| 52 | + let listener1CallCount = 0; |
| 53 | + let listener2CallCount = 0; |
| 54 | + let listener3CallCount = 0; |
| 55 | + const listener1 = step(function (): void { |
| 56 | + listener1CallCount++; |
| 57 | + // Recursively dispatch another event from this listener. |
| 58 | + // This will only call listener2 because listener1 is a "once" listener. |
| 59 | + target.dispatchEvent(new Event(type)); |
| 60 | + expect(listener1CallCount).toBe(1); |
| 61 | + expect(listener2CallCount).toBe(1); |
| 62 | + expect(listener3CallCount).toBe(0); |
| 63 | + |
| 64 | + // Now all listeners are removed - the two "once" listeners have already both |
| 65 | + // been called once. Add another listener. |
| 66 | + target.addEventListener(type, listener3); |
| 67 | + }); |
| 68 | + function listener2(): void { |
| 69 | + listener2CallCount++; |
| 70 | + } |
| 71 | + function listener3(): void { |
| 72 | + listener3CallCount++; |
| 73 | + } |
| 74 | + |
| 75 | + // Add two "once" listeners. |
| 76 | + target.addEventListener(type, listener1, { once: true }); |
| 77 | + target.addEventListener(type, listener2, { once: true }); |
| 78 | + |
| 79 | + // Dispatch the event. |
| 80 | + target.dispatchEvent(new Event(type)); |
| 81 | + |
| 82 | + // The listener call counts should still match what they were |
| 83 | + // at the end of listener1. |
| 84 | + expect(listener1CallCount).toBe(1); |
| 85 | + expect(listener2CallCount).toBe(1); |
| 86 | + expect(listener3CallCount).toBe(0); |
| 87 | + |
| 88 | + // Now that only listener3 is set, dispatch another event. Only |
| 89 | + // listener3 should be called. |
| 90 | + target.dispatchEvent(new Event(type)); |
| 91 | + expect(listener1CallCount).toBe(1); |
| 92 | + expect(listener2CallCount).toBe(1); |
| 93 | + expect(listener3CallCount).toBe(1); |
| 94 | + }); |
| 95 | +} |
0 commit comments