Skip to content

Commit

Permalink
[#121] Add test case for testing separate strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
bterone committed Sep 29, 2023
1 parent 50c031e commit 458ea7a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/cli-tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ To run the CLI on your local machine:

> 💡 Running just `./bin/dev` without argument will display all the possible commands as well as additional information.
To test with local changes in either the `./packages/cra-template` or the `./vite-temaplte/` folders, use the following commands:
To test with local changes in either the `./packages/cra-template` or the `./vite-template/` folders, use the following commands:
- For Vite:
```BASH
# Assuming the repository `react-temapltes` is in `~/Documents/Source/`.
# Assuming the repository `react-templates` is in `~/Documents/Source/`.
# The generated app will be in `~/Documents/Source/vite-app`
./bin/dev generate vite-app ~/Documents/Source/ feature/gh88-replace-webpack-with-vite
```
- For Create React App (Webpack):
```BASH
# Assuming the repository `react-temapltes` is in `~/Documents/Source/`.
# Assuming the repository `react-templates` is in `~/Documents/Source/`.
# The generated app will be in `~/Documents/Source/webpack-app`
./bin/dev generate webpack-app ~/Documents/Source/ file:react-templates/packages/cra-template
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { InitTemplateOptions } from '../.';
import runCommand from '../../helpers/child-process';
import { FetchStrategy } from './';

const DEFAULT_TEMPLATE_REFERENCE = '../vite-template';
const TEMPLATE_SOURCE_FILES = '../vite-template';

class CopyStrategy implements FetchStrategy {
async fetchTemplateFiles(options: InitTemplateOptions): Promise<void> {
Expand All @@ -17,7 +17,7 @@ class CopyStrategy implements FetchStrategy {

return runCommand(
'cp',
['-r', DEFAULT_TEMPLATE_REFERENCE, options.dest],
['-r', TEMPLATE_SOURCE_FILES, options.dest],
);
}

Expand Down
13 changes: 9 additions & 4 deletions packages/cli-tool/src/template/initialize-vite-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { CliUx } from '@oclif/core';
import { InitTemplateOptions } from '.';
import runCommand from '../helpers/child-process';
import { replaceLine } from '../helpers/file-editor';
import { CopyStrategy } from './fetchingStrategy';
import { CopyStrategy, DownloadStrategy } from './fetchingStrategy';

const replaceAppNameInFiles = ['package.json', 'index.html'];

const fetchTemplateFiles = (options: InitTemplateOptions): Promise<void> => {
// If given a branch name, use
// const fetchStrategy = new DownloadStrategy();
const fetchStrategy = new CopyStrategy();
let fetchStrategy: CopyStrategy | DownloadStrategy;

// If passed templateReference in CLI, use the DownloadStrategy
if (options.templateReference && options.templateReference.trim() === '') {
fetchStrategy = new DownloadStrategy();
} else {
fetchStrategy = new CopyStrategy();
}

return fetchStrategy.fetchTemplateFiles(options);
};
Expand Down
32 changes: 30 additions & 2 deletions packages/cli-tool/test/commands/generate/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { gitHubTestData, gitLabTestData, noVersionControlTestData } from '../../
import { TestScenario } from '../../helpers/test-scenario';

const craTemplateReference = 'file:./react-templates/packages/cra-template';
const viteTemplateReference = '../vite-template';
// TODO: Adjust viteTemplateReference to use commit hash of development branch for vite template
// https://github.com/nimblehq/react-templates/commit/52288d1e5e560bcc717f760f1fd19f7cb1b0085e
const viteTemplateReference = '52288d1e5e560bcc717f760f1fd19f7cb1b0085e';
const projectName = 'test-app';
const testFolderPath = '../../../';

Expand All @@ -21,6 +23,7 @@ const viteTestScenarios: TestScenario[] = [
versionControl: 'github',
uiFramework: 'bootstrap',
},
templateReference: viteTemplateReference,
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
Expand All @@ -42,6 +45,7 @@ const viteTestScenarios: TestScenario[] = [
versionControl: 'gitlab',
uiFramework: 'tailwindCss',
},
templateReference: viteTemplateReference,
testData: {
filesShouldExist: [
...gitLabTestData.filesShouldExist,
Expand All @@ -57,6 +61,28 @@ const viteTestScenarios: TestScenario[] = [
],
},
},
{
options: {
template: 'vite',
versionControl: 'github',
uiFramework: 'bootstrap',
},
templateReference: '',
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
...bootstrapTestData.filesShouldExist,
],
filesShouldNotExist: [
...gitHubTestData.filesShouldNotExist,
...bootstrapTestData.filesShouldNotExist,
],
filesShouldContain: [
...gitHubTestData.filesShouldContain,
...bootstrapTestData.filesShouldContain,
],
},
},
];
const craTestScenarios: TestScenario[] = [
{
Expand All @@ -65,6 +91,7 @@ const craTestScenarios: TestScenario[] = [
versionControl: 'github',
uiFramework: 'bootstrap',
},
templateReference: craTemplateReference,
testData: {
filesShouldExist: [
...gitHubTestData.filesShouldExist,
Expand All @@ -86,6 +113,7 @@ const craTestScenarios: TestScenario[] = [
versionControl: 'none',
uiFramework: 'tailwindCss',
},
templateReference: craTemplateReference,
testData: {
filesShouldExist: [
...noVersionControlTestData.filesShouldExist,
Expand Down Expand Up @@ -126,7 +154,7 @@ describe('generate', () => {
test
.stdout()
.stub(Inquirer, 'prompt', () => scenario.options)
.command(['generate', `${projectName}`, testFolderPath, scenario.options.template === 'vite' ? viteTemplateReference : craTemplateReference])
.command(['generate', `${projectName}`, testFolderPath, scenario.templateReference])
.it(
`generates a ${scenario.options.template} app ${projectName} with ${scenario.options.versionControl} and ${scenario.options.uiFramework}`,
(ctx) => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli-tool/test/helpers/test-scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import { TestData } from './test-data';
* into a single generate command execution */
export type TestScenario = {
options: any; // eslint-disable-line @typescript-eslint/no-explicit-any
templateReference: string;
testData: TestData;
};

0 comments on commit 458ea7a

Please sign in to comment.