|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { describe, it, expect, beforeEach, vi } from 'vitest' |
| 4 | +import { setupTest, runRule } from '../utils.js' |
| 5 | +import rule from '../../src/rules/components-index-required.js' |
| 6 | + |
| 7 | +const mockFileSystem = (hasIndex = true, indexFilename = 'index.ts') => { |
| 8 | + vi.spyOn(fs, 'existsSync').mockImplementation((p) => { |
| 9 | + const s = String(p) |
| 10 | + // simulate components folder path containing '/features/<feature>/components' |
| 11 | + if ( |
| 12 | + s.includes(`${path.sep}features${path.sep}auth${path.sep}components`) || |
| 13 | + s.includes(`${path.sep}features${path.sep}shared${path.sep}components`) |
| 14 | + ) { |
| 15 | + if (s.endsWith(indexFilename)) { |
| 16 | + return hasIndex |
| 17 | + } |
| 18 | + return true |
| 19 | + } |
| 20 | + return false |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +describe('vue-modular/components-index-required', () => { |
| 25 | + beforeEach(setupTest) |
| 26 | + |
| 27 | + it('reports when index.ts missing for components folder', () => { |
| 28 | + mockFileSystem(false) |
| 29 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'Button.vue') |
| 30 | + const ctx = runRule(rule, filename, [{ components: 'components', ignore: [] }]) |
| 31 | + expect(ctx.report).toHaveBeenCalled() |
| 32 | + }) |
| 33 | + |
| 34 | + it('returns early when parentName missing (components at path start)', () => { |
| 35 | + mockFileSystem(false) |
| 36 | + // relative filename with components as the first segment -> parentName undefined |
| 37 | + const filename = path.join('components', 'Button.vue') |
| 38 | + const ctx = runRule(rule, filename, [{ components: 'components', ignore: [] }]) |
| 39 | + expect(ctx.report).not.toHaveBeenCalled() |
| 40 | + }) |
| 41 | + |
| 42 | + it('runs only once per session', () => { |
| 43 | + mockFileSystem(false) |
| 44 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'Button.vue') |
| 45 | + const first = runRule(rule, filename, [{ components: 'components', ignore: [] }]) |
| 46 | + const second = runRule(rule, filename, [{ components: 'components', ignore: [] }]) |
| 47 | + |
| 48 | + expect(first.report).toHaveBeenCalled() |
| 49 | + expect(second.report).not.toHaveBeenCalled() |
| 50 | + }) |
| 51 | + |
| 52 | + it('does not report when index.ts present for components folder', () => { |
| 53 | + mockFileSystem(true) |
| 54 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'Button.vue') |
| 55 | + const ctx = runRule(rule, filename, [{ components: 'components', ignore: [] }]) |
| 56 | + expect(ctx.report).not.toHaveBeenCalled() |
| 57 | + }) |
| 58 | + |
| 59 | + it('respects ignore option', () => { |
| 60 | + mockFileSystem(false) |
| 61 | + const filename = path.join(process.cwd(), 'src', 'features', 'shared', 'components', 'Icon.vue') |
| 62 | + const ctx = runRule(rule, filename, [{ components: 'components', ignore: ['shared'] }]) |
| 63 | + expect(ctx.report).not.toHaveBeenCalled() |
| 64 | + }) |
| 65 | + |
| 66 | + it('does nothing for virtual filenames', () => { |
| 67 | + mockFileSystem(false) |
| 68 | + const ctx = runRule(rule, '<input>', [{ components: 'components', ignore: [] }]) |
| 69 | + expect(ctx.report).not.toHaveBeenCalled() |
| 70 | + }) |
| 71 | + |
| 72 | + it('does nothing when configured components folder is not in filename path', () => { |
| 73 | + mockFileSystem(false) |
| 74 | + const filename = path.join(process.cwd(), 'src', 'other', 'auth', 'components', 'Button.vue') |
| 75 | + // configure the rule to look for a components segment that doesn't exist in the path |
| 76 | + const ctx = runRule(rule, filename, [{ components: 'no-components-here', ignore: [] }]) |
| 77 | + expect(ctx.report).not.toHaveBeenCalled() |
| 78 | + }) |
| 79 | + |
| 80 | + it('supports custom index filename', () => { |
| 81 | + mockFileSystem(true, 'main.ts') |
| 82 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'Button.vue') |
| 83 | + const ctx = runRule(rule, filename, [{ components: 'components', ignore: [], index: 'main.ts' }]) |
| 84 | + expect(ctx.report).not.toHaveBeenCalled() |
| 85 | + }) |
| 86 | +}) |
0 commit comments