|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +// import 'jasmine'; (google3-only) |
| 8 | + |
| 9 | +import {afterDispatch, setupDispatchHooks} from './dispatch-hooks.js'; |
| 10 | + |
| 11 | +describe('dispatch hooks', () => { |
| 12 | + let element: HTMLDivElement; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + element = document.createElement('div'); |
| 16 | + document.body.appendChild(element); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + document.body.removeChild(element); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('setupDispatchHooks()', () => { |
| 24 | + it('does not add more than one setup listener for an event type', () => { |
| 25 | + spyOn(element, 'addEventListener').and.callThrough(); |
| 26 | + setupDispatchHooks(element, 'foo'); |
| 27 | + setupDispatchHooks(element, 'foo'); |
| 28 | + |
| 29 | + expect(element.addEventListener) |
| 30 | + .withContext('element.addEventListener') |
| 31 | + .toHaveBeenCalledTimes(1); |
| 32 | + }); |
| 33 | + |
| 34 | + it('can add setup listeners for multiple event types', () => { |
| 35 | + spyOn(element, 'addEventListener').and.callThrough(); |
| 36 | + |
| 37 | + setupDispatchHooks(element, 'foo', 'bar', 'baz'); |
| 38 | + expect(element.addEventListener) |
| 39 | + .withContext('element.addEventListener') |
| 40 | + .toHaveBeenCalledTimes(3); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('afterDispatch()', () => { |
| 45 | + it('resolves synchronously after the event is finished dispatching', () => { |
| 46 | + setupDispatchHooks(element, 'click'); |
| 47 | + |
| 48 | + const afterDispatchCallback = jasmine.createSpy('afterDispatchCallback'); |
| 49 | + const clickListener = jasmine |
| 50 | + .createSpy('clickListener') |
| 51 | + .and.callFake((event: Event) => { |
| 52 | + afterDispatch(event, afterDispatchCallback); |
| 53 | + }); |
| 54 | + |
| 55 | + element.addEventListener('click', clickListener); |
| 56 | + element.click(); |
| 57 | + |
| 58 | + expect(clickListener) |
| 59 | + .withContext('clickListener') |
| 60 | + .toHaveBeenCalledTimes(1); |
| 61 | + expect(afterDispatchCallback) |
| 62 | + .withContext('afterDispatch() callback') |
| 63 | + .toHaveBeenCalledTimes(1); |
| 64 | + }); |
| 65 | + |
| 66 | + it('supports multiple afterDispatch listeners', () => { |
| 67 | + setupDispatchHooks(element, 'click'); |
| 68 | + |
| 69 | + const firstAfterDispatchCallback = jasmine.createSpy( |
| 70 | + 'firstAfterDispatchCallback', |
| 71 | + ); |
| 72 | + element.addEventListener('click', (event) => { |
| 73 | + afterDispatch(event, firstAfterDispatchCallback); |
| 74 | + }); |
| 75 | + |
| 76 | + const secondAfterDispatchCallback = jasmine.createSpy( |
| 77 | + 'secondAfterDispatchCallback', |
| 78 | + ); |
| 79 | + element.addEventListener('click', (event) => { |
| 80 | + afterDispatch(event, secondAfterDispatchCallback); |
| 81 | + }); |
| 82 | + |
| 83 | + element.click(); |
| 84 | + |
| 85 | + expect(firstAfterDispatchCallback) |
| 86 | + .withContext('afterDispatch() first callback') |
| 87 | + .toHaveBeenCalledTimes(1); |
| 88 | + expect(secondAfterDispatchCallback) |
| 89 | + .withContext('afterDispatch() second callback') |
| 90 | + .toHaveBeenCalledTimes(1); |
| 91 | + }); |
| 92 | + |
| 93 | + it('resolves synchronously after the event is finished dispatching', () => { |
| 94 | + setupDispatchHooks(element, 'click'); |
| 95 | + |
| 96 | + const afterDispatchCallback = jasmine.createSpy('afterDispatchCallback'); |
| 97 | + const clickListener = jasmine |
| 98 | + .createSpy('clickListener') |
| 99 | + .and.callFake((event: Event) => { |
| 100 | + afterDispatch(event, afterDispatchCallback); |
| 101 | + }); |
| 102 | + |
| 103 | + element.addEventListener('click', clickListener); |
| 104 | + element.click(); |
| 105 | + |
| 106 | + expect(clickListener) |
| 107 | + .withContext('clickListener') |
| 108 | + .toHaveBeenCalledTimes(1); |
| 109 | + expect(afterDispatchCallback) |
| 110 | + .withContext('afterDispatch() callback') |
| 111 | + .toHaveBeenCalledTimes(1); |
| 112 | + }); |
| 113 | + |
| 114 | + it('can be used to synchronously detect if event was canceled', () => { |
| 115 | + setupDispatchHooks(element, 'click'); |
| 116 | + |
| 117 | + // element listener |
| 118 | + let eventDefaultPreventedInAfterDispatch: boolean | null = null; |
| 119 | + element.addEventListener('click', (event) => { |
| 120 | + afterDispatch(event, () => { |
| 121 | + eventDefaultPreventedInAfterDispatch = event.defaultPrevented; |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + // client listener |
| 126 | + element.addEventListener('click', (event) => { |
| 127 | + event.preventDefault(); |
| 128 | + }); |
| 129 | + |
| 130 | + element.click(); |
| 131 | + |
| 132 | + expect(eventDefaultPreventedInAfterDispatch) |
| 133 | + .withContext('event.defaultPrevented() in afterDispatch() callback') |
| 134 | + .toBeTrue(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('throws if setupDispatchHooks() was not called for the event type', () => { |
| 138 | + // Do not set up hooks |
| 139 | + let errorThrown: unknown; |
| 140 | + element.addEventListener('click', (event) => { |
| 141 | + try { |
| 142 | + afterDispatch(event, () => {}); |
| 143 | + } catch (error) { |
| 144 | + errorThrown = error; |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + element.click(); |
| 149 | + expect(errorThrown) |
| 150 | + .withContext('error thrown calling afterDispatch()') |
| 151 | + .toBeInstanceOf(Error); |
| 152 | + |
| 153 | + expect((errorThrown as Error).message) |
| 154 | + .withContext('errorThrown.message') |
| 155 | + .toMatch('setupDispatchHooks'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('does not fire multiple times if setupDispatchHooks() is called multiple times for the same element', () => { |
| 159 | + setupDispatchHooks(element, 'click'); |
| 160 | + setupDispatchHooks(element, 'click'); |
| 161 | + |
| 162 | + const afterDispatchCallback = jasmine.createSpy('afterDispatchCallback'); |
| 163 | + const clickListener = jasmine |
| 164 | + .createSpy('clickListener') |
| 165 | + .and.callFake((event: Event) => { |
| 166 | + afterDispatch(event, afterDispatchCallback); |
| 167 | + }); |
| 168 | + |
| 169 | + element.addEventListener('click', clickListener); |
| 170 | + element.click(); |
| 171 | + |
| 172 | + expect(clickListener) |
| 173 | + .withContext('clickListener') |
| 174 | + .toHaveBeenCalledTimes(1); |
| 175 | + expect(afterDispatchCallback) |
| 176 | + .withContext('afterDispatch() callback') |
| 177 | + .toHaveBeenCalledTimes(1); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments