| 1 |
import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 2 |
import { JSDOM } from 'jsdom'; |
| 3 |
|
| 4 |
import { getContextualAttr, isPromise, query, queryAll } from '../../src/utils.js'; |
| 5 |
|
| 6 |
const createDocument = (body: string): Document => { |
| 7 |
const dom = new JSDOM(/*html*/ `<!DOCTYPE html><body>${body}</body>`); |
| 8 |
return dom.window.document; |
| 9 |
}; |
| 10 |
|
| 11 |
const stubGlobalDocument = (body: string): Document => { |
| 12 |
vi.stubGlobal('document', createDocument(body)); |
| 13 |
return document; |
| 14 |
}; |
| 15 |
|
| 16 |
afterEach(() => { |
| 17 |
vi.unstubAllGlobals(); |
| 18 |
}); |
| 19 |
|
| 20 |
describe('query', () => { |
| 21 |
it('should call querySelector on document', () => { |
| 22 |
const doc = stubGlobalDocument(``); |
| 23 |
const spy = vi.spyOn(doc, 'querySelector'); |
| 24 |
query('.selector'); |
| 25 |
expect(spy).toHaveBeenCalledWith('.selector'); |
| 26 |
}); |
| 27 |
|
| 28 |
it('should return queried element', () => { |
| 29 |
stubGlobalDocument(`<div class="test">Test</div>`); |
| 30 |
expect(query('.test')?.textContent).toBe('Test'); |
| 31 |
}); |
| 32 |
|
| 33 |
it('should accept a custom root element', () => { |
| 34 |
const doc = stubGlobalDocument(/*html*/ ` |
| 35 |
<div class="sibling">Sibling</div> |
| 36 |
<div class="nested"> |
| 37 |
<div class="child">Child</div> |
| 38 |
</div> |
| 39 |
`); |
| 40 |
|
| 41 |
const root = doc.querySelector('.nested')!; |
| 42 |
expect(query('.sibling')).toBe(doc.querySelector('.sibling')); |
| 43 |
expect(query('.sibling', root)).toBe(null); |
| 44 |
expect(query('.child', root)).toBe(doc.querySelector('.child')); |
| 45 |
}); |
| 46 |
}); |
| 47 |
|
| 48 |
describe('queryAll', () => { |
| 49 |
it('should call querySelectorAll on document', () => { |
| 50 |
const doc = stubGlobalDocument(``); |
| 51 |
const spy = vi.spyOn(doc, 'querySelectorAll'); |
| 52 |
queryAll('.selector'); |
| 53 |
expect(spy).toHaveBeenCalledWith('.selector'); |
| 54 |
}); |
| 55 |
|
| 56 |
it('returns an array', () => { |
| 57 |
const els = queryAll('.lorem-ipsum'); |
| 58 |
expect(Array.isArray(els)).toBe(true); |
| 59 |
}); |
| 60 |
|
| 61 |
it('should return queried elements', () => { |
| 62 |
stubGlobalDocument(`<div class="test">Test 1</div><div class="test">Test 2</div>`); |
| 63 |
expect(queryAll('.test').map(el => el.textContent)).toStrictEqual(['Test 1', 'Test 2']); |
| 64 |
}); |
| 65 |
|
| 66 |
it('should accept a custom root element', () => { |
| 67 |
const doc = stubGlobalDocument(/*html*/ ` |
| 68 |
<div class="sibling">Sibling</div> |
| 69 |
<div class="nested"> |
| 70 |
<div class="child">Child</div> |
| 71 |
<div class="child">Child</div> |
| 72 |
</div> |
| 73 |
`); |
| 74 |
|
| 75 |
const root = doc.querySelector('.nested')!; |
| 76 |
expect(queryAll('.sibling')).toStrictEqual([...doc.querySelectorAll('.sibling')]); |
| 77 |
expect(queryAll('.sibling', root)).toStrictEqual([]); |
| 78 |
expect(queryAll('.child', root)).toStrictEqual([...doc.querySelectorAll('.child')]); |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
describe('isPromise', () => { |
| 83 |
it('accepts promises', () => { |
| 84 |
expect(isPromise(Promise.resolve())).toBe(true); |
| 85 |
expect(isPromise(new Promise(() => {}))).toBe(true); |
| 86 |
}); |
| 87 |
|
| 88 |
it('accepts Promise-like objects', () => { |
| 89 |
expect(isPromise({ then: () => {} })).toBe(true); |
| 90 |
expect(isPromise({ then: () => {}, catch: () => {} })).toBe(true); |
| 91 |
}); |
| 92 |
|
| 93 |
it('rejects non-promises', () => { |
| 94 |
expect(isPromise(0)).toBe(false); |
| 95 |
expect(isPromise('a')).toBe(false); |
| 96 |
expect(isPromise([1, 2, 3])).toBe(false); |
| 97 |
expect(isPromise({ b: 'c' })).toBe(false); |
| 98 |
}); |
| 99 |
}); |
| 100 |
|
| 101 |
describe('getContextualAttr', () => { |
| 102 |
it('gets attribute value from element', () => { |
| 103 |
const doc = stubGlobalDocument(/*html*/ `<div attr="value"></div>`); |
| 104 |
const el = doc.querySelector('div')!; |
| 105 |
expect(getContextualAttr(el, 'attr')).toBe('value'); |
| 106 |
}); |
| 107 |
|
| 108 |
it('returns true for attrs without value', () => { |
| 109 |
const doc = stubGlobalDocument(/*html*/ `<div disabled></div>`); |
| 110 |
const el = doc.querySelector('div')!; |
| 111 |
expect(getContextualAttr(el, 'disabled')).toBe(true); |
| 112 |
}); |
| 113 |
|
| 114 |
it('gets attribute value from parent', () => { |
| 115 |
const doc = stubGlobalDocument(/*html*/ `<section attr="value"><div></div></section>`); |
| 116 |
const el = doc.querySelector('div')!; |
| 117 |
expect(getContextualAttr(el, 'attr')).toBe('value'); |
| 118 |
}); |
| 119 |
|
| 120 |
it('prefers attribute value from element', () => { |
| 121 |
const doc = stubGlobalDocument(/*html*/ `<section attr="parent"><div attr="self"></div></section>`); |
| 122 |
const el = doc.querySelector('div')!; |
| 123 |
expect(getContextualAttr(el, 'attr')).toBe('self'); |
| 124 |
}); |
| 125 |
|
| 126 |
it('returns null for missing attribute', () => { |
| 127 |
const doc = stubGlobalDocument(/*html*/ `<div></div>`); |
| 128 |
const el = doc.querySelector('div')!; |
| 129 |
expect(getContextualAttr(el, 'attr')).toBe(undefined); |
| 130 |
}); |
| 131 |
|
| 132 |
it('returns null for missing element', () => { |
| 133 |
const doc = stubGlobalDocument(/*html*/ `<div></div>`); |
| 134 |
const el = doc.querySelector('h1')!; |
| 135 |
expect(getContextualAttr(el, 'attr')).toBe(undefined); |
| 136 |
}); |
| 137 |
}); |