-
Notifications
You must be signed in to change notification settings - Fork 19
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
Recommendations for testing with vue-test-utils? #28
Comments
@MaxGfeller Any ideas here? Thanks! |
Hi @danielbachhuber, sorry for only getting back to you now. That's a good question. Does the component actually get mounted? Because the Vue plugin does the wiring in the And where do you set up the store? |
Yep, the component is mounted. I can see the other HTML rendered by the component, but not the the HTML that would be rendered if Vuex Pagination items were present.
In beforeEach(() => {
mock = new MockAdapter(window.axios);
product = importJsonMock('models/Product/slack');
store = new Vuex.Store({
state: {
product,
},
actions,
mutations,
});
}); Also, I've added an it('mounts', async () => {
expect.assertions(1);
mock.onGet('/api-uri')
.reply(200, JSON.stringify([
firstItem,
secondItem,
]), {'x-total-count': 2});
initializeResources();
wrapper = mount(
Component,
{
localVue,
router,
store,
mocks: {
$route,
},
stubs: ['router-link', 'router-view']
},
);
await flushPromises();
await wrapper.vm.$nextTick();
}); |
Interestingly, I added some debugging inside of I also added some debug to the component itself. When export default {
computed: {
...mapState({
product: state => state.product,
}),
themes: createInstance('themes', {
pageSize: 200,
args() {
return {
product: this.product,
search: this.filter.search,
status: this.filter.status,
};
}
}),
themesIsLoading() {
return this.themes.loading;
}
},
watch: {
themesIsLoading(val) {
console.log('themesIsLoading');
console.log(val);
console.log(this.themes.items);
}
}
} So maybe the component isn't re-rendering when its computed state is changing? |
Yep, this must be it. Force updating causes the test to pass. it('mounts', async () => {
expect.assertions(1);
mock.onGet('/api-uri')
.reply(200, JSON.stringify([
firstItem,
secondItem,
]), {'x-total-count': 2});
initializeResources();
wrapper = mount(
Component,
{
localVue,
router,
store,
mocks: {
$route,
},
stubs: ['router-link', 'router-view']
},
);
await flushPromises();
await wrapper.vm.$nextTick();
await wrapper.vm.$forceUpdate();
}); |
I remember vaguely that i once had a similar problem... 🤔 I think you can also access |
Hm, force updating worked once and now it no longer works 😦 |
That sounds like a race condition. Does it work with directly checking |
Yeah, or some object mutation issue.
Oh, good catch. I thought all of my |
You can try more |
Hm, this doesn't seem to work either: it('mounts', async () => {
expect.assertions(1);
mock.onGet('/api-uri')
.reply(200, JSON.stringify([
firstItem,
secondItem,
]), {'x-total-count': 2});
initializeResources();
wrapper = mount(
Component,
{
localVue,
router,
store,
mocks: {
$route,
},
stubs: ['router-link', 'router-view']
},
);
await flushPromises();
await new Promise(r => setTimeout(r, 1000));
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$forceUpdate();
}); |
@MaxGfeller Here's a repository that reproduces the issue: https://github.com/danielbachhuber/vuex-pagination-28 If you run Notably, here's how the |
Thanks for the reproduction @danielbachhuber. I just had a look at it and got it to run. Here's what i did:
Hope this helps. |
Even better solution: update to Will document this in the README soon-ish. |
Thanks Max! I’ll check this out when I’m back at the computer in the next couple of days. I thought I tried the setTimeout approach but maybe I missed something. My reproduction example was from scratch, so there might be a nuance of my main application that it missed. If it doesn’t fix, I’ll send you a short screencast of some further debugging. Thanks again for your help! |
Sure thing! Problem was that the Vuex actions were not completed yet. That's why i added the Please let me know if it works in your project, too. |
@MaxGfeller The plot thickens! I've found the real source of the problem I'm experiencing in my project though. A component using Vuex pagination passes the assertions in the first The reproduction repo is updated to document this behavior: https://github.com/danielbachhuber/vuex-pagination-28/commit/2e7d42ba4a70655d41bd5d07a9040e17acd3cc1d I don't fully understand your store registration code is doing, but my guess is that Line 40 in 33f08e2
|
@danielbachhuber You are exactly right! In this case the store does not get reset. I added a https://github.com/cyon/vuex-pagination/blob/master/src/index.js#L115-L119 Hope that helps! |
Thanks @MaxGfeller ! |
@danielbachhuber Awesome! I'm closing this issue then and provide documentation for those two small additions in a later version 👍🏻 |
Hi,
Do you have any recommendations for how to test for paginated data with vue-test-utils?
My typical test looks like this:
However, the computed
createInstance()
seems stuck in the.loading
state, even after I've flushed the promises and waited for a re-render.Any ideas on what I might be doing wrong?
Thanks!
The text was updated successfully, but these errors were encountered: