-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateEngine.test.js
More file actions
111 lines (97 loc) · 3.44 KB
/
templateEngine.test.js
File metadata and controls
111 lines (97 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {join} from 'path';
import {describe, expect, it} from 'vitest';
import {postProcessFile} from '../src/postProcess.js';
import {TemplateEngine} from '../src/templateEngine.js';
const TEMPLATES_DIR = join(process.cwd(), 'test', 'templates');
const createTestContext = (answers) => ({
projectName: answers.projectName,
language: answers.language,
framework: answers.framework,
isTypescript: answers.language === 'typescript',
isReact: answers.framework === 'react',
ext:
answers.language === 'typescript'
? answers.framework === 'react'
? 'tsx'
: 'ts'
: answers.framework === 'react'
? 'jsx'
: 'js',
...answers,
});
describe('TemplateEngine', () => {
it('should add imports to top of file', async () => {
const context = createTestContext({
projectName: 'test',
language: 'typescript',
framework: 'react',
});
const engine = new TemplateEngine(context, TEMPLATES_DIR);
const {content: processed} = await engine.processTemplate('imports.hbs');
const {content} = await postProcessFile('test.ts', processed, {
prettier: true,
});
expect(content).toMatchSnapshot();
});
it('should include entire files', async () => {
const context = createTestContext({
projectName: 'test',
language: 'typescript',
framework: 'react',
});
const engine = new TemplateEngine(context, TEMPLATES_DIR);
const {content: processed, includedFiles} =
await engine.processTemplate('main.hbs');
const {content} = await postProcessFile('test.ts', processed, {
prettier: true,
});
expect(content).toMatchSnapshot();
expect(includedFiles).toMatchSnapshot();
});
it('should handle lists with conditional items', async () => {
const context = createTestContext({
projectName: 'test',
language: 'typescript',
framework: 'react',
includeRouter: true,
includeRedux: false,
hasItem2: true,
});
const engine = new TemplateEngine(context, TEMPLATES_DIR);
const {content: processed} = await engine.processTemplate('list-block.hbs');
const {content} = await postProcessFile('test.js', processed, {
prettier: true,
});
expect(content).toContain("react: '^18.0.0',");
expect(content).toContain("'react-router': '^6.0.0',");
expect(content).toContain("lodash: '^4.17.21'");
expect(content).not.toContain('redux');
expect(content).toContain("'item1',");
expect(content).toContain("'item2',");
expect(content).toContain("'item3'");
expect(content).toMatchSnapshot();
});
it('should handle lists with all items excluded', async () => {
const context = createTestContext({
projectName: 'test',
language: 'typescript',
framework: 'react',
includeRouter: false,
includeRedux: false,
hasItem2: false,
});
const engine = new TemplateEngine(context, TEMPLATES_DIR);
const {content: processed} = await engine.processTemplate('list-block.hbs');
const {content} = await postProcessFile('test.js', processed, {
prettier: true,
});
expect(content).toContain("react: '^18.0.0',");
expect(content).not.toContain('react-router');
expect(content).toContain("lodash: '^4.17.21'");
expect(content).not.toContain('redux');
expect(content).toContain("'item1',");
expect(content).not.toContain("'item2'");
expect(content).toContain("'item3'");
expect(content).toMatchSnapshot();
});
});