import { appendElement } from '../../test' import { getSelectorFromElement, isSelectorUniqueAmongSiblings } from './getSelectorFromElement' describe('getSelectorFromElement', () => { afterEach(() => { document.body.classList.remove('foo') }) it('returns undefined for detached elements', () => { const element = document.createElement('div') expect(getSelector(element)).toBeUndefined() }) describe('ID selector', () => { it('should use the ID selector when the element as an ID', () => { expect(getSelector('
')).toBe('#foo') }) it('should not use the ID selector when the ID is not unique', () => { expect(getSelector('')).not.toContain('#foo') }) it('should not use generated IDs', () => { expect(getSelector('')).toBe('BODY>DIV') }) }) describe('class selector', () => { it('should use the class selector when the element as classes', () => { expect(getSelector('')).toBe('BODY>DIV.foo') }) it('should use the class selector when siblings have the same classes but different tags', () => { expect(getSelector('')).toBe('BODY>DIV.foo') }) it('should not use the class selector when siblings have the tag + classes', () => { expect(getSelector('')).not.toContain('DIV.foo') expect(getSelector('')).not.toContain('DIV.foo') }) it('should not use the class selector for body elements', () => { const element = appendElement('') document.body.classList.add('foo') expect(getSelector(element)).toBe('BODY>DIV') }) it('should not use generated classes', () => { expect(getSelector('')).toBe('BODY>DIV') }) it('uses only the first class', () => { expect(getSelector('')).toBe('BODY>DIV.foo') }) }) describe('position selector', () => { it('should use nth-of-type when the selector matches multiple descendants', () => { expect( getSelector(` `) ).toBe('BODY>DIV:nth-of-type(2)>BUTTON') }) it('should not use nth-of-type when the selector is matching a single descendant', () => { expect( getSelector(` `) ).toBe('BODY>DIV>BUTTON') }) it('should only consider direct descendants (>) of the parent element when checking for unicity', () => { expect( getSelector(`