Skip to content

Commit

Permalink
Merge pull request #22287 from cypress-io/mikep/CLOUD-577-fix-compone…
Browse files Browse the repository at this point in the history
…nt-tests
  • Loading branch information
MuazOthman committed Jun 14, 2022
2 parents d181a96 + e3c89b0 commit 24da048
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 24da048

Please sign in to comment.