agentleFS
garthcodesgarthcodes/claude-skills/skills/component-test/SKILL.md

component-test

Skill2 starsChanged 2 months agoMITView on GitHub

Generate RSpec tests for a ViewComponent following the application's component-test patterns

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

31 rules. About 0 are common, 25 are distinctive, and 6 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈25

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 6

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Test component inheritance from ApplicationComponent

    line 18
  2. Distinctive:

    Test initialization with required/optional parameters

    line 19
  3. Distinctive:

    Test parameter validation and error cases

    line 20
  4. Distinctive:

    Test basic rendering with render_inline

    line 21
  5. Distinctive:

    Test all slot configurations (renders_one, renders_many)

    line 22
  6. Distinctive:

    Test conditional rendering (states, modes, visibility)

    line 23
  7. Very specific:

    Test theme colors and styling (using theme colors from app/assets/tailwind/application.css)

    line 24
  8. Distinctive:

    Test accessibility attributes (ARIA, semantic HTML)

    line 25
  9. Distinctive:

    Test interactive elements (links, buttons, forms)

    line 26
  10. Distinctive:

    Test empty states and edge cases

    line 27
  11. Distinctive:

    Test helper methods (public and private through public interface)

    line 28
  12. Distinctive:

    Test nested/child components

    line 29
  13. Very specific:

    Use build instead of create when database persistence is not required

    line 32
  14. Very specific:

    Use let for lazy evaluation, let!

    line 33
  15. Distinctive:

    Avoid unnecessary database hits

    line 34
  16. Distinctive:

    Use deterministic data with sequences, not random values

    line 35
  17. Distinctive:

    Test rendered output, not implementation details

    line 36
  18. Very specific:

    When the user provides a component file path as $ARGUMENTS, follow this iterative workflow until ALL tests pass:

    line 152
  19. Distinctive:

    If ALL tests pass → Report success and exit

    line 188
  20. Distinctive:

    If ANY tests fail → Continue to step 3

    line 189
  21. Distinctive:

    Read and understand the error message

    line 192
  22. Distinctive:

    Update factory if needed

    line 203
  23. Distinctive:

    Update test setup (add missing let!

    line 205
  24. Distinctive:

    Factory validation failures: Ensure factory has all required attributes

    line 245
  25. Very specific:

    Association errors: Create associated records with let!

    line 246
  26. Distinctive:

    CSS selector mismatches: Verify selectors match actual rendered HTML

    line 247
  27. Distinctive:

    Missing slots: Test all slot configurations including when empty

    line 248
  28. Very specific:

    Multi-tenant errors: Always set ActsAsTenant.current_tenant

    line 249
  29. Distinctive:

    Conditional rendering: Test all branches of conditional logic

    line 250
  30. Distinctive:

    Accessibility assertions: Verify ARIA attributes exist and have correct values

    line 251
  31. Distinctive:

    Theme color assertions: Use exact class names from theme (bg-primary, text-text-light, etc.)

    line 252

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

ViewComponent Test Generator Command

You are an expert Rails ViewComponent test developer. Write comprehensive, fast, and maintainable RSpec component tests for the application following established patterns and best practices.

Reference Skill

For detailed patterns, examples, and testing conventions, consult the skill file at: .claude/skills/viewcomponent-test-expert/SKILL.md

Core Principles

1. Comprehensive Coverage

  • Test component inheritance from ApplicationComponent
  • Test initialization with required/optional parameters
  • Test parameter validation and error cases
  • Test basic rendering with render_inline
  • Test all slot configurations (renders_one, renders_many)
  • Test conditional rendering (states, modes, visibility)
  • Test theme colors and styling (using theme colors from app/assets/tailwind/application.css)
  • Test accessibility attributes (ARIA, semantic HTML)
  • Test interactive elements (links, buttons, forms)
  • Test empty states and edge cases
  • Test helper methods (public and private through public interface)
  • Test nested/child components

2. Write Fast, Efficient Tests

  • Use build instead of create when database persistence is not required
  • Use let for lazy evaluation, let! for immediate evaluation
  • Avoid unnecessary database hits
  • Use deterministic data with sequences, not random values
  • Test rendered output, not implementation details

3. Follow Established Patterns

Reference these component test files for patterns:

  • spec/components/ directory for existing component tests
  • .claude/skills/viewcomponent-test-expert/SKILL.md for comprehensive testing guide

Test File Structure

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ComponentName, type: :component do
  let(:organization) { create(:organization) }
  let(:model) { create(:model_name, organization: organization) }

  before do
    ActsAsTenant.current_tenant = organization
  end

  describe 'inheritance' do
    it 'inherits from ApplicationComponent' do
      expect(described_class.superclass).to eq(ApplicationComponent)
    end
  end

  describe 'initialization' do
    # Test required and optional parameters
  end

  describe 'rendering' do
    # Test basic rendering with render_inline
  end

  describe 'content rendering' do
    # Test specific content sections
  end

  describe 'slots' do
    # Test renders_one and renders_many slots
  end

  describe 'styling' do
    # Test theme colors and CSS classes
  end

  describe 'accessibility' do
    # Test ARIA attributes and semantic HTML
  end

  describe 'helper methods' do
    # Test public helper methods
  end

  describe 'edge cases' do
    # Test nil, empty, special characters
  end
end

Key Testing Patterns

Rendering with render_inline

rendered = render_inline(described_class.new(model: model))
expect(rendered.text).to include('Expected Text')
expect(rendered.css('.class-name')).to be_present
expect(rendered.to_html).to include('data-attribute')

Testing Slots

rendered = render_inline(described_class.new) do |component|
  component.with_header { 'Header Content' }
  component.with_body { 'Body Content' }
end
expect(rendered.css('.header').text).to eq('Header Content')

Testing Conditional Rendering

context 'when condition is true' do
  it 'shows element' do
    rendered = render_inline(described_class.new(show: true))
    expect(rendered.css('.element')).to be_present
  end
end

context 'when condition is false' do
  it 'hides element' do
    rendered = render_inline(described_class.new(show: false))
    expect(rendered.css('.element')).not_to be_present
  end
end

Testing Accessibility

describe 'accessibility' do
  it 'includes aria-label on interactive elements' do
    rendered = render_inline(described_class.new(model: model))
    button = rendered.css('button').first
    expect(button['aria-label']).to be_present
  end

  it 'marks decorative elements as aria-hidden' do
    rendered = render_inline(described_class.new(model: model))
    expect(rendered.css('[aria-hidden="true"]')).to be_present
  end
end

Instructions for AI

When the user provides a component file path as $ARGUMENTS, follow this iterative workflow until ALL tests pass:

Phase 1: Analysis and Test Generation

  1. Read the component file at $ARGUMENTS
  2. Determine test file path: Convert component path to spec path
    • app/components/foo_component.rb → spec/components/foo_component_spec.rb
    • app/components/foo/bar_component.rb → spec/components/foo/bar_component_spec.rb
  3. Check if spec file exists at the determined path
  4. Analyze the component to identify:
    • Class inheritance (should be ApplicationComponent)
    • Initialize parameters (required, optional, defaults)
    • Slots (renders_one, renders_many)
    • Helper methods (public and private)
    • Conditional rendering logic
    • Theme colors and styling
    • Accessibility attributes
    • Nested/child components
  5. If test file exists, read it and identify:
    • What is already tested
    • What is missing
    • What needs improvement
  6. Generate or update tests following the skill guide at .claude/skills/viewcomponent-test-expert/SKILL.md
  7. Ensure comprehensive coverage of all component behavior
  8. Use factories defined in spec/factories/ or suggest new ones

Phase 2: Iterative Test Execution and Fixes

CRITICAL: Do not stop until all tests pass. Repeat this loop until success:

  1. Run the tests for the specific component:
    bundle exec rspec spec/components/component_name_spec.rb
  2. Analyze test results:
    • If ALL tests pass → Report success and exit
    • If ANY tests fail → Continue to step 3
  3. For each test failure:
    • Read and understand the error message
    • Identify the root cause:
      • Missing factory attributes
      • Incorrect test expectations
      • Missing slot configurations
      • Incorrect CSS selectors
      • Missing multi-tenant setup
      • Missing associated records
      • Incorrect render_inline usage
  4. Fix the failure:
    • Update factory if needed
    • Fix test expectations if they're incorrect
    • Update test setup (add missing let! blocks, create associations)
    • Adjust CSS selectors to match actual component output
    • DO NOT modify the component unless there's a genuine bug
  5. Return to step 1 and run tests again

Phase 3: Final Verification

Once all tests pass:

  1. Run the full test suite one more time to confirm
  2. Provide a summary including:
    • Total number of examples and failures (should be 0 failures)
    • What was fixed during the iteration
    • Final test coverage breakdown
    • Any recommendations for the component or tests

Test Organization Checklist

For every component test file, verify:

  • require 'rails_helper' at top
  • type: :component metadata
  • Multi-tenant setup with ActsAsTenant.current_tenant
  • Test inheritance from ApplicationComponent
  • Test initialization with required/optional parameters
  • Test parameter validation and error cases
  • Test basic rendering
  • Test all slot configurations
  • Test conditional rendering (states, modes)
  • Test theme colors and styling
  • Test accessibility attributes
  • Test interactive elements (links, buttons)
  • Test empty states
  • Test edge cases (nil, empty, special chars)
  • Test helper methods
  • Test nested components if applicable

Common Failure Patterns and Solutions

  1. Factory validation failures: Ensure factory has all required attributes
  2. Association errors: Create associated records with let! or in factory
  3. CSS selector mismatches: Verify selectors match actual rendered HTML
  4. Missing slots: Test all slot configurations including when empty
  5. Multi-tenant errors: Always set ActsAsTenant.current_tenant
  6. Conditional rendering: Test all branches of conditional logic
  7. Accessibility assertions: Verify ARIA attributes exist and have correct values
  8. Theme color assertions: Use exact class names from theme (bg-primary, text-text-light, etc.)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.