235 lines
5.9 KiB
TypeScript
Executable file
235 lines
5.9 KiB
TypeScript
Executable file
/**
|
|
* @vitest-environment jsdom
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import {
|
|
mockLocalStorage,
|
|
mockSessionStorage,
|
|
mockMatchMedia,
|
|
mockIntersectionObserver,
|
|
mockResizeObserver,
|
|
mockScrollTo,
|
|
mockBroadcastChannel,
|
|
} from '../mocks/browser'
|
|
|
|
describe('Browser Mocks', () => {
|
|
describe('mockLocalStorage', () => {
|
|
beforeEach(() => {
|
|
mockLocalStorage()
|
|
})
|
|
|
|
it('should mock localStorage.setItem', () => {
|
|
localStorage.setItem('key', 'value')
|
|
expect(localStorage.getItem('key')).toBe('value')
|
|
})
|
|
|
|
it('should mock localStorage.removeItem', () => {
|
|
localStorage.setItem('key', 'value')
|
|
localStorage.removeItem('key')
|
|
expect(localStorage.getItem('key')).toBeNull()
|
|
})
|
|
|
|
it('should mock localStorage.clear', () => {
|
|
localStorage.setItem('key1', 'value1')
|
|
localStorage.setItem('key2', 'value2')
|
|
localStorage.clear()
|
|
expect(localStorage.length).toBe(0)
|
|
})
|
|
|
|
it('should track length correctly', () => {
|
|
expect(localStorage.length).toBe(0)
|
|
localStorage.setItem('key', 'value')
|
|
expect(localStorage.length).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('mockSessionStorage', () => {
|
|
beforeEach(() => {
|
|
mockSessionStorage()
|
|
})
|
|
|
|
it('should mock sessionStorage.setItem', () => {
|
|
sessionStorage.setItem('key', 'value')
|
|
expect(sessionStorage.getItem('key')).toBe('value')
|
|
})
|
|
|
|
it('should be independent from localStorage', () => {
|
|
mockLocalStorage()
|
|
localStorage.setItem('key', 'local')
|
|
sessionStorage.setItem('key', 'session')
|
|
|
|
expect(localStorage.getItem('key')).toBe('local')
|
|
expect(sessionStorage.getItem('key')).toBe('session')
|
|
})
|
|
})
|
|
|
|
describe('mockMatchMedia', () => {
|
|
beforeEach(() => {
|
|
mockMatchMedia()
|
|
})
|
|
|
|
it('should mock window.matchMedia', () => {
|
|
const mql = window.matchMedia('(min-width: 768px)')
|
|
expect(mql).toBeDefined()
|
|
expect(mql.matches).toBe(false)
|
|
expect(mql.media).toBe('(min-width: 768px)')
|
|
})
|
|
|
|
it('should support addEventListener', () => {
|
|
const mql = window.matchMedia('(min-width: 768px)')
|
|
const listener = vi.fn()
|
|
|
|
mql.addEventListener('change', listener)
|
|
|
|
expect(listener).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should support removeEventListener', () => {
|
|
const mql = window.matchMedia('(min-width: 768px)')
|
|
const listener = vi.fn()
|
|
|
|
mql.addEventListener('change', listener)
|
|
mql.removeEventListener('change', listener)
|
|
|
|
// No error should be thrown
|
|
expect(true).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('mockIntersectionObserver', () => {
|
|
beforeEach(() => {
|
|
mockIntersectionObserver()
|
|
})
|
|
|
|
it('should mock IntersectionObserver constructor', () => {
|
|
const callback = vi.fn()
|
|
const observer = new IntersectionObserver(callback)
|
|
|
|
expect(observer).toBeDefined()
|
|
expect(observer.observe).toBeDefined()
|
|
expect(observer.unobserve).toBeDefined()
|
|
expect(observer.disconnect).toBeDefined()
|
|
})
|
|
|
|
it('should support observe method', () => {
|
|
const callback = vi.fn()
|
|
const observer = new IntersectionObserver(callback)
|
|
const element = document.createElement('div')
|
|
|
|
observer.observe(element)
|
|
|
|
// No error should be thrown
|
|
expect(true).toBe(true)
|
|
})
|
|
|
|
it('should support unobserve method', () => {
|
|
const callback = vi.fn()
|
|
const observer = new IntersectionObserver(callback)
|
|
const element = document.createElement('div')
|
|
|
|
observer.observe(element)
|
|
observer.unobserve(element)
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
|
|
it('should support disconnect method', () => {
|
|
const callback = vi.fn()
|
|
const observer = new IntersectionObserver(callback)
|
|
|
|
observer.disconnect()
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('mockResizeObserver', () => {
|
|
beforeEach(() => {
|
|
mockResizeObserver()
|
|
})
|
|
|
|
it('should mock ResizeObserver constructor', () => {
|
|
const callback = vi.fn()
|
|
const observer = new ResizeObserver(callback)
|
|
|
|
expect(observer).toBeDefined()
|
|
expect(observer.observe).toBeDefined()
|
|
expect(observer.unobserve).toBeDefined()
|
|
expect(observer.disconnect).toBeDefined()
|
|
})
|
|
|
|
it('should support observe with options', () => {
|
|
const callback = vi.fn()
|
|
const observer = new ResizeObserver(callback)
|
|
const element = document.createElement('div')
|
|
|
|
observer.observe(element, { box: 'border-box' })
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('mockScrollTo', () => {
|
|
beforeEach(() => {
|
|
mockScrollTo()
|
|
})
|
|
|
|
it('should mock window.scrollTo with x, y', () => {
|
|
window.scrollTo(0, 100)
|
|
|
|
// No error should be thrown
|
|
expect(true).toBe(true)
|
|
})
|
|
|
|
it('should mock window.scrollTo with options', () => {
|
|
window.scrollTo({
|
|
top: 100,
|
|
left: 0,
|
|
behavior: 'smooth',
|
|
})
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('mockBroadcastChannel', () => {
|
|
beforeEach(() => {
|
|
mockBroadcastChannel()
|
|
})
|
|
|
|
it('should mock BroadcastChannel constructor', () => {
|
|
const channel = new BroadcastChannel('test-channel')
|
|
|
|
expect(channel).toBeDefined()
|
|
expect(channel.name).toBe('test-channel')
|
|
expect(channel.postMessage).toBeDefined()
|
|
expect(channel.close).toBeDefined()
|
|
})
|
|
|
|
it('should support postMessage', () => {
|
|
const channel = new BroadcastChannel('test-channel')
|
|
|
|
channel.postMessage({ type: 'test', data: 'hello' })
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
|
|
it('should support addEventListener', () => {
|
|
const channel = new BroadcastChannel('test-channel')
|
|
const listener = vi.fn()
|
|
|
|
channel.addEventListener('message', listener)
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
|
|
it('should support close', () => {
|
|
const channel = new BroadcastChannel('test-channel')
|
|
|
|
channel.close()
|
|
|
|
expect(true).toBe(true)
|
|
})
|
|
})
|
|
})
|