-
Notifications
You must be signed in to change notification settings - Fork 65
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
QDialog is not mounted in tests when used with mount() #72
Comments
Hi, The QDialog, QMenu and QTooltip components do not mount in the same place that they are used. They are mounted at the end of What you should do is to use |
I've tried it, although I can tell the dialog is being opened somewhere, I cannot find a way of selecting any of its children. In my tests, Thanks for trying to help! |
I use the following code to be able to test the components in the QDialog. @rstoenescu Could it be considered the use of portal-vue? import { createWrapper } from '@vue/test-utils'
/**
* The dialogs are created through document.createElement in order to place the elements at the root
* level of the page's dom (they won't be subcomponents of any view).
* This methos allows getting access to the component of the dialog if there is any.
* @returns {Wrapper} the vue wrapper of the dialog
*/
export const getDialogComponentWrapper = () => {
let dialogElements = document.getElementsByClassName('q-dialog')
// if there is any, there should be only one
if (dialogElements
&& dialogElements.length>0
&& dialogElements[0].__vue__){
return createWrapper(dialogElements[0].__vue__)
}
return undefined
} |
This is such a clever way of doing it, well done. I had no idea a DOM element has a corresponding |
Well, it relies too much on the internal structures of vue and it is subject to be broken with future releases of vue.... That's why I was asking if it could be used portal-vue in the implementation of the QDialog as it seems to be posible to mock for the tests the location where these dialogs will be appearing in the dom. https://portal-vue.linusb.org/guide/getting-started.html#use-cases |
While this is a brilliant workaround (for now), you need to call
|
Based on @Abuntxa 's awesome answer I use the following technique to test // somewhere in the setup process I create the wrapper of the component I want to test
const wrapper = mount(MyComponent, { localVue })
// in the same place I create a wrapper for `document.body`
const bodyWrapper = createWrapper(document.body)
// and when I need it, I can query the `q-dialog` on bodyWrapper
bodyWrapper.find('.q-dialog') The good thing is that The bad thing is - as @kennyki mentioned - that even if I create a brand new Thanks guys! |
@SzNagyMisu could u plz share the whole file ? |
Hey guys! Is there any new progress here? I'm running quasar 2 with Vue3, but I can't find any way how to test contents of Qdialog.. I also tried to create separate component and open in via dialog plugin like this |
This is a long-standing problem with Portal-based components, but still could not find a workaround If you need to test the dialog content, abstract the dialog inside its own component and mount that component for testing |
I've been able to get this to work with Vue 3 and quasar 2 using the attachTo property when mounting the component.
|
@OneFourFree I think you saved me multiple hours of struggle ❤️ |
@OneFourFree do you have some time to create a PR adding you solution either to the README or as an example automatically scaffolded when adding Jest AE to a project? |
@IlCallo yeah I'll have time to add that this week |
Thanks :D |
Latest Quasar Upgrade (the jest/test utils that are maintained independently or something) breaks this again... |
@AlphaJuliettOmega Play necromancer in Diablo, but not GitHub Please do us a favor and open a new issue |
@Shinigami92 Ok, will try do so! |
Sorry for reviving the thread. Just to sum up for people still looking how to deal with QDialog testing in Vue 3 usingTest Utils V2 : Thanks @OneFourFree updating MyDialog.spec.js import { installQuasarPlugin } from '@quasar/quasar-app-extension-testing-unit-jest';
import { beforeEach, describe, expect, it } from '@jest/globals';
import { DOMWrapper, mount } from '@vue/test-utils';
import MyDialog from './demo/MyDialog';
installQuasarPlugin();
describe('MyDialog', () => {
beforeEach(() => {
mount(MyDialog, {
data: () => ({
isDialogOpen: true,
}),
});
});
it('should mount the document body and expose for testing', () => {
const wrapper = new DOMWrapper(document.body);
expect(wrapper.find('.q-dialog').exists()).toBeTruthy();
});
it('can check the inner text of the dialog', () => {
const wrapper = new DOMWrapper(document.body);
expect(wrapper.find('.q-dialog').html()).toContain(
'Custom dialog which should be tested',
);
});
}); |
Follow up on this Discord chat: https://discordapp.com/channels/415874313728688138/450312684790087691/560449585307320322
I am using 1.0.
If used with
shallowMount()
, it mounts the stub fine. If used withmount()
thewrapper.html()
doesn't have any mention of the<q-dialog>
component.Some Quasar components run in their own context (QDialog, QMenu, QTooltip). I am not sure, but maybe this is somehow causing this problem.
Usually, I would just full mount the component that uses Quasar components and then trigger the events on components like QBtn. Currently, I can't do that.
My component:
My test:
Snapshot output:
The text was updated successfully, but these errors were encountered: