|
| 1 | +/* |
| 2 | + * This file is part of the Symfony package. |
| 3 | + * |
| 4 | + * (c) Fabien Potencier <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { Application } from '@hotwired/stimulus'; |
| 11 | +import { getByTestId, waitFor } from '@testing-library/dom'; |
| 12 | +import { clearDOM, mountDOM } from '@symfony/stimulus-testing'; |
| 13 | +import SvelteController from '../src/render_controller'; |
| 14 | +import MyComponentSvelte5 from './fixtures/MyComponentSvelte5.svelte'; |
| 15 | +import { VERSION as SVELTE_VERSION } from 'svelte/compiler'; |
| 16 | + |
| 17 | +const startStimulus = () => { |
| 18 | + const application = Application.start(); |
| 19 | + application.register('svelte', SvelteController); |
| 20 | + |
| 21 | + return application; |
| 22 | +}; |
| 23 | + |
| 24 | +(window as any).resolveSvelteComponent = () => { |
| 25 | + return MyComponentSvelte5; |
| 26 | +}; |
| 27 | + |
| 28 | +describe.skipIf(SVELTE_VERSION < '5')('Svelte5Controller', () => { |
| 29 | + let application: Application; |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + clearDOM(); |
| 33 | + application.stop(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('connect with props', async () => { |
| 37 | + const container = mountDOM(` |
| 38 | + <div data-testid="component" |
| 39 | + data-controller="check svelte 5" |
| 40 | + data-svelte-component-value="Svelte5Component" |
| 41 | + data-svelte-props-value="{"name": "Svelte 5 !"}" /> |
| 42 | + `); |
| 43 | + |
| 44 | + const component = getByTestId(container, 'component'); |
| 45 | + |
| 46 | + application = startStimulus(); |
| 47 | + |
| 48 | + await waitFor(() => expect(component.innerHTML).toContain('<div><div>Hello Svelte 5 !</div></div>')); |
| 49 | + }); |
| 50 | + |
| 51 | + it('connect without props', async () => { |
| 52 | + const container = mountDOM(` |
| 53 | + <div data-testid="component" id="container" |
| 54 | + data-controller="check svelte 5" |
| 55 | + data-svelte-component-value="Svelte5Component" /> |
| 56 | + `); |
| 57 | + |
| 58 | + const component = getByTestId(container, 'component'); |
| 59 | + |
| 60 | + application = startStimulus(); |
| 61 | + |
| 62 | + await waitFor(() => expect(component.innerHTML).toContain('<div><div>Hello without props</div></div>')); |
| 63 | + }); |
| 64 | +}); |
0 commit comments