svelte-testing-library icon indicating copy to clipboard operation
svelte-testing-library copied to clipboard

Issue with testing components that use slots

Open PClmnt opened this issue 3 years ago • 1 comments

I don't seem to be able to use the fireEvent method to trigger a change while using a component that has slots?

I've made an example in this repo, you can clone it and run yarn test to see, but essentially the fireEvent doesn't seem to bubble up the change to the parent component when the child component is using a slot?

https://github.com/PClmnt/svelte-testing-library-issue

PClmnt avatar Jan 31 '22 12:01 PClmnt

I'm thinking this may be more of a nuance of carbon-components-svelte than of svelte-testing-library. After looking at issues in the carbon components repo it seems like the suggested use, in this case, is to add the event handler to the slot component. See this comment as an example. Indeed if I follow the pattern suggested there and change your example to:

<script>
	import {  Button } from 'carbon-components-svelte'

	let loggedIn = 'yes'
	
	function changeState() {
		loggedIn = 'no'
  	}


</script>
<section>
	<div>
	  {#if loggedIn === 'yes'}
		<Button  as kind="secondary">
		  <span on:click={() => changeState()}>test</span>
		</Button>
	  {:else if loggedIn === 'no'}
	  <div>example text</div>
	  {/if}
	</div>
  </section>

The test now passes

Screen Shot 2022-04-07 at 10 09 32 PM

jgbowser avatar Apr 08 '22 04:04 jgbowser

@yanick I agree with @jgbowser's assessment, this definitely looks like a fundamental limitation/problem with carbon-components-svelte. There's nothing unique to svelte-testing-library here, I think this issue should be closed

mcous avatar Jan 24 '24 07:01 mcous

I agree with the agreement. Closing. :-)

yanick avatar Jan 24 '24 14:01 yanick