2019-10-02 11:43:54 +02:00
|
|
|
import SelectorEngine from '../../../src/dom/selector-engine'
|
|
|
|
import { getFixture, clearFixture } from '../../helpers/fixture'
|
2019-03-24 18:30:30 +01:00
|
|
|
|
|
|
|
describe('SelectorEngine', () => {
|
|
|
|
let fixtureEl
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fixtureEl = getFixture()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clearFixture()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('find', () => {
|
|
|
|
it('should find elements', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(SelectorEngine.find('div', fixtureEl)).toEqual([div])
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should find elements globaly', () => {
|
|
|
|
fixtureEl.innerHTML = '<div id="test"></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('#test')
|
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(SelectorEngine.find('#test')).toEqual([div])
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should handle :scope selectors', () => {
|
2021-12-09 15:01:29 +01:00
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<ul>',
|
|
|
|
' <li></li>',
|
|
|
|
' <li>',
|
|
|
|
' <a href="#" class="active">link</a>',
|
|
|
|
' </li>',
|
|
|
|
' <li></li>',
|
|
|
|
'</ul>'
|
|
|
|
].join('')
|
2019-03-24 18:30:30 +01:00
|
|
|
|
|
|
|
const listEl = fixtureEl.querySelector('ul')
|
|
|
|
const aActive = fixtureEl.querySelector('.active')
|
|
|
|
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(SelectorEngine.find(':scope > li > .active', listEl)).toEqual([aActive])
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('findOne', () => {
|
|
|
|
it('should return one element', () => {
|
|
|
|
fixtureEl.innerHTML = '<div id="test"></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('#test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.findOne('#test')).toEqual(div)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('children', () => {
|
|
|
|
it('should find children', () => {
|
2021-12-09 15:01:29 +01:00
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<ul>',
|
|
|
|
' <li></li>',
|
|
|
|
' <li></li>',
|
|
|
|
' <li></li>',
|
|
|
|
'</ul>'
|
|
|
|
].join('')
|
2019-03-24 18:30:30 +01:00
|
|
|
|
|
|
|
const list = fixtureEl.querySelector('ul')
|
2020-03-25 15:35:02 +01:00
|
|
|
const liList = [].concat(...fixtureEl.querySelectorAll('li'))
|
|
|
|
const result = SelectorEngine.children(list, 'li')
|
2019-03-24 18:30:30 +01:00
|
|
|
|
|
|
|
expect(result).toEqual(liList)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('parents', () => {
|
|
|
|
it('should return parents', () => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(SelectorEngine.parents(fixtureEl, 'body')).toHaveSize(1)
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('prev', () => {
|
|
|
|
it('should return previous element', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="test"></div><button class="btn"></button>'
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelector('.test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.prev(btn, '.test')).toEqual([divTest])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return previous element with an extra element between', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="test"></div>',
|
|
|
|
'<span></span>',
|
|
|
|
'<button class="btn"></button>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelector('.test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.prev(btn, '.test')).toEqual([divTest])
|
|
|
|
})
|
2020-03-09 16:21:04 +01:00
|
|
|
|
|
|
|
it('should return previous element with comments or text nodes between', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="test"></div>',
|
|
|
|
'<div class="test"></div>',
|
|
|
|
'<!-- Comment-->',
|
|
|
|
'Text',
|
|
|
|
'<button class="btn"></button>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelectorAll('.test')[1]
|
|
|
|
|
|
|
|
expect(SelectorEngine.prev(btn, '.test')).toEqual([divTest])
|
|
|
|
})
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
2020-02-01 14:56:20 +01:00
|
|
|
|
|
|
|
describe('next', () => {
|
|
|
|
it('should return next element', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="test"></div><button class="btn"></button>'
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelector('.test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return next element with an extra element between', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="test"></div>',
|
|
|
|
'<span></span>',
|
|
|
|
'<button class="btn"></button>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelector('.test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return next element with comments or text nodes between', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="test"></div>',
|
|
|
|
'<!-- Comment-->',
|
|
|
|
'Text',
|
|
|
|
'<button class="btn"></button>',
|
|
|
|
'<button class="btn"></button>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const btn = fixtureEl.querySelector('.btn')
|
|
|
|
const divTest = fixtureEl.querySelector('.test')
|
|
|
|
|
|
|
|
expect(SelectorEngine.next(divTest, '.btn')).toEqual([btn])
|
|
|
|
})
|
|
|
|
})
|
2021-07-27 07:01:04 +02:00
|
|
|
|
|
|
|
describe('focusableChildren', () => {
|
|
|
|
it('should return only elements with specific tag names', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div>lorem</div>',
|
|
|
|
'<span>lorem</span>',
|
|
|
|
'<a>lorem</a>',
|
|
|
|
'<button>lorem</button>',
|
|
|
|
'<input />',
|
|
|
|
'<textarea></textarea>',
|
|
|
|
'<select></select>',
|
|
|
|
'<details>lorem</details>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const expectedElements = [
|
|
|
|
fixtureEl.querySelector('a'),
|
|
|
|
fixtureEl.querySelector('button'),
|
|
|
|
fixtureEl.querySelector('input'),
|
|
|
|
fixtureEl.querySelector('textarea'),
|
|
|
|
fixtureEl.querySelector('select'),
|
|
|
|
fixtureEl.querySelector('details')
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return any element with non negative tab index', () => {
|
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div tabindex>lorem</div>',
|
|
|
|
'<div tabindex="0">lorem</div>',
|
|
|
|
'<div tabindex="10">lorem</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const expectedElements = [
|
|
|
|
fixtureEl.querySelector('[tabindex]'),
|
|
|
|
fixtureEl.querySelector('[tabindex="0"]'),
|
|
|
|
fixtureEl.querySelector('[tabindex="10"]')
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return not return elements with negative tab index', () => {
|
2021-11-26 08:16:59 +01:00
|
|
|
fixtureEl.innerHTML = '<button tabindex="-1">lorem</button>'
|
2021-07-27 07:01:04 +02:00
|
|
|
|
|
|
|
const expectedElements = []
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return contenteditable elements', () => {
|
2021-11-26 08:16:59 +01:00
|
|
|
fixtureEl.innerHTML = '<div contenteditable="true">lorem</div>'
|
2021-07-27 07:01:04 +02:00
|
|
|
|
|
|
|
const expectedElements = [fixtureEl.querySelector('[contenteditable="true"]')]
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not return disabled elements', () => {
|
2021-11-26 08:16:59 +01:00
|
|
|
fixtureEl.innerHTML = '<button disabled="true">lorem</button>'
|
2021-07-27 07:01:04 +02:00
|
|
|
|
|
|
|
const expectedElements = []
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not return invisible elements', () => {
|
2021-11-26 08:16:59 +01:00
|
|
|
fixtureEl.innerHTML = '<button style="display:none;">lorem</button>'
|
2021-07-27 07:01:04 +02:00
|
|
|
|
|
|
|
const expectedElements = []
|
|
|
|
|
|
|
|
expect(SelectorEngine.focusableChildren(fixtureEl)).toEqual(expectedElements)
|
|
|
|
})
|
|
|
|
})
|
2019-03-24 18:30:30 +01:00
|
|
|
})
|
|
|
|
|