import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing'; import { getProjects, NxJsonConfiguration, readJson, Tree } from '@nrwl/devkit'; import { applicationGenerator } from './application'; describe('app', () => { let tree: Tree; beforeEach(() => { tree = createTreeWithEmptyV1Workspace(); }); describe('not nested', () => { it('should update workspace.json and set defaultProject', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'css', standaloneConfig: false, }); const workspaceJson = readJson(tree, 'workspace.json'); expect(workspaceJson.projects['my-app'].root).toEqual('apps/my-app'); expect(workspaceJson.projects['my-app-e2e'].root).toEqual( 'apps/my-app-e2e' ); }); it('should update tags and implicit dependencies', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'css', tags: 'one,two', standaloneConfig: false, }); const projects = Object.fromEntries(getProjects(tree)); expect(projects).toMatchObject({ 'my-app': { tags: ['one', 'two'], }, 'my-app-e2e': { tags: [], implicitDependencies: ['my-app'], }, }); }); it('should generate files', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'css', standaloneConfig: false, }); expect(tree.exists('apps/my-app/tsconfig.json')).toBeTruthy(); expect(tree.exists('apps/my-app/pages/index.tsx')).toBeTruthy(); expect(tree.exists('apps/my-app/specs/index.spec.tsx')).toBeTruthy(); expect(tree.exists('apps/my-app/pages/index.module.css')).toBeTruthy(); }); it('should extend from root tsconfig.base.json', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'css', standaloneConfig: false, }); const tsConfig = readJson(tree, 'apps/my-app/tsconfig.json'); expect(tsConfig.extends).toBe('../../tsconfig.base.json'); }); it('should extend from root tsconfig.json when no tsconfig.base.json', async () => { tree.rename('tsconfig.base.json', 'tsconfig.json'); await applicationGenerator(tree, { name: 'myApp', style: 'css', standaloneConfig: false, }); const tsConfig = readJson(tree, 'apps/my-app/tsconfig.json'); expect(tsConfig.extends).toBe('../../tsconfig.json'); }); }); describe('--style scss', () => { it('should generate scss styles', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'scss', standaloneConfig: false, }); expect(tree.exists('apps/my-app/pages/index.module.scss')).toBeTruthy(); expect(tree.exists('apps/my-app/pages/styles.css')).toBeTruthy(); const indexContent = tree.read('apps/my-app/pages/index.tsx', 'utf-8'); expect(indexContent).toContain( `import styles from './index.module.scss'` ); }); }); describe('--style less', () => { it('should generate scss styles', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'less', standaloneConfig: false, }); expect(tree.exists('apps/my-app/pages/index.module.less')).toBeTruthy(); expect(tree.exists('apps/my-app/pages/styles.less')).toBeTruthy(); const indexContent = tree.read('apps/my-app/pages/index.tsx', 'utf-8'); expect(indexContent).toContain( `import styles from './index.module.less'` ); }); }); describe('--style styl', () => { it('should generate scss styles', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'styl', standaloneConfig: false, }); expect(tree.exists('apps/my-app/pages/index.module.styl')).toBeTruthy(); expect(tree.exists('apps/my-app/pages/styles.styl')).toBeTruthy(); const indexContent = tree.read('apps/my-app/pages/index.tsx', 'utf-8'); expect(indexContent).toContain( `import styles from './index.module.styl'` ); }); }); describe('--style styled-components', () => { it('should generate scss styles', async () => { await applicationGenerator(tree, { name: 'myApp', style: 'styled-components', standaloneConfig: false, }); expect( tree.exists('apps/my-app/pages/index.module.styled-components') ).toBeFalsy(); expect(tree.exists('apps/my-app/pages/styles.css')).toBeTruthy(); const indexContent = tree.read('apps/my-app/pages/index.tsx', 'utf-8'); expect(indexContent).not.toContain(`import styles from './index.module`); expect(indexContent).toContain(`import styled from 'styled-components'`); }); }); describe('--style @emotion/styled', () => { it('should generate scss styles', async () => { await applicationGenerator(tree, { name: 'myApp', style: '@emotion/styled', standaloneConfig: false, }); expect( tree.exists('apps/my-app/pages/index.module.styled-components') ).toBeFalsy(); expect(tree.exists('apps/my-app/pages/styles.css')).toBeTruthy(); const indexContent = tree.read('apps/my-app/pages/index.tsx', 'utf-8'); expect(indexContent).not.toContain(`import styles from './index.module`); expect(indexContent).toContain(`import styled from '@emotion/styled'`); }); it('should add jsxImportSource in tsconfig.json', async () => { await applicationGenerator(tree, { name: 'myApp', style: '@emotion/styled', standaloneConfig: false, }); const tsconfigJson = readJson(tree, 'apps/my-app/tsconfig.json'); expect(tsconfigJson.compilerOptions['jsxImportSource']).toEqual( '@emotion/react' ); }); }); describe('--style styled-jsx', () => { it('should use