Skip to content

Commit 458ea7a

Browse files
committed
[#121] Add test case for testing separate strategies
1 parent 50c031e commit 458ea7a

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

packages/cli-tool/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ To run the CLI on your local machine:
9191

9292
> 💡 Running just `./bin/dev` without argument will display all the possible commands as well as additional information.
9393
94-
To test with local changes in either the `./packages/cra-template` or the `./vite-temaplte/` folders, use the following commands:
94+
To test with local changes in either the `./packages/cra-template` or the `./vite-template/` folders, use the following commands:
9595
- For Vite:
9696
```BASH
97-
# Assuming the repository `react-temapltes` is in `~/Documents/Source/`.
97+
# Assuming the repository `react-templates` is in `~/Documents/Source/`.
9898
# The generated app will be in `~/Documents/Source/vite-app`
9999
./bin/dev generate vite-app ~/Documents/Source/ feature/gh88-replace-webpack-with-vite
100100
```
101101
- For Create React App (Webpack):
102102
```BASH
103-
# Assuming the repository `react-temapltes` is in `~/Documents/Source/`.
103+
# Assuming the repository `react-templates` is in `~/Documents/Source/`.
104104
# The generated app will be in `~/Documents/Source/webpack-app`
105105
./bin/dev generate webpack-app ~/Documents/Source/ file:react-templates/packages/cra-template
106106
```

packages/cli-tool/src/template/fetchingStrategy/copyStrategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { InitTemplateOptions } from '../.';
44
import runCommand from '../../helpers/child-process';
55
import { FetchStrategy } from './';
66

7-
const DEFAULT_TEMPLATE_REFERENCE = '../vite-template';
7+
const TEMPLATE_SOURCE_FILES = '../vite-template';
88

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

1818
return runCommand(
1919
'cp',
20-
['-r', DEFAULT_TEMPLATE_REFERENCE, options.dest],
20+
['-r', TEMPLATE_SOURCE_FILES, options.dest],
2121
);
2222
}
2323

packages/cli-tool/src/template/initialize-vite-app.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import { CliUx } from '@oclif/core';
33
import { InitTemplateOptions } from '.';
44
import runCommand from '../helpers/child-process';
55
import { replaceLine } from '../helpers/file-editor';
6-
import { CopyStrategy } from './fetchingStrategy';
6+
import { CopyStrategy, DownloadStrategy } from './fetchingStrategy';
77

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

1010
const fetchTemplateFiles = (options: InitTemplateOptions): Promise<void> => {
11-
// If given a branch name, use
12-
// const fetchStrategy = new DownloadStrategy();
13-
const fetchStrategy = new CopyStrategy();
11+
let fetchStrategy: CopyStrategy | DownloadStrategy;
12+
13+
// If passed templateReference in CLI, use the DownloadStrategy
14+
if (options.templateReference && options.templateReference.trim() === '') {
15+
fetchStrategy = new DownloadStrategy();
16+
} else {
17+
fetchStrategy = new CopyStrategy();
18+
}
1419

1520
return fetchStrategy.fetchTemplateFiles(options);
1621
};

packages/cli-tool/test/commands/generate/index.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import { gitHubTestData, gitLabTestData, noVersionControlTestData } from '../../
99
import { TestScenario } from '../../helpers/test-scenario';
1010

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

@@ -21,6 +23,7 @@ const viteTestScenarios: TestScenario[] = [
2123
versionControl: 'github',
2224
uiFramework: 'bootstrap',
2325
},
26+
templateReference: viteTemplateReference,
2427
testData: {
2528
filesShouldExist: [
2629
...gitHubTestData.filesShouldExist,
@@ -42,6 +45,7 @@ const viteTestScenarios: TestScenario[] = [
4245
versionControl: 'gitlab',
4346
uiFramework: 'tailwindCss',
4447
},
48+
templateReference: viteTemplateReference,
4549
testData: {
4650
filesShouldExist: [
4751
...gitLabTestData.filesShouldExist,
@@ -57,6 +61,28 @@ const viteTestScenarios: TestScenario[] = [
5761
],
5862
},
5963
},
64+
{
65+
options: {
66+
template: 'vite',
67+
versionControl: 'github',
68+
uiFramework: 'bootstrap',
69+
},
70+
templateReference: '',
71+
testData: {
72+
filesShouldExist: [
73+
...gitHubTestData.filesShouldExist,
74+
...bootstrapTestData.filesShouldExist,
75+
],
76+
filesShouldNotExist: [
77+
...gitHubTestData.filesShouldNotExist,
78+
...bootstrapTestData.filesShouldNotExist,
79+
],
80+
filesShouldContain: [
81+
...gitHubTestData.filesShouldContain,
82+
...bootstrapTestData.filesShouldContain,
83+
],
84+
},
85+
},
6086
];
6187
const craTestScenarios: TestScenario[] = [
6288
{
@@ -65,6 +91,7 @@ const craTestScenarios: TestScenario[] = [
6591
versionControl: 'github',
6692
uiFramework: 'bootstrap',
6793
},
94+
templateReference: craTemplateReference,
6895
testData: {
6996
filesShouldExist: [
7097
...gitHubTestData.filesShouldExist,
@@ -86,6 +113,7 @@ const craTestScenarios: TestScenario[] = [
86113
versionControl: 'none',
87114
uiFramework: 'tailwindCss',
88115
},
116+
templateReference: craTemplateReference,
89117
testData: {
90118
filesShouldExist: [
91119
...noVersionControlTestData.filesShouldExist,
@@ -126,7 +154,7 @@ describe('generate', () => {
126154
test
127155
.stdout()
128156
.stub(Inquirer, 'prompt', () => scenario.options)
129-
.command(['generate', `${projectName}`, testFolderPath, scenario.options.template === 'vite' ? viteTemplateReference : craTemplateReference])
157+
.command(['generate', `${projectName}`, testFolderPath, scenario.templateReference])
130158
.it(
131159
`generates a ${scenario.options.template} app ${projectName} with ${scenario.options.versionControl} and ${scenario.options.uiFramework}`,
132160
(ctx) => {

packages/cli-tool/test/helpers/test-scenario.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import { TestData } from './test-data';
44
* into a single generate command execution */
55
export type TestScenario = {
66
options: any; // eslint-disable-line @typescript-eslint/no-explicit-any
7+
templateReference: string;
78
testData: TestData;
89
};

0 commit comments

Comments
 (0)