Skip to content

Commit

Permalink
chore: Move component-index generation to scaffold-config package (#2…
Browse files Browse the repository at this point in the history
…1090)

* chore: Move component-index generation to scaffold-config package

* Review fixes

* Fix review comments for realzies this time
  • Loading branch information
Blue F committed Apr 20, 2022
1 parent b8ba111 commit 10dfccc
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 64 deletions.
62 changes: 4 additions & 58 deletions packages/data-context/src/actions/WizardActions.ts
Expand Up @@ -10,11 +10,6 @@ const debug = Debug('cypress:data-context:wizard-actions')

import type { DataContext } from '..'

interface WizardGetCodeComponent {
chosenLanguage: 'js' | 'ts'
chosenFramework: typeof WIZARD_FRAMEWORKS[number]
}

export class WizardActions {
constructor (private ctx: DataContext) {}

Expand Down Expand Up @@ -200,10 +195,7 @@ export class WizardActions {
this.scaffoldFixtures(),
this.scaffoldSupport('component', chosenLanguage),
this.scaffoldSupport('commands', chosenLanguage),
this.getComponentIndexHtml({
chosenFramework,
chosenLanguage,
}),
this.scaffoldComponentIndexHtml(chosenFramework),
])
}

Expand Down Expand Up @@ -325,64 +317,18 @@ export class WizardActions {
return codeBlocks.join('\n')
}

private async getComponentIndexHtml (opts: WizardGetCodeComponent): Promise<NexusGenObjects['ScaffoldedFile']> {
const [storybookInfo] = await Promise.all([
this.ctx.storybook.loadStorybookInfo(),
this.ensureDir('component'),
])
const framework = opts.chosenFramework.type
let headModifier = ''
let bodyModifier = ''

if (framework === 'nextjs') {
headModifier += '<div id="__next_css__DO_NOT_USE__"></div>'
}

const previewHead = storybookInfo?.files.find(({ name }) => name === 'preview-head.html')

if (previewHead) {
headModifier += previewHead.content
}

const previewBody = storybookInfo?.files.find(({ name }) => name === 'preview-body.html')

if (previewBody) {
headModifier += previewBody.content
}

const template = this.getComponentTemplate({
headModifier,
bodyModifier,
})
private async scaffoldComponentIndexHtml (chosenFramework: typeof WIZARD_FRAMEWORKS[number]): Promise<NexusGenObjects['ScaffoldedFile']> {
await this.ensureDir('component')

const componentIndexHtmlPath = path.join(this.projectRoot, 'cypress', 'support', 'component-index.html')

return this.scaffoldFile(
componentIndexHtmlPath,
template,
chosenFramework.componentIndexHtml(),
'The HTML used as the wrapper for all component tests',
)
}

private getComponentTemplate = (opts: { headModifier: string, bodyModifier: string }) => {
// TODO: Properly indent additions and strip newline if none
return dedent`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
${opts.headModifier}
</head>
<body>
${opts.bodyModifier}
<div data-cy-root></div>
</body>
</html>`
}

private async scaffoldFile (filePath: string, contents: string, description: string): Promise<NexusGenObjects['ScaffoldedFile']> {
try {
debug('scaffoldFile: start %s', filePath)
Expand Down
27 changes: 27 additions & 0 deletions packages/scaffold-config/src/component-index-template.ts
@@ -0,0 +1,27 @@
import dedent from 'dedent'

const componentIndexHtmlGenerator = (headModifier: string = '') => {
return () => {
const template = dedent`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
${headModifier}
</head>
<body>
<div data-cy-root></div>
</body>
</html>
`

// If the framework supplies an empty string for the modifier,
// strip out the empty line
return template.replace(/\n {4}\n/g, '\n')
}
}

export default componentIndexHtmlGenerator
9 changes: 9 additions & 0 deletions packages/scaffold-config/src/frameworks.ts
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import dedent from 'dedent'
import fs from 'fs-extra'
import * as dependencies from './dependencies'
import componentIndexHtmlGenerator from './component-index-template'
import { defineConfigAvailable } from '@packages/data-context/src/sources/migration/codegen'
import semver from 'semver'
import resolveFrom from 'resolve-from'
Expand Down Expand Up @@ -122,6 +123,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: 'cypress/react',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vueclivue2',
Expand All @@ -142,6 +144,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: 'cypress/vue2',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vueclivue3',
Expand All @@ -162,6 +165,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: 'cypress/vue',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'nextjs',
Expand All @@ -181,6 +185,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: 'cypress/react',
componentIndexHtml: componentIndexHtmlGenerator('<div id="__next_css__DO_NOT_USE__"></div>'),
},
{
type: 'nuxtjs',
Expand All @@ -200,6 +205,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: 'cypress/vue2',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vue2',
Expand All @@ -219,6 +225,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: 'cypress/vue2',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'vue3',
Expand All @@ -238,6 +245,7 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'vue',
glob: '*.vue',
mountModule: 'cypress/vue',
componentIndexHtml: componentIndexHtmlGenerator(),
},
{
type: 'react',
Expand All @@ -257,5 +265,6 @@ export const WIZARD_FRAMEWORKS = [
codeGenFramework: 'react',
glob: '*.{js,jsx,tsx}',
mountModule: 'cypress/react',
componentIndexHtml: componentIndexHtmlGenerator(),
},
] as const
@@ -0,0 +1,46 @@
import { expect } from 'chai'
import dedent from 'dedent'
import componentIndexHtmlGenerator from '../../src/component-index-template'

describe('componentIndexHtmlGenerator', () => {
it('strips spaces and newlines appropriately', () => {
const generator = componentIndexHtmlGenerator()

const expected = dedent`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>
<body>
<div data-cy-root></div>
</body>
</html>`

expect(generator()).to.eq(expected)
})

it('handles header modifier', () => {
const generator = componentIndexHtmlGenerator('foobar')

const expected = dedent`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
foobar
</head>
<body>
<div data-cy-root></div>
</body>
</html>`

expect(generator()).to.eq(expected)
})
})
Expand Up @@ -5,10 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>

</head>
<body>

<div data-cy-root></div>
</body>
</html>
</html>
Expand Up @@ -5,10 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>

</head>
<body>

<div data-cy-root></div>
</body>
</html>
</html>

2 comments on commit 10dfccc

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10dfccc Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/linux-x64/10.0-release-10dfccc67495a5161b3a95dcb003101b651eb393/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 10dfccc Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.0/darwin-x64/10.0-release-10dfccc67495a5161b3a95dcb003101b651eb393/cypress.tgz

Please sign in to comment.