From 3a9a064b8e1303ed654a6954a479800d648db567 Mon Sep 17 00:00:00 2001 From: Caleb Ukle Date: Mon, 17 Apr 2023 15:51:53 -0500 Subject: [PATCH] wip --- e2e/next/src/next-component-tests.test.ts | 175 +++++++++++++++++- .../cypress-component-configuration.ts | 11 +- 2 files changed, 172 insertions(+), 14 deletions(-) diff --git a/e2e/next/src/next-component-tests.test.ts b/e2e/next/src/next-component-tests.test.ts index c6aaf7ea7548ff..db29e3778c961c 100644 --- a/e2e/next/src/next-component-tests.test.ts +++ b/e2e/next/src/next-component-tests.test.ts @@ -1,24 +1,181 @@ -import { newProject, uniq } from '@nrwl/e2e/utils'; +import { + createFile, + newProject, + runCLI, + uniq, + updateFile, + runCypressTests, +} from '@nrwl/e2e/utils'; describe('NextJs Component Testing', () => { - let projName: string; + let projectName: string; const appName = uniq('next-app'); const libName = uniq('next-lib'); const buildableLibName = uniq('next-buildable-lib'); beforeAll(() => { - projName = newProject({ - name: 'next-ct', + projectName = newProject({ + name: uniq('next-ct'), }); }); - it('should test a NextJs app', () => {}); + it('should test a NextJs app', () => { + createAppWithCt(appName); + if (runCypressTests) { + expect(runCLI(`component-test ${appName} --no-watch`)).toContain( + 'All specs passed!' + ); + } + addTailwindToApp(appName); + if (runCypressTests) { + expect(runCLI(`component-test ${appName} --no-watch`)).toContain( + 'All specs passed!' + ); + } + }); - it('should test a NextJs lib', () => {}); + it('should test a NextJs lib', async () => { + createLibWithCt(libName, false); + if (runCypressTests) { + expect(runCLI(`component-test ${libName} --no-watch`)).toContain( + 'All specs passed!' + ); + } - it('should test a NextJs buildable lib', () => {}); + addTailwindToLib(libName); + }); - it('should support tailwinds across targets', () => {}); + it.skip('should test a NextJs buildable lib', async () => { + createLibWithCt(buildableLibName, true); + if (runCypressTests) { + expect(runCLI(`component-test ${buildableLibName} --no-watch`)).toContain( + 'All specs passed!' + ); + } + }); - it('should be able to load assest from app', () => {}); + it.todo('should support tailwinds'); + + it.todo('should be able to load assest'); }); + +function createAppWithCt(appName: string) { + runCLI(`generate @nrwl/next:app ${appName} --no-interactive`); + runCLI( + `generate @nrwl/next:component button --project=${appName} --directory=components --flat --no-interactive` + ); + createFile( + `apps/${appName}/public/data.json`, + JSON.stringify({ message: 'loaded from app data.json' }) + ); + + updateFile(`apps/${appName}/components/button.tsx`, (content) => { + return `import { useEffect, useState } from 'react'; + +export interface ButtonProps { + text: string; +} + +const data = fetch('/data.json').then((r) => r.json()); +export default function Button(props: ButtonProps) { + const [state, setState] = useState>(); + useEffect(() => { + data.then(setState); + }, []); + return ( + <> + {state &&
{JSON.stringify(state, null, 2)}
} +

Button

+ + + ); +} +`; + }); + + runCLI( + `generate @nrwl/next:cypress-component-configuration --project=${appName} --generate-tests --no-interactive` + ); +} + +function addTailwindToApp(appName: string) { + runCLI( + `generate @nrwl/react:setup-tailwind --project=${appName} --no-interactive` + ); + updateFile(`apps/${appName}/cypress/support/component.ts`, (content) => { + return `${content} +import '../../pages/styles.css'`; + }); + + updateFile(`apps/${appName}/components/button.cy.tsx`, (content) => { + return `import * as React from 'react'; +import Button from './button'; + +describe(Button.name, () => { + it('renders', () => { + cy.mount( +} +`; + }); + + runCLI( + `generate @nrwl/next:cypress-component-configuration --project=${libName} --generate-tests --no-interactive` + ); +} +function addTailwindToLib(libName: string) { + createFile(`libs/${libName}/src/lib/styles.css`, ``); + runCLI( + `generate @nrwl/react:setup-tailwind --project=${libName} --no-interactive` + ); + updateFile(`libs/${libName}/cypress/support/component.ts`, (content) => { + return `${content} +import '../../src/lib/styles.css'`; + }); + + updateFile(`libs/${libName}/src/lib/button.cy.tsx`, (content) => { + return `import * as React from 'react'; +import Button from './button'; + +describe(Button.name, () => { + it('renders', () => { + cy.mount(