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

fix: Allow GQL variable definitions in mountFragment #22287

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/app/src/specs/SpecsList.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { defaultMessages } from '@cy/i18n'

function mountWithTestingType (testingType: TestingTypeEnum) {
cy.mountFragment(Specs_SpecsListFragmentDoc, {
variableTypes: {
hasBranch: 'Boolean',
},
variables: {
hasBranch: true,
},
onResult: (ctx) => {
if (!ctx.currentProject) throw new Error('need current project')

Expand All @@ -25,6 +31,12 @@ describe('<SpecsList />', { keystrokeDelay: 0 }, () => {
const showCreateSpecModalSpy = cy.spy().as('showCreateSpecModalSpy')

cy.mountFragment(Specs_SpecsListFragmentDoc, {
variableTypes: {
hasBranch: 'Boolean',
},
variables: {
hasBranch: true,
},
onResult: (ctx) => {
specs = ctx.currentProject?.specs || []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,20 @@ export const registerMountFn = ({ plugins }: MountFnOptions = {}) => {
mountingOptions?.global?.plugins?.push(pluginFn())
})

let queryVariablesSegment = ''

if (options.variableTypes) {
queryVariablesSegment = Object.entries(options.variableTypes || {}).map(([name, type]) => `\$${name}: ${type}`).join(',')
queryVariablesSegment = `(${queryVariablesSegment})`
}

return mount(defineComponent({
name: `MountFragment`,
setup () {
const result = useQuery({
variables: options.variables,
query: `
query MountFragmentTest {
query MountFragmentTest${queryVariablesSegment} {
${fieldName} {
...${(source.definitions[0] as FragmentDefinitionNode).name.value}
}
Expand Down Expand Up @@ -161,6 +169,16 @@ export const registerMountFn = ({ plugins }: MountFnOptions = {}) => {
}

type MountFragmentConfig<T extends TypedDocumentNode<any, any>> = {
/**
* Dictionary of GQL variable names to their GQL data type (String, Boolean!, etc)
* for any variables that are used in the fragment.
* This should be used in conjunction with `variables`
*/
variableTypes?: Record<keyof VariablesOf<T>, string>
/**
* Dictionary of variable names to their values for any variables that are used in the
* fragment. This should be used in conjunction with `variableTypes`
*/
variables?: VariablesOf<T>
/**
* When we are mounting a GraphQL Fragment, we can use `onResult`
Expand All @@ -180,6 +198,7 @@ type MountFragmentListConfig<T extends TypedDocumentNode<any, any>> = {
* @default 2
*/
count?: number
variableTypes?: Record<keyof VariablesOf<T>, string>
variables?: VariablesOf<T>
render: (frag: Exclude<ResultOf<T>, undefined>[]) => JSX.Element
onResult?: (result: ResultOf<T>, ctx: ClientTestContext) => ResultOf<T> | void
Expand Down