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

Method to test scoped slots API #809

Open
davestaab opened this issue Jul 5, 2018 · 2 comments
Open

Method to test scoped slots API #809

davestaab opened this issue Jul 5, 2018 · 2 comments

Comments

@davestaab
Copy link
Contributor

What problem does this feature solve?

Given the following component:

export default {
{
    data() {
        return {
          count: 0
        };
      },
      methods: {
        increment() {
          this.count++;
        }
      },
      render() {
        return this.$scopedSlots.default({
          increment: this.increment // <-- api given to scoped slot
        });
      }
    }
}

How should the API given to scoped slots be tested?

Currently, the best approach I've found is creating a test helper component that's mounted in the test and renders the given component and it's scoped slot contents. Example of this approach by posva: https://github.com/posva/vue-promised/tree/master/test/utils

Creating a component to be used just for unit tests in not ideal. It's extra code polluting my tests folder. That code can be complicated and the more complex the component api the more components are needed to fully test the api.

What does the proposed API look like?

The solution to this could be to expose the scoped slot api from wrapper.

const wrapper = shallowMount(MyComponent);
expect(wrapper.vm.count).toBe(0);
wrapper.scopedSlotApi().default.increment(); // <-- using scoped slots api
expect(wrapper.vm.count).toBe(1);

This provides several benefits.

  1. I can simple assert that the api exposes the correct methods. Currently there's no way to do this with methods. I actually have to render scoped slot content that ties some event to the method, trigger the event and then assert the method made some change.
  2. The test is concerned with testing the api and doesn't have to build out a rendering of the API to test that.

Alternate solution

Maybe this would be a better approach as it doesn't add any new methods to wrapper api.

The mount options for scopedSlots could be given the api which could then be captured.

  let capturedApi;
  const wrapper = mount(MyComponent, { 
    scopedSlots: {
      default: api => {
        capturedApi = api;
      }
    }
  });

  expect(capturedApi.increment).toBeDefined();
  expect(wrapper.vm.count).toBe(0);
  capturedApi.increment();
  expect(wrapper.vm.count).toBe(1);
@lloydjatkinson
Copy link

Been a couple of years - this could be really useful!

@rriixx
Copy link

rriixx commented Apr 26, 2021

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants