|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import path from 'node:path'; |
| 8 | +import { expect } from 'chai'; |
| 9 | +import { TestSession, execCmd } from '@salesforce/cli-plugins-testkit'; |
| 10 | +import { nls } from '@salesforce/templates/lib/i18n/index.js'; |
| 11 | +import assert from 'yeoman-assert'; |
| 12 | + |
| 13 | +describe('Web application creation tests:', () => { |
| 14 | + let session: TestSession; |
| 15 | + before(async () => { |
| 16 | + session = await TestSession.create({ |
| 17 | + project: {}, |
| 18 | + devhubAuthStrategy: 'NONE', |
| 19 | + }); |
| 20 | + }); |
| 21 | + after(async () => { |
| 22 | + await session?.clean(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('Check webapp creation with default template', () => { |
| 26 | + it('should create webapp using default template in webApplications directory', () => { |
| 27 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 28 | + execCmd(`webapp generate --name MyWebApp --output-dir "${outputDir}"`, { ensureExitCode: 0 }); |
| 29 | + assert.file([ |
| 30 | + path.join(outputDir, 'MyWebApp', 'MyWebApp.webApplication-meta.xml'), |
| 31 | + path.join(outputDir, 'MyWebApp', 'index.html'), |
| 32 | + path.join(outputDir, 'MyWebApp', 'webapp.json'), |
| 33 | + ]); |
| 34 | + assert.fileContent(path.join(outputDir, 'MyWebApp', 'index.html'), '<title>My Web App</title>'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should default to project webApplications directory when --output-dir is omitted', () => { |
| 38 | + const expectedOutputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 39 | + execCmd('webapp generate --name DefaultDirApp', { ensureExitCode: 0 }); |
| 40 | + assert.file([ |
| 41 | + path.join(expectedOutputDir, 'DefaultDirApp', 'DefaultDirApp.webApplication-meta.xml'), |
| 42 | + path.join(expectedOutputDir, 'DefaultDirApp', 'index.html'), |
| 43 | + path.join(expectedOutputDir, 'DefaultDirApp', 'webapp.json'), |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should create webapp with custom label', () => { |
| 48 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 49 | + execCmd(`webapp generate --name TestApp --label "Custom Label" --output-dir "${outputDir}"`, { |
| 50 | + ensureExitCode: 0, |
| 51 | + }); |
| 52 | + assert.file([ |
| 53 | + path.join(outputDir, 'TestApp', 'TestApp.webApplication-meta.xml'), |
| 54 | + path.join(outputDir, 'TestApp', 'index.html'), |
| 55 | + ]); |
| 56 | + assert.fileContent(path.join(outputDir, 'TestApp', 'index.html'), '<title>Custom Label</title>'); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('Check webapp creation with reactbasic template', () => { |
| 61 | + it('should create React webapp with all required files', () => { |
| 62 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 63 | + execCmd(`webapp generate --name MyReactApp --template reactbasic --output-dir "${outputDir}"`, { |
| 64 | + ensureExitCode: 0, |
| 65 | + }); |
| 66 | + assert.file([ |
| 67 | + path.join(outputDir, 'MyReactApp', 'MyReactApp.webApplication-meta.xml'), |
| 68 | + path.join(outputDir, 'MyReactApp', 'index.html'), |
| 69 | + path.join(outputDir, 'MyReactApp', 'webapp.json'), |
| 70 | + path.join(outputDir, 'MyReactApp', 'package.json'), |
| 71 | + path.join(outputDir, 'MyReactApp', 'vite.config.ts'), |
| 72 | + path.join(outputDir, 'MyReactApp', 'tsconfig.json'), |
| 73 | + path.join(outputDir, 'MyReactApp', 'tsconfig.node.json'), |
| 74 | + path.join(outputDir, 'MyReactApp', 'tailwind.config.js'), |
| 75 | + path.join(outputDir, 'MyReactApp', 'postcss.config.js'), |
| 76 | + path.join(outputDir, 'MyReactApp', 'src', 'main.tsx'), |
| 77 | + path.join(outputDir, 'MyReactApp', 'src', 'App.tsx'), |
| 78 | + path.join(outputDir, 'MyReactApp', 'src', 'routes.ts'), |
| 79 | + path.join(outputDir, 'MyReactApp', 'src', 'vite-env.d.ts'), |
| 80 | + path.join(outputDir, 'MyReactApp', 'src', 'components', 'Navigation.tsx'), |
| 81 | + path.join(outputDir, 'MyReactApp', 'src', 'pages', 'Home.tsx'), |
| 82 | + path.join(outputDir, 'MyReactApp', 'src', 'pages', 'About.tsx'), |
| 83 | + path.join(outputDir, 'MyReactApp', 'src', 'pages', 'NotFound.tsx'), |
| 84 | + path.join(outputDir, 'MyReactApp', 'src', 'styles', 'global.css'), |
| 85 | + path.join(outputDir, 'MyReactApp', 'src', 'test-setup', 'setup.ts'), |
| 86 | + ]); |
| 87 | + assert.fileContent(path.join(outputDir, 'MyReactApp', 'package.json'), '"name": "MyReactApp"'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('Check that all invalid name errors are thrown', () => { |
| 92 | + it('should throw a missing name error', () => { |
| 93 | + const stderr = execCmd('webapp generate').shellOutput.stderr; |
| 94 | + expect(stderr).to.contain('Missing required flag'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should throw invalid non alphanumeric webapp name error', () => { |
| 98 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 99 | + const stderr = execCmd(`webapp generate --name /a --output-dir "${outputDir}"`).shellOutput.stderr; |
| 100 | + expect(stderr).to.contain(nls.localize('AlphaNumericNameError')); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should throw invalid webapp name starting with numeric error', () => { |
| 104 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 105 | + const stderr = execCmd(`webapp generate --name 3aa --output-dir "${outputDir}"`).shellOutput.stderr; |
| 106 | + expect(stderr).to.contain(nls.localize('NameMustStartWithLetterError')); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should throw invalid webapp name ending with underscore error', () => { |
| 110 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 111 | + const stderr = execCmd(`webapp generate --name a_ --output-dir "${outputDir}"`).shellOutput.stderr; |
| 112 | + expect(stderr).to.contain(nls.localize('EndWithUnderscoreError')); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should throw invalid webapp name with double underscore error', () => { |
| 116 | + const outputDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'webApplications'); |
| 117 | + const stderr = execCmd(`webapp generate --name a__a --output-dir "${outputDir}"`).shellOutput.stderr; |
| 118 | + expect(stderr).to.contain(nls.localize('DoubleUnderscoreError')); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should throw error when output dir is not webApplications folder', () => { |
| 122 | + const stderr = execCmd('webapp generate --name TestApp --output-dir /tmp/invalid').shellOutput.stderr; |
| 123 | + expect(stderr).to.contain(nls.localize('MissingWebApplicationsDir')); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments