Skip to content

Test Suite: Accordion.svelte#162

Open
sm17p wants to merge 2 commits intomelt-ui:mainfrom
sm17p:tests-accordion
Open

Test Suite: Accordion.svelte#162
sm17p wants to merge 2 commits intomelt-ui:mainfrom
sm17p:tests-accordion

Conversation

@sm17p
Copy link
Copy Markdown
Contributor

@sm17p sm17p commented Jul 1, 2025

Add Comprehensive Accordion Component Tests

Overview

This PR introduces a complete test suite for the Accordion component, covering both unit tests and browser-based integration tests.

What's Changed

🧪 Test Coverage Added

  • Constructor & Initialization: Tests default and custom prop handling, including getter functions
  • State Management: Comprehensive coverage of expand/collapse operations in both single and multiple modes
  • Accessibility: ARIA attributes, keyboard navigation, and screen reader compatibility
  • User Interactions: Click handling, keyboard shortcuts, and focus management
  • Disabled States: Both item-level and accordion-level disabled functionality

🏗️ Test Architecture

The test suite is organized into logical sections:

  1. Unit Tests (Accordion class)

    • Constructor validation
    • State management methods
    • Attribute generation
    • Callback handling
  2. Component Tests (AccordionItem class)

    • Item-specific state management
    • ARIA attribute correctness
    • Event handler functionality
  3. Integration Tests (Browser-based)

    • Real user interactions
    • Keyboard navigation
    • Focus management
    • Accessibility compliance

🔧 Key Features Tested

State Management

  • Single Selection Mode: Only one item can be expanded at a time
  • Multiple Selection Mode: Multiple items can be expanded simultaneously
  • Toggle Behavior: Proper expand/collapse state transitions

Keyboard Navigation

  • Space/Enter: Toggle expansion
  • Home: Focus first item
  • End: Focus last item
  • Arrow Up/Down: Navigate between items

🎯 Browser Testing

  • Real DOM Interactions: Tests actual click and keyboard events
  • Focus Management: Validates proper focus handling during navigation
  • State Synchronization: Ensures UI state matches component state
  • Multiple Browser Support: Tests run in both Chromium and Firefox

🛡️ Edge Cases Covered

  • Disabled States: Both accordion-level and item-level disabling
  • Getter Functions: Dynamic prop evaluation
  • State Persistence: Proper cleanup and initialization
  • Event Propagation: Correct handling of user events

Implementation Details

Test Structure

describe("Accordion", () => {
  // Core functionality tests
  describe("State Management", () => {
    // Svelte 5 $effect.root for proper cleanup
    let cleanupEffect: () => void;
    
    beforeEach(() => {
      cleanupEffect = $effect.root(() => {
        accordion = new Accordion();
      });
    });
    
    afterEach(() => {
      cleanupEffect();
    });
  });
});

Browser Test Integration

describe("Accordion User Interactions (Browser)", () => {
  testWithEffect("should handle click interactions", async () => {
    const { container } = render(AccordionTest, { items });
    
    const buttons = page.getByRole("button");
    const allButtons = await buttons.all();
    
    // Test real user interactions
    await allButtons[0]?.click();
    expect(allButtons[0]).toHaveAttribute("data-state", "open");
  });
});

Quality Assurance

  • ✅ All tests pass in both Chromium and Firefox
  • ✅ Real DOM interactions tested
  • ✅ Edge cases covered (disabled states, focus management)
  • ✅ Accessibility compliance verified

Future Considerations

  • Had problems setting up axe-core accessibility automated testing, will add that later in another PR

Breaking Changes

None.

@vercel
Copy link
Copy Markdown

vercel bot commented Jul 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-gen ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 4, 2025 11:05am

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jul 1, 2025

🦋 Changeset detected

Latest commit: 6f8cdde

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
melt Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@sm17p
Copy link
Copy Markdown
Contributor Author

sm17p commented Jul 2, 2025

I'm happy with my changes here

Copy link
Copy Markdown
Member

@TGlide TGlide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Just 2 small changes needed

Comment thread .changeset/curly-owls-draw.md Outdated
"melt": patch
---

test suite Accordion
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for changesets here, these are just for version bumps. since the source code did not change, and the tests dont get shipped, its okay to leave it :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually you did change something, so lets just change the text

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, completely missed that. Thank you for pointing it out, I'll update the text over here and push a new commit

Comment thread packages/melt/package.json Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants