WIP: dataCy_directive #1053

Draft
jsegarra wants to merge 4 commits from dataCy_directive into dev
11 changed files with 94 additions and 4 deletions

11
src/boot/dataCy.js Normal file
View File

@ -0,0 +1,11 @@
export default {
mounted: function (el, binding, { ctx }) {
const { innerText, localName } = el;
const { type, attrs } = ctx;
const name = ctx.parent.attrs?.name ?? ctx.parent.attrs?.label ?? innerText;
const value = `${attrs.dataCy ?? name}_${type.name
.replace('Q', '')
.toLowerCase()}`;
el.setAttribute('data-cy', value);
},
};

View File

@ -3,12 +3,14 @@ import qFormMixin from './qformMixin';
import keyShortcut from './keyShortcut'; import keyShortcut from './keyShortcut';
import useNotify from 'src/composables/useNotify.js'; import useNotify from 'src/composables/useNotify.js';
import { CanceledError } from 'axios'; import { CanceledError } from 'axios';
import dataCy from './dataCy';
const { notify } = useNotify(); const { notify } = useNotify();
export default boot(({ app }) => { export default boot(({ app }) => {
app.mixin(qFormMixin); app.mixin(qFormMixin);
app.directive('shortcut', keyShortcut); app.directive('shortcut', keyShortcut);
app.directive('dataCy', dataCy);
app.config.errorHandler = (error) => { app.config.errorHandler = (error) => {
let message; let message;
const response = error.response; const response = error.response;

View File

@ -79,5 +79,5 @@ function accountShortToStandard() {
</script> </script>
<template> <template>
<QInput @keydown="handleKeydown" ref="vnInputRef" v-model="internalValue" /> <QInput @keydown="handleKeydown" ref="vnInputRef" v-model="internalValue" v-data-cy />
</template> </template>

View File

@ -64,6 +64,7 @@ defineExpose({ show: () => changePassDialog.value.show() });
size="xs" size="xs"
style="flex: 0" style="flex: 0"
v-close-popup v-close-popup
v-data-cy
/> />
</VnRow> </VnRow>
</slot> </slot>
@ -113,6 +114,7 @@ defineExpose({ show: () => changePassDialog.value.show() });
flat flat
type="reset" type="reset"
v-close-popup v-close-popup
data-cy
/> />
<QBtn <QBtn
:disabled="isLoading" :disabled="isLoading"
@ -120,6 +122,7 @@ defineExpose({ show: () => changePassDialog.value.show() });
:label="t('globals.confirm')" :label="t('globals.confirm')"
color="primary" color="primary"
@click="validate" @click="validate"
data-cy
/> />
</slot> </slot>
</QCardActions> </QCardActions>

View File

@ -2,7 +2,7 @@
const model = defineModel({ type: [String, Number], required: true }); const model = defineModel({ type: [String, Number], required: true });
</script> </script>
<template> <template>
<QDate v-model="model" :today-btn="true" :options="$attrs.options" /> <QDate v-model="model" :today-btn="true" :options="$attrs.options" data-cy />
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.q-date { .q-date {

View File

@ -157,6 +157,7 @@ function addDefaultData(data) {
/> />
</VnRow> </VnRow>
<QInput <QInput
v-data-cy
:label="t('globals.description')" :label="t('globals.description')"
v-model="dms.description" v-model="dms.description"
type="textarea" type="textarea"

View File

@ -132,7 +132,7 @@ const handleInsertMode = (e) => {
:rules="mixinRules" :rules="mixinRules"
:lazy-rules="true" :lazy-rules="true"
hide-bottom-space hide-bottom-space
:data-cy="$attrs.dataCy ?? $attrs.label + '_input'" v-data-cy
> >
<template v-if="$slots.prepend" #prepend> <template v-if="$slots.prepend" #prepend>
<slot name="prepend" /> <slot name="prepend" />

View File

@ -108,6 +108,7 @@ const manageDate = (date) => {
:clearable="false" :clearable="false"
@click="isPopupOpen = true" @click="isPopupOpen = true"
hide-bottom-space hide-bottom-space
v-data-cy
> >
<template #append> <template #append>
<QIcon <QIcon
@ -124,6 +125,7 @@ const manageDate = (date) => {
model = null; model = null;
isPopupOpen = false; isPopupOpen = false;
" "
v-data-cy
/> />
<QIcon <QIcon
v-if="showEvent" v-if="showEvent"
@ -131,6 +133,7 @@ const manageDate = (date) => {
class="cursor-pointer" class="cursor-pointer"
@click="isPopupOpen = !isPopupOpen" @click="isPopupOpen = !isPopupOpen"
:title="t('Open date')" :title="t('Open date')"
v-data-cy
/> />
</template> </template>
<QMenu <QMenu

View File

@ -313,7 +313,7 @@ function handleKeyDown(event) {
:input-debounce="useURL ? '300' : '0'" :input-debounce="useURL ? '300' : '0'"
:loading="isLoading" :loading="isLoading"
@virtual-scroll="onScroll" @virtual-scroll="onScroll"
:data-cy="$attrs.dataCy ?? $attrs.label + '_select'" v-data-cy
> >
<template #append> <template #append>
<QIcon <QIcon

View File

@ -98,6 +98,7 @@ function cancel({ cancel }) {
{{ t('Mana') }} {{ toCurrency(props.mana) }} {{ t('Mana') }} {{ toCurrency(props.mana) }}
</QBanner> </QBanner>
<QInput <QInput
v-data-cy
v-model="scope.value" v-model="scope.value"
type="number" type="number"
dense dense
@ -117,6 +118,7 @@ function cancel({ cancel }) {
<QCardActions class="q-px-none q-mt-sm" align="right"> <QCardActions class="q-px-none q-mt-sm" align="right">
<QBtn :label="t('Cancel')" color="primary" flat @click="cancel(scope)" /> <QBtn :label="t('Cancel')" color="primary" flat @click="cancel(scope)" />
<QBtn <QBtn
data-cy
:label="t('Update')" :label="t('Update')"
color="primary" color="primary"
:loading="isLoading" :loading="isLoading"

View File

@ -0,0 +1,68 @@
// tests/dataCy.spec.js
import { mount, shallowMount } from '@vue/test-utils';
import { describe, it, expect } from 'vitest';
import { Quasar, QInput, QSelect, QIcon, QBtn } from 'quasar';
import dataCy from 'src/boot/dataCy.js';
const mountWithQuasar = (component, options) => {
return mount(component, {
global: {
plugins: [Quasar],
stubs: {
vDataCy: true,
},
directives: {
dataCy: dataCy,
},
},
...options,
});
};
describe('dataCy directive', () => {
it.only('applies data-cy attribute to QInput', () => {
const wrapper = mountWithQuasar(QInput, {
props: {
label: 'Test Input',
},
});
const input = wrapper.find('input');
dataCy.mounted(input.element, null, {
ctx: {
type: QInput,
attrs: { dataCy: 'Test Input' },
parent: { attrs: { label: 'Test Input' } },
},
});
expect(input.attributes('data-cy')).toBe('Test Input_input');
});
it('applies data-cy attribute to QSelect', () => {
const wrapper = mountWithQuasar(QSelect, {
props: {
label: 'Test Select',
},
});
const select = wrapper.find('.q-field__native');
expect(select.attributes('data-cy')).toBe('Test Select_select');
});
it('applies data-cy attribute to QIcon', () => {
const wrapper = mountWithQuasar(QIcon, {
props: {
name: 'home',
},
});
const icon = wrapper.find('i');
expect(icon.attributes('data-cy')).toBe('home_icon');
});
it('applies data-cy attribute to QBtn', () => {
const wrapper = mountWithQuasar(QBtn, {
props: {
label: 'Test Button',
},
});
const button = wrapper.find('button');
expect(button.attributes('data-cy')).toBe('Test Button_btn');
});
});