๐Ÿ“ฆ leonardomso / 33-js-concepts

๐Ÿ“„ event-delegation.test.js ยท 536 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536/**
 * @vitest-environment jsdom
 */

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'

/**
 * Tests for Event Delegation concept page
 * Source: docs/beyond/concepts/event-delegation.mdx
 */

describe('Event Delegation', () => {
  let container

  beforeEach(() => {
    // Set up a fresh DOM container for each test
    container = document.createElement('div')
    container.id = 'test-container'
    document.body.appendChild(container)
  })

  afterEach(() => {
    // Clean up after each test
    document.body.removeChild(container)
    container = null
  })

  describe('event.target vs event.currentTarget', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:78-85
    it('should demonstrate target vs currentTarget difference', () => {
      // Set up nested structure
      container.innerHTML = `
        <ul id="menu">
          <li>
            <button id="test-button">Click</button>
          </li>
        </ul>
      `

      const menu = document.getElementById('menu')
      const button = document.getElementById('test-button')
      
      let capturedTarget = null
      let capturedCurrentTarget = null

      menu.addEventListener('click', (event) => {
        capturedTarget = event.target
        capturedCurrentTarget = event.currentTarget
      })

      // Simulate click on the button
      button.click()

      // target is the button (what was clicked)
      expect(capturedTarget).toBe(button)
      expect(capturedTarget.tagName).toBe('BUTTON')
      
      // currentTarget is the ul (where listener is attached)
      expect(capturedCurrentTarget).toBe(menu)
      expect(capturedCurrentTarget.tagName).toBe('UL')
    })

    // Source: docs/beyond/concepts/event-delegation.mdx:87-102
    it('should show target changes based on click location while currentTarget stays constant', () => {
      container.innerHTML = `
        <div id="outer">
          <p id="para">
            <span id="inner">Click me</span>
          </p>
        </div>
      `

      const outer = document.getElementById('outer')
      const span = document.getElementById('inner')
      const para = document.getElementById('para')
      
      const targets = []
      const currentTargets = []

      outer.addEventListener('click', (event) => {
        targets.push(event.target)
        currentTargets.push(event.currentTarget)
      })

      // Click the span
      span.click()
      expect(targets[0]).toBe(span)
      expect(currentTargets[0]).toBe(outer)

      // Click the paragraph
      para.click()
      expect(targets[1]).toBe(para)
      expect(currentTargets[1]).toBe(outer) // Always outer
    })
  })

  describe('Element.matches() for filtering', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:104-130
    it('should filter events using matches()', () => {
      container.innerHTML = `
        <div class="container">
          <button class="btn">Regular</button>
          <button class="btn delete-btn">Delete</button>
          <button class="btn primary" disabled>Disabled Primary</button>
          <button class="btn primary">Enabled Primary</button>
          <span data-action="test">Not a button</span>
        </div>
      `

      const containerEl = container.querySelector('.container')
      const results = []

      containerEl.addEventListener('click', (event) => {
        if (event.target.matches('button')) {
          results.push('button')
        }
        if (event.target.matches('.delete-btn')) {
          results.push('delete')
        }
        if (event.target.matches('[data-action]')) {
          results.push('action: ' + event.target.dataset.action)
        }
        if (event.target.matches('button.primary:not(:disabled)')) {
          results.push('enabled-primary')
        }
      })

      // Click delete button
      container.querySelector('.delete-btn').click()
      expect(results).toContain('button')
      expect(results).toContain('delete')

      // Reset and click span with data-action
      results.length = 0
      container.querySelector('[data-action]').click()
      expect(results).toContain('action: test')
      expect(results).not.toContain('button')

      // Reset and click enabled primary button
      results.length = 0
      container.querySelector('button.primary:not(:disabled)').click()
      expect(results).toContain('button')
      expect(results).toContain('enabled-primary')
    })
  })

  describe('Element.closest() for nested elements', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:132-162
    it('should find ancestor elements using closest()', () => {
      container.innerHTML = `
        <div class="container">
          <button class="action-btn">
            <i class="icon" id="icon-element">icon</i>
            <span id="span-element">Delete</span>
          </button>
        </div>
      `

      const containerEl = container.querySelector('.container')
      const icon = document.getElementById('icon-element')
      const span = document.getElementById('span-element')
      const button = container.querySelector('.action-btn')
      
      let foundButton = null

      containerEl.addEventListener('click', (event) => {
        // Use closest to find the button, regardless of what was clicked
        foundButton = event.target.closest('.action-btn')
      })

      // Click the icon inside the button
      icon.click()
      expect(foundButton).toBe(button)

      // Click the span inside the button
      foundButton = null
      span.click()
      expect(foundButton).toBe(button)

      // Click the button itself
      foundButton = null
      button.click()
      expect(foundButton).toBe(button)
    })

    it('should return null when no ancestor matches', () => {
      container.innerHTML = `
        <div class="container">
          <span class="outside">Outside</span>
          <button class="action-btn">Inside</button>
        </div>
      `

      const containerEl = container.querySelector('.container')
      const outside = container.querySelector('.outside')
      
      let foundButton = null

      containerEl.addEventListener('click', (event) => {
        foundButton = event.target.closest('.action-btn')
      })

      outside.click()
      expect(foundButton).toBeNull()
    })
  })

  describe('Basic event delegation pattern', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:188-215
    it('should handle clicks on list items with delegation', () => {
      container.innerHTML = `
        <ul id="todo-list">
          <li data-id="1">Buy groceries</li>
          <li data-id="2">Walk the dog</li>
          <li data-id="3">Finish report</li>
        </ul>
      `

      const todoList = document.getElementById('todo-list')
      const clickedIds = []

      todoList.addEventListener('click', (event) => {
        const item = event.target.closest('li')
        if (item) {
          clickedIds.push(item.dataset.id)
          item.classList.toggle('completed')
        }
      })

      // Click first item
      container.querySelector('li[data-id="1"]').click()
      expect(clickedIds).toContain('1')
      expect(container.querySelector('li[data-id="1"]').classList.contains('completed')).toBe(true)

      // Click second item
      container.querySelector('li[data-id="2"]').click()
      expect(clickedIds).toContain('2')

      // Click first item again to toggle off
      container.querySelector('li[data-id="1"]').click()
      expect(container.querySelector('li[data-id="1"]').classList.contains('completed')).toBe(false)
    })
  })

  describe('Handling dynamic elements', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:221-262
    it('should automatically handle dynamically added elements', () => {
      container.innerHTML = `<ul id="todo-list"></ul>`

      const todoList = document.getElementById('todo-list')
      const clickedTexts = []

      // Set up delegation BEFORE adding items
      todoList.addEventListener('click', (event) => {
        if (event.target.matches('li')) {
          clickedTexts.push(event.target.textContent)
          event.target.classList.toggle('completed')
        }
      })

      // Add items dynamically
      function addTodo(text) {
        const li = document.createElement('li')
        li.textContent = text
        todoList.appendChild(li)
      }

      addTodo('First task')
      addTodo('Second task')
      addTodo('Third task')

      // Click dynamically added items - they should work!
      const items = todoList.querySelectorAll('li')
      items[0].click()
      items[1].click()

      expect(clickedTexts).toEqual(['First task', 'Second task'])
      expect(items[0].classList.contains('completed')).toBe(true)
      expect(items[1].classList.contains('completed')).toBe(true)
      expect(items[2].classList.contains('completed')).toBe(false)
    })
  })

  describe('Action buttons with data-action pattern', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:308-334
    it('should dispatch to correct action based on data-action attribute', () => {
      container.innerHTML = `
        <div id="toolbar">
          <button data-action="save">Save</button>
          <button data-action="load">Load</button>
          <button data-action="delete">Delete</button>
        </div>
      `

      const executedActions = []
      const actions = {
        save() { executedActions.push('save') },
        load() { executedActions.push('load') },
        delete() { executedActions.push('delete') }
      }

      const toolbar = document.getElementById('toolbar')
      toolbar.addEventListener('click', (event) => {
        const action = event.target.dataset.action
        if (action && actions[action]) {
          actions[action]()
        }
      })

      // Click save button
      container.querySelector('[data-action="save"]').click()
      expect(executedActions).toEqual(['save'])

      // Click delete button
      container.querySelector('[data-action="delete"]').click()
      expect(executedActions).toEqual(['save', 'delete'])

      // Click load button
      container.querySelector('[data-action="load"]').click()
      expect(executedActions).toEqual(['save', 'delete', 'load'])
    })
  })

  describe('Tab interface pattern', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:336-361
    it('should switch tabs using delegation', () => {
      container.innerHTML = `
        <div class="tabs">
          <button class="tab" data-tab="home">Home</button>
          <button class="tab" data-tab="profile">Profile</button>
          <button class="tab" data-tab="settings">Settings</button>
        </div>
        <div class="tab-content" id="home">Home content</div>
        <div class="tab-content" id="profile" hidden>Profile content</div>
        <div class="tab-content" id="settings" hidden>Settings content</div>
      `

      const tabsContainer = container.querySelector('.tabs')
      
      tabsContainer.addEventListener('click', (event) => {
        const tab = event.target.closest('.tab')
        if (!tab) return

        // Remove active class from all tabs
        container.querySelectorAll('.tab').forEach(t => t.classList.remove('active'))
        tab.classList.add('active')

        // Hide all content, show selected
        const tabId = tab.dataset.tab
        container.querySelectorAll('.tab-content').forEach(content => {
          content.hidden = content.id !== tabId
        })
      })

      // Click profile tab
      container.querySelector('[data-tab="profile"]').click()
      
      expect(container.querySelector('[data-tab="profile"]').classList.contains('active')).toBe(true)
      expect(container.querySelector('[data-tab="home"]').classList.contains('active')).toBe(false)
      expect(document.getElementById('profile').hidden).toBe(false)
      expect(document.getElementById('home').hidden).toBe(true)
      expect(document.getElementById('settings').hidden).toBe(true)

      // Click settings tab
      container.querySelector('[data-tab="settings"]').click()
      
      expect(container.querySelector('[data-tab="settings"]').classList.contains('active')).toBe(true)
      expect(container.querySelector('[data-tab="profile"]').classList.contains('active')).toBe(false)
      expect(document.getElementById('settings').hidden).toBe(false)
      expect(document.getElementById('profile').hidden).toBe(true)
    })
  })

  describe('Container boundary verification', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:468-482
    it('should verify element is within container using contains()', () => {
      container.innerHTML = `
        <table id="outer-table">
          <tr>
            <td>
              Outer cell
              <table id="inner-table">
                <tr><td id="inner-cell">Inner cell</td></tr>
              </table>
            </td>
          </tr>
          <tr><td id="outer-cell">Real outer cell</td></tr>
        </table>
      `

      const outerTable = document.getElementById('outer-table')
      const innerCell = document.getElementById('inner-cell')
      const outerCell = document.getElementById('outer-cell')
      
      const handledCells = []

      outerTable.addEventListener('click', (event) => {
        const td = event.target.closest('td')
        
        // Only handle cells that are direct children of outer table
        // Using a more specific check
        if (td && td.closest('table') === outerTable) {
          handledCells.push(td.id || 'unnamed')
        }
      })

      // Click inner cell - should NOT be handled (belongs to inner table)
      innerCell.click()
      expect(handledCells).not.toContain('inner-cell')

      // Click outer cell - should be handled
      outerCell.click()
      expect(handledCells).toContain('outer-cell')
    })
  })

  describe('focusin/focusout delegation (bubbling alternatives)', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:444-461
    it('should delegate focus events using focusin/focusout', () => {
      container.innerHTML = `
        <form class="form">
          <input type="text" id="input1" />
          <input type="email" id="input2" />
        </form>
      `

      const form = container.querySelector('.form')
      const input1 = document.getElementById('input1')
      const input2 = document.getElementById('input2')
      
      const focusedInputs = []
      const blurredInputs = []

      // focusin and focusout bubble, unlike focus and blur
      form.addEventListener('focusin', (event) => {
        if (event.target.matches('input')) {
          event.target.classList.add('focused')
          focusedInputs.push(event.target.id)
        }
      })

      form.addEventListener('focusout', (event) => {
        if (event.target.matches('input')) {
          event.target.classList.remove('focused')
          blurredInputs.push(event.target.id)
        }
      })

      // Focus first input
      input1.focus()
      expect(focusedInputs).toContain('input1')
      expect(input1.classList.contains('focused')).toBe(true)

      // Focus second input (should blur first)
      input2.focus()
      expect(focusedInputs).toContain('input2')
      expect(blurredInputs).toContain('input1')
      expect(input2.classList.contains('focused')).toBe(true)
      expect(input1.classList.contains('focused')).toBe(false)
    })
  })

  describe('Common mistakes', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:519-530
    describe('Mistake 1: Not using closest() for nested elements', () => {
      it('should demonstrate why matches() alone fails with nested elements', () => {
        container.innerHTML = `
          <div class="container">
            <button class="btn">
              <span class="icon">X</span>
              <span class="text">Delete</span>
            </button>
          </div>
        `

        const containerEl = container.querySelector('.container')
        const icon = container.querySelector('.icon')
        
        let matchesFound = false
        let closestFound = null

        containerEl.addEventListener('click', (event) => {
          // Wrong way - matches() returns false for child elements
          matchesFound = event.target.matches('.btn')
          
          // Right way - closest() finds the button ancestor
          closestFound = event.target.closest('.btn')
        })

        // Click on the icon inside the button
        icon.click()

        // matches() fails because icon is not .btn
        expect(matchesFound).toBe(false)
        
        // closest() succeeds by traversing up to find .btn
        expect(closestFound).not.toBeNull()
        expect(closestFound.classList.contains('btn')).toBe(true)
      })
    })
  })

  describe('Performance comparison', () => {
    // Source: docs/beyond/concepts/event-delegation.mdx:409-425
    it('should handle many elements with single listener', () => {
      // Create 100 items
      container.innerHTML = '<ul class="list"></ul>'
      const list = container.querySelector('.list')
      
      for (let i = 0; i < 100; i++) {
        const li = document.createElement('li')
        li.className = 'item'
        li.dataset.id = i.toString()
        li.textContent = `Item ${i}`
        list.appendChild(li)
      }

      const clickedItems = []

      // Single delegated listener handles all items
      list.addEventListener('click', (event) => {
        if (event.target.matches('.item')) {
          clickedItems.push(event.target.dataset.id)
        }
      })

      // Click various items
      list.querySelector('[data-id="0"]').click()
      list.querySelector('[data-id="50"]').click()
      list.querySelector('[data-id="99"]').click()

      expect(clickedItems).toEqual(['0', '50', '99'])
    })
  })
})