|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | +import { runRule, setupTest } from '../test-utils' |
| 3 | +import { componentsIndexRequired } from '../../src/rules/components-index-required' |
| 4 | +import fs from 'fs' |
| 5 | + |
| 6 | +describe('components-index-required rule', () => { |
| 7 | + beforeEach(() => setupTest()) |
| 8 | + |
| 9 | + it('reports a missing index file for a components directory that has files but no index', () => { |
| 10 | + vi.spyOn(fs, 'readdirSync').mockReturnValue(['components-without-index', 'components-with-index'] as unknown as fs.Dirent< |
| 11 | + Buffer<ArrayBufferLike> |
| 12 | + >[]) |
| 13 | + vi.spyOn(fs, 'statSync').mockReturnValue({ isDirectory: () => true } as unknown as fs.Stats) |
| 14 | + vi.spyOn(fs, 'existsSync').mockImplementation((p) => String(p).includes('components-with-index')) |
| 15 | + |
| 16 | + const context = runRule(componentsIndexRequired, 'filename.ts') |
| 17 | + |
| 18 | + expect(context.report).toHaveBeenCalledTimes(1) |
| 19 | + expect(context.report).toHaveBeenCalledWith( |
| 20 | + expect.objectContaining({ |
| 21 | + messageId: 'missingIndex', |
| 22 | + data: expect.objectContaining({ indexPath: 'components-without-index/index.ts', index: 'index.ts' }), |
| 23 | + }), |
| 24 | + ) |
| 25 | + }) |
| 26 | + |
| 27 | + it('does not report when every components directory contains the expected index file', () => { |
| 28 | + vi.spyOn(fs, 'readdirSync').mockReturnValue(['components-a', 'components-b'] as unknown as fs.Dirent<Buffer<ArrayBufferLike>>[]) |
| 29 | + vi.spyOn(fs, 'statSync').mockReturnValue({ isDirectory: () => true } as unknown as fs.Stats) |
| 30 | + vi.spyOn(fs, 'existsSync').mockReturnValue(true) |
| 31 | + |
| 32 | + const context = runRule(componentsIndexRequired, 'filename.ts') |
| 33 | + |
| 34 | + expect(context.report).not.toHaveBeenCalled() |
| 35 | + }) |
| 36 | + |
| 37 | + it('respects ignore option and does not report for ignored components dirs', () => { |
| 38 | + vi.spyOn(fs, 'readdirSync').mockReturnValue(['ignored-components', 'normal-components'] as unknown as fs.Dirent< |
| 39 | + Buffer<ArrayBufferLike> |
| 40 | + >[]) |
| 41 | + vi.spyOn(fs, 'statSync').mockReturnValue({ isDirectory: () => true } as unknown as fs.Stats) |
| 42 | + vi.spyOn(fs, 'existsSync').mockReturnValue(false) |
| 43 | + |
| 44 | + const context = runRule(componentsIndexRequired, 'filename.ts', [{ ignores: ['ignored-components'], index: 'index.ts' }]) |
| 45 | + |
| 46 | + expect(context.report).toHaveBeenCalledTimes(1) |
| 47 | + expect(context.report).toHaveBeenCalledWith( |
| 48 | + expect.objectContaining({ |
| 49 | + messageId: 'missingIndex', |
| 50 | + data: expect.objectContaining({ indexPath: 'normal-components/index.ts', index: 'index.ts' }), |
| 51 | + }), |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + it('does nothing when runOnce returns false', () => { |
| 56 | + vi.spyOn(fs, 'readdirSync').mockReturnValue(['components-without-index'] as unknown as fs.Dirent<Buffer<ArrayBufferLike>>[]) |
| 57 | + vi.spyOn(fs, 'statSync').mockReturnValue({ isDirectory: () => true } as unknown as fs.Stats) |
| 58 | + vi.spyOn(fs, 'existsSync').mockReturnValue(false) |
| 59 | + |
| 60 | + const context1 = runRule(componentsIndexRequired, 'filename1.ts') |
| 61 | + const context2 = runRule(componentsIndexRequired, 'filename2.ts') |
| 62 | + |
| 63 | + expect(context1.report).toHaveBeenCalledTimes(1) |
| 64 | + expect(context2.report).not.toHaveBeenCalled() |
| 65 | + }) |
| 66 | + |
| 67 | + it('handles fs errors gracefully (does not throw)', () => { |
| 68 | + vi.spyOn(fs, 'readdirSync').mockImplementation(() => { |
| 69 | + throw new Error('boom') |
| 70 | + }) |
| 71 | + |
| 72 | + expect(() => { |
| 73 | + runRule(componentsIndexRequired, 'filename.ts') |
| 74 | + }).not.toThrow() |
| 75 | + }) |
| 76 | +}) |
0 commit comments