Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mountSuspended] Easier access to setupState #616

Closed
TimvdEijnden opened this issue Nov 15, 2023 · 0 comments · Fixed by danielroe/nuxt-vitest#399
Closed

[mountSuspended] Easier access to setupState #616

TimvdEijnden opened this issue Nov 15, 2023 · 0 comments · Fixed by danielroe/nuxt-vitest#399

Comments

@TimvdEijnden
Copy link
Contributor

I'm testing a page. And inside the unit test I'd like to access the setup state, so I can execute the onSuccess function.
It is possible but it seems a bit cumbersome, I understand I have to find the component because it's wrapped.

<template>
  <div>Example</div>
</template>
<script setup type="ts">
const { data } = await useDetailData();

function onSuccess() {
  window.dispatchEvent(new CustomEvent('success'));
</script>

This a unit test, as you can see it's difficult to access the setupState of the wrapped Componened passed to mountSuspended.
Or is this already possible somehow? Else I'd be able to make a PR to expose the setupState for easier access.

it('should dispatch success event', async () => {
  const wrapper = await mountSuspended(DetailPage); // name of file is [...slug].vue

  const spy = vi.spyOn(window, 'dispatchEvent');
  // this works
  const detailPageWrapper = wrapper.findComponent({ name: '[...slug]'}).componentVM._.setupState;
 
  // But i'd prefer to use something like this instead: 
  const detailPageWrapper = wrapper.findComponent({ name: '[...slug]'}).vm
  // Or this
  wrapper.setupState.onSuccess();

  
  detailPageWrapper.onSuccess();

  expect(spy).toHaveBeenCalledWith(
    expect.objectContaining({ type: 'success' })
  );
});
@TimvdEijnden TimvdEijnden changed the title Easier access to setupState [mountSuspended]Easier access to setupState Nov 15, 2023
@TimvdEijnden TimvdEijnden changed the title [mountSuspended]Easier access to setupState [mountSuspended] Easier access to setupState Nov 15, 2023
@danielroe danielroe transferred this issue from danielroe/nuxt-vitest Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants