|
| 1 | +import './commands'; |
| 2 | +import AppTemplate from '../templates/App.vue'; |
| 3 | +import vuetify from "../../src/plugins/vuetify"; |
| 4 | +import { h } from "vue"; |
| 5 | +import { mount } from 'cypress/vue'; |
| 6 | +import VStepperForm from '../../src/plugin/VStepperForm.vue'; |
| 7 | +import * as DATA from '../templates/testData'; |
| 8 | +import type { Component } from 'vue'; |
| 9 | +import "cypress-real-events"; |
| 10 | + |
| 11 | + |
| 12 | +// declare global { |
| 13 | +// namespace Cypress { |
| 14 | +// interface Chainable { |
| 15 | +// baseIconClass(icon: string): string; |
| 16 | +// getBaseStepperElements(excluded: string[]): Chainable; |
| 17 | +// getDataCy(value: string): Chainable<JQuery<HTMLElement>>; |
| 18 | +// mount: typeof mount; |
| 19 | +// mountComponent(options: any): Chainable; |
| 20 | +// } |
| 21 | +// } |
| 22 | +// } |
| 23 | + |
| 24 | +Cypress.Commands.add('mount', (component: Component, options: any = {}) => { |
| 25 | + // Ensure global settings are defined |
| 26 | + options.global = options.global || {}; |
| 27 | + options.global.stubs = options.global.stubs || {}; |
| 28 | + options.global.stubs['transition'] = false; |
| 29 | + options.global.components = options.global.components || {}; |
| 30 | + options.global.plugins = options.global.plugins || [vuetify]; |
| 31 | + |
| 32 | + // Process slots to ensure they are functions |
| 33 | + const slots = options.slots |
| 34 | + ? Object.fromEntries( |
| 35 | + Object.entries(options.slots).map(([key, value]) => [ |
| 36 | + key, |
| 37 | + // Convert strings or other non-function values into functions |
| 38 | + typeof value === 'function' |
| 39 | + ? value |
| 40 | + : () => (typeof value === 'string' ? h('div', value) : h(value as any)), |
| 41 | + ]) |
| 42 | + ) |
| 43 | + : {}; |
| 44 | + |
| 45 | + // Mount AppTemplate as the root and render `component` inside it |
| 46 | + return mount(AppTemplate, { |
| 47 | + ...options, |
| 48 | + slots: { |
| 49 | + // Render the main component in the default slot of AppTemplate |
| 50 | + default: () => h(component, options.props, slots), |
| 51 | + }, |
| 52 | + }) as Cypress.Chainable; |
| 53 | +}); |
| 54 | + |
| 55 | + |
| 56 | +Cypress.Commands.add('getDataCy', (name: string) => { |
| 57 | + return cy.get(`[data-cy="${name}"]`); |
| 58 | +}); |
| 59 | + |
| 60 | +const answers = { |
| 61 | + buttonField: null, |
| 62 | +}; |
| 63 | + |
| 64 | +const buttonField = DATA.buttonFieldOptions; |
| 65 | + |
| 66 | +const fieldDefault = { |
| 67 | + label: 'Button Field Question', |
| 68 | + name: 'buttonField', |
| 69 | + options: buttonField.options.basic, |
| 70 | + type: 'buttons' as const, |
| 71 | +}; |
| 72 | + |
| 73 | +const globalOptions = { |
| 74 | + validateOn: 'blur', |
| 75 | +}; |
| 76 | + |
| 77 | +interface MountComponentOptions { |
| 78 | + modelValue?: Record<string, any>; |
| 79 | + field?: Partial<typeof fieldDefault>; |
| 80 | + globalProps?: Record<string, any>; |
| 81 | + stepperProps?: Record<string, any>; |
| 82 | +} |
| 83 | + |
| 84 | +Cypress.Commands.add('mountComponent', (options: MountComponentOptions = {}) => { |
| 85 | + const { modelValue = {}, field = {}, globalProps = {}, stepperProps = {} } = options; |
| 86 | + |
| 87 | + const localModelValue = { ...answers, ...modelValue }; |
| 88 | + |
| 89 | + return cy.then(() => { |
| 90 | + cy.mount(VStepperForm as any, { |
| 91 | + props: { |
| 92 | + modelValue: localModelValue, |
| 93 | + pages: [{ fields: [{ ...fieldDefault, ...field, }], }], |
| 94 | + onSubmit: stepperProps.onSubmit ?? undefined, |
| 95 | + validationSchema: stepperProps.validationSchema ?? undefined, |
| 96 | + ...stepperProps, |
| 97 | + }, |
| 98 | + global: { provide: { globalOptions: { ...globalOptions, ...globalProps }, }, }, |
| 99 | + }).as('wrapper'); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | + |
| 104 | +Cypress.Commands.add('getBaseStepperElements', (excluded = []) => { |
| 105 | + // Stepper Form // |
| 106 | + cy.get('[data-cy="vsf-stepper-form"]').as('stepperForm'); |
| 107 | + cy.get('@stepperForm') |
| 108 | + .should('exist') |
| 109 | + .and('be.visible'); |
| 110 | + |
| 111 | + // Stepper Header // |
| 112 | + cy.getDataCy('vsf-stepper-header').as('stepperHeader'); |
| 113 | + cy.get('@stepperHeader') |
| 114 | + .should('exist') |
| 115 | + .and('be.visible'); |
| 116 | + |
| 117 | + cy.get('@stepperHeader') |
| 118 | + .find('.v-stepper-item') |
| 119 | + .as('stepperHeaderItems'); |
| 120 | + |
| 121 | + // Application Wrap // |
| 122 | + cy.get('.v-application__wrap').as('appWrap'); |
| 123 | + |
| 124 | + // Submit Button // |
| 125 | + if (!excluded.includes('buttonsField')) { |
| 126 | + cy.getDataCy('vsf-submit-button') |
| 127 | + .should('exist') |
| 128 | + .and('be.visible'); |
| 129 | + |
| 130 | + // Field Group and Buttons // |
| 131 | + cy.getDataCy('vsf-field-group-buttonField').as('fieldGroup'); |
| 132 | + cy.getDataCy('vsf-field-group-buttonField').find('button').as('fieldButtons'); |
| 133 | + } |
| 134 | +}); |
| 135 | + |
| 136 | +cy.baseIconClass = (icon: string) => { |
| 137 | + return icon.replace(/^mdi:/, ''); |
| 138 | +}; |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Navigation // |
| 143 | +Cypress.Commands.add('navigationMountComponent', (options = {}) => { |
| 144 | + const { editable = true, jumpAhead = true, pages, validationSchema = undefined } = options || {}; |
| 145 | + const answers = DATA.navigationTest.answers; |
| 146 | + |
| 147 | + cy.mount(VStepperForm as any, { |
| 148 | + props: { |
| 149 | + answers, |
| 150 | + editable, |
| 151 | + jumpAhead, |
| 152 | + modelValue: answers, |
| 153 | + pages, |
| 154 | + validationSchema, |
| 155 | + }, |
| 156 | + global: DATA.navigationTest.global, |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +Cypress.Commands.add('navigationGetButtons', () => { |
| 161 | + cy.getBaseStepperElements(['buttonsField']); |
| 162 | + cy.getDataCy('vsf-next-button').as('nextButton'); |
| 163 | + cy.getDataCy('vsf-previous-button').as('previousButton'); |
| 164 | +}); |
| 165 | + |
| 166 | +Cypress.Commands.add('checkedEnabledDisabledHeaderItems', ({ enabled, disabled, pages }) => { |
| 167 | + pages.forEach((_, index) => { |
| 168 | + if (enabled.includes(index)) { |
| 169 | + cy.get('@stepperHeaderItems') |
| 170 | + .eq(index) |
| 171 | + .should('be.enabled'); |
| 172 | + } |
| 173 | + |
| 174 | + if (disabled.includes(index)) { |
| 175 | + cy.get('@stepperHeaderItems') |
| 176 | + .eq(index) |
| 177 | + .should('be.disabled'); |
| 178 | + } |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +cy.cloneArray = (array: any[]): any[] => { |
| 183 | + return JSON.parse(JSON.stringify(array)); |
| 184 | +}; |
0 commit comments