๐Ÿ“ฆ leonardomso / 33-js-concepts

๐Ÿ“„ intersection-observer.test.js ยท 427 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/**
 * Tests for Intersection Observer concept page
 * Source: /docs/beyond/concepts/intersection-observer.mdx
 * 
 * Note: Intersection Observer is a browser API that cannot be fully tested
 * without a real browser environment. These tests verify the concepts and
 * patterns described in the documentation using mocks and simulated behavior.
 */

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

describe('Intersection Observer Concepts', () => {
  describe('IntersectionObserverEntry Properties', () => {
    // MDX lines ~105-130: IntersectionObserverEntry Properties table
    it('should understand entry property types', () => {
      // Simulating the shape of IntersectionObserverEntry
      const mockEntry = {
        target: { id: 'test-element' },        // Element being observed
        isIntersecting: true,                   // boolean
        intersectionRatio: 0.5,                 // number (0.0 to 1.0)
        boundingClientRect: { top: 100, left: 0, width: 200, height: 100 },
        intersectionRect: { top: 100, left: 0, width: 200, height: 50 },
        rootBounds: { top: 0, left: 0, width: 800, height: 600 },
        time: 1234567890.123                    // DOMHighResTimeStamp
      }

      expect(typeof mockEntry.target).toBe('object')
      expect(typeof mockEntry.isIntersecting).toBe('boolean')
      expect(typeof mockEntry.intersectionRatio).toBe('number')
      expect(mockEntry.intersectionRatio).toBeGreaterThanOrEqual(0)
      expect(mockEntry.intersectionRatio).toBeLessThanOrEqual(1)
      expect(typeof mockEntry.time).toBe('number')
    })

    // MDX lines ~125-135: intersectionRatio calculation
    it('should calculate visibility percentage from intersectionRatio', () => {
      const intersectionRatio = 0.75
      const visibilityPercent = Math.round(intersectionRatio * 100) + '%'
      
      expect(visibilityPercent).toBe('75%')
    })
  })

  describe('Observer Options Validation', () => {
    // MDX lines ~140-165: The root Option
    it('should understand root option defaults', () => {
      const defaultOptions = {
        root: null,           // viewport by default
        rootMargin: '0px',    // no margin by default  
        threshold: 0          // any pixel visible by default
      }

      expect(defaultOptions.root).toBeNull()
      expect(defaultOptions.rootMargin).toBe('0px')
      expect(defaultOptions.threshold).toBe(0)
    })

    // MDX lines ~180-215: The rootMargin Option
    it('should understand rootMargin string format', () => {
      // rootMargin follows CSS margin format
      const validMargins = [
        '100px',                    // All sides
        '100px 0px',                // top/bottom, left/right
        '100px 0px 100px 0px',      // top, right, bottom, left
        '-50px',                    // Negative margins shrink
        '0px 0px -50%',             // Percentages work
        '200px 0px 200px 0px'       // Large buffer for preloading
      ]

      validMargins.forEach(margin => {
        expect(typeof margin).toBe('string')
      })
    })

    // MDX lines ~220-260: The threshold Option
    it('should accept single threshold values', () => {
      const singleThresholds = [0, 0.5, 1.0]
      
      singleThresholds.forEach(threshold => {
        expect(threshold).toBeGreaterThanOrEqual(0)
        expect(threshold).toBeLessThanOrEqual(1)
      })
    })

    it('should accept array of threshold values', () => {
      const thresholdArray = [0, 0.25, 0.5, 0.75, 1.0]
      
      expect(Array.isArray(thresholdArray)).toBe(true)
      thresholdArray.forEach(threshold => {
        expect(threshold).toBeGreaterThanOrEqual(0)
        expect(threshold).toBeLessThanOrEqual(1)
      })
    })
  })

  describe('Lazy Loading Pattern Logic', () => {
    // MDX lines ~315-365: Implementing Lazy Loading Images
    it('should swap data-src to src pattern', () => {
      const mockImage = {
        src: 'placeholder.svg',
        dataset: { src: 'real-image.jpg', srcset: 'image-1x.jpg 1x, image-2x.jpg 2x' },
        classList: {
          removed: [],
          added: [],
          remove(cls) { this.removed.push(cls) },
          add(cls) { this.added.push(cls) }
        }
      }

      // Simulate lazy loading logic
      function lazyLoadImage(img) {
        img.src = img.dataset.src
        if (img.dataset.srcset) {
          img.srcset = img.dataset.srcset
        }
        img.classList.remove('lazy')
        img.classList.add('loaded')
      }

      lazyLoadImage(mockImage)

      expect(mockImage.src).toBe('real-image.jpg')
      expect(mockImage.srcset).toBe('image-1x.jpg 1x, image-2x.jpg 2x')
      expect(mockImage.classList.removed).toContain('lazy')
      expect(mockImage.classList.added).toContain('loaded')
    })
  })

  describe('Infinite Scroll Pattern Logic', () => {
    // MDX lines ~385-440: Building Infinite Scroll
    it('should track loading state to prevent duplicate requests', async () => {
      let isLoading = false
      let page = 1
      const fetchCalls = []

      async function loadMoreContent() {
        if (isLoading) return // Prevent duplicate calls
        
        isLoading = true
        fetchCalls.push(page)
        page++
        
        // Simulate async fetch
        await new Promise(resolve => setTimeout(resolve, 10))
        isLoading = false
      }

      // Simulate rapid scroll events (should only trigger one load)
      await Promise.all([
        loadMoreContent(),
        loadMoreContent(),
        loadMoreContent()
      ])

      // Only one fetch should have happened due to isLoading guard
      expect(fetchCalls.length).toBe(1)
      expect(page).toBe(2)
    })

    it('should stop observing when no more content', () => {
      let observing = true
      const posts = [] // Empty array = no more content
      
      function handleIntersection() {
        if (posts.length === 0) {
          observing = false // Stop observing
          return
        }
      }

      handleIntersection()
      expect(observing).toBe(false)
    })
  })

  describe('Scroll Animation Pattern Logic', () => {
    // MDX lines ~455-500: Scroll-Triggered Animations
    it('should create reusable animation observer factory', () => {
      function createScrollAnimator(options = {}) {
        const {
          threshold = 0.2,
          rootMargin = '0px',
          animateOnce = true,
          animatedClass = 'animated'
        } = options

        return { threshold, rootMargin, animateOnce, animatedClass }
      }

      const defaultAnimator = createScrollAnimator()
      expect(defaultAnimator.threshold).toBe(0.2)
      expect(defaultAnimator.rootMargin).toBe('0px')
      expect(defaultAnimator.animateOnce).toBe(true)
      expect(defaultAnimator.animatedClass).toBe('animated')

      const customAnimator = createScrollAnimator({ 
        threshold: 0.5, 
        animateOnce: false 
      })
      expect(customAnimator.threshold).toBe(0.5)
      expect(customAnimator.animateOnce).toBe(false)
    })
  })

  describe('Cleanup Patterns', () => {
    // MDX lines ~605-650: The #1 Intersection Observer Mistake
    it('should demonstrate cleanup function pattern', () => {
      let disconnected = false
      
      function setupObserver() {
        const mockObserver = {
          observe: vi.fn(),
          unobserve: vi.fn(),
          disconnect: () => { disconnected = true }
        }
        
        // Return cleanup function
        return () => mockObserver.disconnect()
      }

      const cleanup = setupObserver()
      expect(disconnected).toBe(false)
      
      cleanup() // Simulate component unmount
      expect(disconnected).toBe(true)
    })

    it('should unobserve after lazy loading', () => {
      const unobservedElements = []
      
      const mockObserver = {
        unobserve: (element) => unobservedElements.push(element)
      }

      // Simulate lazy load callback
      function handleIntersection(entry, observer) {
        if (entry.isIntersecting) {
          // Load image...
          observer.unobserve(entry.target)
        }
      }

      const mockEntry = { isIntersecting: true, target: { id: 'img1' } }
      handleIntersection(mockEntry, mockObserver)

      expect(unobservedElements).toContainEqual({ id: 'img1' })
    })
  })

  describe('Visibility Detection Logic', () => {
    // MDX lines ~30-60: isIntersecting vs intersectionRatio
    it('should use isIntersecting for simple visibility checks', () => {
      const entries = [
        { isIntersecting: true, intersectionRatio: 0.5 },
        { isIntersecting: false, intersectionRatio: 0 },
        { isIntersecting: true, intersectionRatio: 1.0 }
      ]

      const visibleEntries = entries.filter(e => e.isIntersecting)
      expect(visibleEntries.length).toBe(2)
    })

    it('should use intersectionRatio for progressive visibility tracking', () => {
      const entry = { intersectionRatio: 0.75 }
      
      // Ad viewability: count impression when 50%+ visible
      const isViewable = entry.intersectionRatio >= 0.5
      expect(isViewable).toBe(true)

      // Progress tracking
      const percentVisible = Math.round(entry.intersectionRatio * 100)
      expect(percentVisible).toBe(75)
    })
  })

  describe('Threshold Behavior', () => {
    // MDX lines ~220-260: threshold option behavior
    it('should understand threshold 0 triggers on any visibility', () => {
      // threshold: 0 means callback fires when target touches root boundary
      const callback = vi.fn()
      
      // Simulate entries at threshold 0
      const entryJustVisible = { intersectionRatio: 0.01, isIntersecting: true }
      
      // Even 1% visible should trigger with threshold: 0
      if (entryJustVisible.isIntersecting) {
        callback(entryJustVisible)
      }
      
      expect(callback).toHaveBeenCalled()
    })

    it('should understand threshold 1.0 requires full visibility', () => {
      // threshold: 1.0 means callback fires only when 100% visible
      const threshold = 1.0
      
      const partiallyVisible = { intersectionRatio: 0.9 }
      const fullyVisible = { intersectionRatio: 1.0 }
      
      expect(partiallyVisible.intersectionRatio >= threshold).toBe(false)
      expect(fullyVisible.intersectionRatio >= threshold).toBe(true)
    })

    it('should warn about threshold 1.0 with tall elements', () => {
      // If element is taller than viewport, it can never be 100% visible
      const elementHeight = 1200 // pixels
      const viewportHeight = 800 // pixels
      
      const canBeFullyVisible = elementHeight <= viewportHeight
      expect(canBeFullyVisible).toBe(false)
      
      // In this case, use a lower threshold or check intersectionRatio
      const practicalThreshold = 0.8
      expect(practicalThreshold).toBeLessThan(1.0)
    })
  })

  describe('Common Mistakes Prevention', () => {
    // MDX lines ~680-750: Common Mistakes section
    it('Mistake 2: should use single observer for multiple elements', () => {
      const observers = []
      const elements = ['el1', 'el2', 'el3', 'el4', 'el5']

      // BAD: Creating new observer for each element
      function badPattern() {
        elements.forEach(el => {
          observers.push({ target: el })
        })
        return observers.length
      }

      // GOOD: One observer watching many elements
      function goodPattern() {
        const singleObserver = { targets: [] }
        elements.forEach(el => {
          singleObserver.targets.push(el)
        })
        return 1 // Just one observer
      }

      expect(badPattern()).toBe(5) // 5 "observers"
      expect(goodPattern()).toBe(1) // 1 observer
    })

    it('Mistake 3: should check isIntersecting in callback', () => {
      // Callback fires immediately on observe() call
      const entriesReceived = []
      
      function handleEntries(entries) {
        entries.forEach(entry => {
          // WRONG: Assuming callback only fires when visible
          // loadImage(entry.target) // Would load all images immediately!
          
          // RIGHT: Check isIntersecting first
          if (entry.isIntersecting) {
            entriesReceived.push(entry)
          }
        })
      }

      // Initial callback with non-intersecting elements
      const initialEntries = [
        { isIntersecting: false, target: 'img1' },
        { isIntersecting: false, target: 'img2' },
        { isIntersecting: true, target: 'img3' }
      ]

      handleEntries(initialEntries)
      expect(entriesReceived.length).toBe(1) // Only img3
    })
  })

  describe('Feature Detection', () => {
    // MDX lines ~775-790: Browser Support
    it('should demonstrate feature detection pattern', () => {
      // Simulate browser without IntersectionObserver
      const windowWithoutIO = {}
      const windowWithIO = { IntersectionObserver: function() {} }

      function hasIntersectionObserver(win) {
        return 'IntersectionObserver' in win
      }

      expect(hasIntersectionObserver(windowWithoutIO)).toBe(false)
      expect(hasIntersectionObserver(windowWithIO)).toBe(true)
    })
  })

  describe('Key Takeaways Validation', () => {
    // MDX lines ~800-830: Key Takeaways
    it('Takeaway 1: Observer is more performant than scroll events', () => {
      // Scroll events fire 60+ times per second
      // IntersectionObserver only fires when visibility changes
      const scrollEventFrequency = 60 // per second
      const observerCallsPerVisibilityChange = 1
      
      expect(scrollEventFrequency).toBeGreaterThan(observerCallsPerVisibilityChange)
    })

    it('Takeaway 4: One observer can watch many elements', () => {
      const observedElements = []
      const mockObserver = {
        observe: (el) => observedElements.push(el)
      }

      // Can observe multiple elements with single observer
      const elements = ['el1', 'el2', 'el3']
      elements.forEach(el => mockObserver.observe(el))

      expect(observedElements.length).toBe(3)
    })

    it('Takeaway 6: rootMargin enables preloading', () => {
      // Positive margin = detect before visible
      const preloadMargin = '100px'
      
      // Element at position 700px from top of viewport (800px height)
      // With rootMargin: 100px, detection happens at 800 + 100 = 900px
      const viewportHeight = 800
      const marginValue = 100
      const effectiveDetectionArea = viewportHeight + marginValue
      
      expect(effectiveDetectionArea).toBe(900)
    })
  })
})