Last active
March 2, 2025 13:05
-
-
Save tzmfreedom/05bc2bf320e278079b803ded39864889 to your computer and use it in GitHub Desktop.
Vitest Example: Alpine.js with JSDOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect, test } from 'vitest' | |
import { Alpine } from 'alpinejs'; | |
import { fireEvent, screen } from "@testing-library/dom"; | |
test('testing alpinejs', () => { | |
document.body.innerHTML = `<div x-data="{ open: true }"> | |
<button data-testid="button" @click.prevent="open = !open;">Expand</button> | |
<span data-testid="content" x-show="open"> | |
Content... | |
</span> | |
</div>`; | |
Alpine.initTree(document.body); | |
await fireEvent.click(screen.getByTestId('button')); | |
await new Promise((resolve) => setTimeout(resolve, 30)); | |
expect(screen.getByTestId("content").style.display).toBe('none'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment