|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { isNativeElement, ELEMENT_TAGS } = require('../../../lib/utils/is-native-element'); |
| 4 | + |
| 5 | +// Tests exercise the list-lookup path only. Scope-based shadowing is covered |
| 6 | +// by the rule-level test suites (see tests/lib/rules/template-no-block-params- |
| 7 | +// for-html-elements.js and siblings) because it requires a real ESLint |
| 8 | +// SourceCode / scope manager that's only built up by the rule tester. |
| 9 | + |
| 10 | +describe('isNativeElement — list-only behavior (no sourceCode)', () => { |
| 11 | + it('returns true for lowercase HTML tag names', () => { |
| 12 | + expect(isNativeElement({ tag: 'div' })).toBe(true); |
| 13 | + expect(isNativeElement({ tag: 'article' })).toBe(true); |
| 14 | + expect(isNativeElement({ tag: 'h1' })).toBe(true); |
| 15 | + expect(isNativeElement({ tag: 'button' })).toBe(true); |
| 16 | + expect(isNativeElement({ tag: 'form' })).toBe(true); |
| 17 | + expect(isNativeElement({ tag: 'section' })).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns true for SVG tag names', () => { |
| 21 | + expect(isNativeElement({ tag: 'svg' })).toBe(true); |
| 22 | + expect(isNativeElement({ tag: 'circle' })).toBe(true); |
| 23 | + expect(isNativeElement({ tag: 'path' })).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns true for MathML tag names', () => { |
| 27 | + expect(isNativeElement({ tag: 'mfrac' })).toBe(true); |
| 28 | + expect(isNativeElement({ tag: 'math' })).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns false for PascalCase component tags', () => { |
| 32 | + expect(isNativeElement({ tag: 'Button' })).toBe(false); |
| 33 | + expect(isNativeElement({ tag: 'MyWidget' })).toBe(false); |
| 34 | + // Native-tag names in PascalCase — the core bug case. |
| 35 | + expect(isNativeElement({ tag: 'Article' })).toBe(false); |
| 36 | + expect(isNativeElement({ tag: 'Form' })).toBe(false); |
| 37 | + expect(isNativeElement({ tag: 'Main' })).toBe(false); |
| 38 | + expect(isNativeElement({ tag: 'Nav' })).toBe(false); |
| 39 | + expect(isNativeElement({ tag: 'Section' })).toBe(false); |
| 40 | + expect(isNativeElement({ tag: 'Table' })).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns false for named-arg invocations', () => { |
| 44 | + expect(isNativeElement({ tag: '@heading' })).toBe(false); |
| 45 | + expect(isNativeElement({ tag: '@tag.foo' })).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns false for this-path invocations', () => { |
| 49 | + expect(isNativeElement({ tag: 'this.myComponent' })).toBe(false); |
| 50 | + expect(isNativeElement({ tag: 'this.comp.sub' })).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns false for dot-path invocations', () => { |
| 54 | + expect(isNativeElement({ tag: 'foo.bar' })).toBe(false); |
| 55 | + expect(isNativeElement({ tag: 'ns.widget' })).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns false for named-block / namespaced invocations', () => { |
| 59 | + expect(isNativeElement({ tag: 'foo::bar' })).toBe(false); |
| 60 | + expect(isNativeElement({ tag: 'Foo::Bar' })).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns false for custom elements (accepted false negative)', () => { |
| 64 | + // Custom elements aren't in the html-tags/svg-tags/mathml-tag-names |
| 65 | + // allowlists. They're treated as "not a native element" so downstream |
| 66 | + // rules skip them — matches the convention established in PR #2689. |
| 67 | + expect(isNativeElement({ tag: 'my-element' })).toBe(false); |
| 68 | + expect(isNativeElement({ tag: 'x-foo' })).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it('returns false for empty / missing / non-string tag', () => { |
| 72 | + expect(isNativeElement({ tag: '' })).toBe(false); |
| 73 | + expect(isNativeElement({ tag: undefined })).toBe(false); |
| 74 | + expect(isNativeElement({ tag: null })).toBe(false); |
| 75 | + expect(isNativeElement({ tag: 123 })).toBe(false); |
| 76 | + expect(isNativeElement({})).toBe(false); |
| 77 | + expect(isNativeElement()).toBe(false); |
| 78 | + expect(isNativeElement(null)).toBe(false); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('ELEMENT_TAGS', () => { |
| 83 | + it('includes all HTML, SVG, and MathML tag names', () => { |
| 84 | + // Sanity check — if this ever drops below a reasonable size, one of the |
| 85 | + // underlying packages has changed contract. |
| 86 | + expect(ELEMENT_TAGS.size).toBeGreaterThan(200); |
| 87 | + expect(ELEMENT_TAGS.has('div')).toBe(true); |
| 88 | + expect(ELEMENT_TAGS.has('circle')).toBe(true); |
| 89 | + expect(ELEMENT_TAGS.has('mfrac')).toBe(true); |
| 90 | + }); |
| 91 | +}); |
0 commit comments