|
| 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/feature-index-required.js' |
| 6 | + |
| 7 | +const mockFileSystem = (hasIndex = true, indexFilename = 'index.ts') => { |
| 8 | + // make existsSync return true for directories and conditionally for index files |
| 9 | + vi.spyOn(fs, 'existsSync').mockImplementation((p) => { |
| 10 | + const s = String(p) |
| 11 | + // simulate feature folder path containing '/features/auth' |
| 12 | + if (s.includes(`${path.sep}features${path.sep}auth`)) { |
| 13 | + if (s.endsWith(indexFilename)) { |
| 14 | + return hasIndex |
| 15 | + } |
| 16 | + return true |
| 17 | + } |
| 18 | + return false |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +describe('vue-modular/feature-index-required', () => { |
| 23 | + beforeEach(setupTest) |
| 24 | + |
| 25 | + it('reports when index.ts missing', () => { |
| 26 | + mockFileSystem(false) |
| 27 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 28 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 29 | + expect(ctx.report).toHaveBeenCalled() |
| 30 | + }) |
| 31 | + |
| 32 | + it('does not report when index.ts present', () => { |
| 33 | + mockFileSystem(true) |
| 34 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 35 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 36 | + expect(ctx.report).not.toHaveBeenCalled() |
| 37 | + }) |
| 38 | + |
| 39 | + it('runs only once per session', () => { |
| 40 | + mockFileSystem(false) |
| 41 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 42 | + const first = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 43 | + const second = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 44 | + |
| 45 | + expect(first.report).toHaveBeenCalled() |
| 46 | + expect(second.report).not.toHaveBeenCalled() |
| 47 | + }) |
| 48 | + |
| 49 | + it('does nothing for virtual filenames', () => { |
| 50 | + mockFileSystem(false) |
| 51 | + const ctx = runRule(rule, '<input>', [{ features: 'features', ignore: [] }]) |
| 52 | + expect(ctx.report).not.toHaveBeenCalled() |
| 53 | + }) |
| 54 | + |
| 55 | + it('returns early when featureName missing (path ends with features)', () => { |
| 56 | + mockFileSystem(false) |
| 57 | + const filename = path.join(process.cwd(), 'src', 'features') |
| 58 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 59 | + expect(ctx.report).not.toHaveBeenCalled() |
| 60 | + }) |
| 61 | + |
| 62 | + it('respects ignore option', () => { |
| 63 | + mockFileSystem(false) |
| 64 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 65 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: ['auth'] }]) |
| 66 | + expect(ctx.report).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it('does nothing when configured features folder is not in filename path', () => { |
| 70 | + mockFileSystem(false) |
| 71 | + const filename = path.join(process.cwd(), 'src', 'other', 'auth', 'components', 'LoginForm.vue') |
| 72 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [] }]) |
| 73 | + expect(ctx.report).not.toHaveBeenCalled() |
| 74 | + }) |
| 75 | + |
| 76 | + it('respects custom index filename option', () => { |
| 77 | + // simulate that 'main.ts' is present and default index.ts is missing |
| 78 | + mockFileSystem(true, 'main.ts') |
| 79 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 80 | + // configure the rule to expect 'main.ts' as the feature index |
| 81 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [], index: 'main.ts' }]) |
| 82 | + // when main.ts exists, there should be no report |
| 83 | + expect(ctx.report).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('reports when configured custom index is missing', () => { |
| 87 | + // simulate that 'main.ts' is missing |
| 88 | + mockFileSystem(false, 'main.ts') |
| 89 | + const filename = path.join(process.cwd(), 'src', 'features', 'auth', 'components', 'LoginForm.vue') |
| 90 | + // configure the rule to expect 'main.ts' as the feature index |
| 91 | + const ctx = runRule(rule, filename, [{ features: 'features', ignore: [], index: 'main.ts' }]) |
| 92 | + // when main.ts does not exist, the rule should report |
| 93 | + expect(ctx.report).toHaveBeenCalled() |
| 94 | + }) |
| 95 | +}) |
0 commit comments