Skip to content

Commit 5711343

Browse files
committed
chore: more tests
1 parent 5a7b91e commit 5711343

File tree

11 files changed

+371
-75
lines changed

11 files changed

+371
-75
lines changed

packages/cta-cli/tests/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, vi } from 'vitest'
1+
import { describe, it, expect } from 'vitest'
22

33
import { cli } from '../src/index.js'
44

packages/cta-cli/tests/ui-environment.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, it, expect, vi } from 'vitest'
1+
import { describe, it, expect, vi } from 'vitest'
22

33
import * as clack from '@clack/prompts'
44

packages/cta-engine/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@vitest/coverage-v8": "3.1.1",
4747
"eslint": "^9.20.0",
4848
"typescript": "^5.6.3",
49-
"vitest": "^3.0.8"
49+
"vitest": "^3.0.8",
50+
"vitest-fetch-mock": "^0.4.5"
5051
}
5152
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { fs, vol } from 'memfs'
3+
4+
import {
5+
compareFilesRecursively,
6+
createAppOptionsFromPersisted,
7+
createPackageAdditions,
8+
createSerializedOptionsFromPersisted,
9+
readCurrentProjectOptions,
10+
} from '../../src/custom-add-ons/shared.js'
11+
import { createMemoryEnvironment } from '../../src/environment.js'
12+
import {
13+
__testClearFrameworks,
14+
__testRegisterFramework,
15+
} from '../../src/frameworks.js'
16+
17+
import type { PersistedOptions } from '../../src/config-file.js'
18+
19+
vi.mock('node:fs', () => fs)
20+
vi.mock('node:fs/promises', () => fs.promises)
21+
22+
beforeEach(() => {
23+
vol.reset()
24+
25+
const fakeFiles = {
26+
'./package.json': JSON.stringify({
27+
name: 'test',
28+
version: '1.0.0',
29+
dependencies: {},
30+
}),
31+
}
32+
33+
__testClearFrameworks()
34+
__testRegisterFramework({
35+
id: 'test',
36+
name: 'Test',
37+
description: 'Test',
38+
version: '1.0.0',
39+
baseDirectory: '/foo',
40+
addOnsDirectories: [],
41+
examplesDirectory: '',
42+
basePackageJSON: {},
43+
optionalPackages: {},
44+
getAddOns: () => [
45+
{
46+
id: 'test',
47+
name: 'Test',
48+
description: 'Test',
49+
version: '1.0.0',
50+
type: 'add-on',
51+
phase: 'add-on',
52+
modes: ['code-router', 'file-router'],
53+
command: {
54+
command: 'echo',
55+
args: ['baz'],
56+
},
57+
packageAdditions: {
58+
dependencies: {
59+
'test-package': '1.0.0',
60+
},
61+
},
62+
dependsOn: [],
63+
getFiles: () => Promise.resolve(['./jack.txt']),
64+
getFileContents: () => Promise.resolve('foo'),
65+
getDeletedFiles: () => Promise.resolve([]),
66+
},
67+
],
68+
getFiles: () => Promise.resolve(Object.keys(fakeFiles)),
69+
getFileContents: (path) => Promise.resolve(fakeFiles[path]),
70+
getDeletedFiles: () => Promise.resolve([]),
71+
})
72+
})
73+
74+
describe('createAppOptionsFromPersisted', () => {
75+
it('should create live options from persisted options', async () => {
76+
const persistedOptions = {
77+
projectName: 'test-project',
78+
framework: 'test',
79+
mode: 'code-router',
80+
typescript: true,
81+
tailwind: true,
82+
git: true,
83+
packageManager: 'npm',
84+
targetDir: '',
85+
starter: undefined,
86+
chosenAddOns: [],
87+
existingAddOns: [],
88+
version: 1,
89+
} as PersistedOptions
90+
const appOptions = await createAppOptionsFromPersisted(persistedOptions)
91+
expect(appOptions.framework.id).toEqual('test')
92+
expect(appOptions.mode).toEqual('code-router')
93+
expect(appOptions.typescript).toEqual(true)
94+
expect(appOptions.tailwind).toEqual(true)
95+
expect(appOptions.git).toEqual(true)
96+
expect(appOptions.packageManager).toEqual('npm')
97+
expect(appOptions.targetDir).toEqual('')
98+
expect(appOptions.starter).toEqual(undefined)
99+
expect(appOptions.chosenAddOns).toEqual([])
100+
expect(appOptions.chosenAddOns).toEqual([])
101+
})
102+
})
103+
104+
describe('createSerializedOptionsFromPersisted', () => {
105+
it('should create serialized options from persisted options', async () => {
106+
const persistedOptions = {
107+
projectName: 'test-project',
108+
framework: 'test',
109+
mode: 'code-router',
110+
typescript: true,
111+
tailwind: true,
112+
git: true,
113+
packageManager: 'npm',
114+
targetDir: '',
115+
starter: undefined,
116+
chosenAddOns: [],
117+
existingAddOns: [],
118+
version: 1,
119+
} as PersistedOptions
120+
const appOptions =
121+
await createSerializedOptionsFromPersisted(persistedOptions)
122+
expect(appOptions.framework).toEqual('test')
123+
expect(appOptions.mode).toEqual('code-router')
124+
expect(appOptions.typescript).toEqual(true)
125+
expect(appOptions.tailwind).toEqual(true)
126+
expect(appOptions.git).toEqual(true)
127+
expect(appOptions.packageManager).toEqual('npm')
128+
expect(appOptions.targetDir).toEqual('')
129+
expect(appOptions.starter).toEqual(undefined)
130+
expect(appOptions.chosenAddOns).toEqual([])
131+
expect(appOptions.chosenAddOns).toEqual([])
132+
})
133+
})
134+
135+
describe('createPackageAdditions', () => {
136+
it('should handles scripts', () => {
137+
const packageAdditions = createPackageAdditions(
138+
{
139+
scripts: {
140+
dev: 'vinxi dev',
141+
},
142+
},
143+
{
144+
scripts: {
145+
dev: 'vinxi dev',
146+
foo: 'bar',
147+
},
148+
},
149+
)
150+
expect(packageAdditions).toEqual({
151+
scripts: {
152+
foo: 'bar',
153+
},
154+
})
155+
})
156+
157+
it('should handles dependencies', () => {
158+
const packageAdditions = createPackageAdditions(
159+
{
160+
dependencies: {
161+
react: '^18.0.0',
162+
},
163+
devDependencies: {
164+
'foo-dev-dependency': '^18.0.0',
165+
'updated-dev-dependency': '^18.0.0',
166+
},
167+
},
168+
{
169+
dependencies: {
170+
react: '^18.0.0',
171+
'react-dom': '^20.0.0',
172+
},
173+
devDependencies: {
174+
'foo-dev-dependency': '^18.0.0',
175+
'bar-dev-dependency': '^18.0.0',
176+
'updated-dev-dependency': '^20.0.0',
177+
},
178+
},
179+
)
180+
expect(packageAdditions).toEqual({
181+
dependencies: {
182+
'react-dom': '^20.0.0',
183+
},
184+
devDependencies: {
185+
'bar-dev-dependency': '^18.0.0',
186+
'updated-dev-dependency': '^20.0.0',
187+
},
188+
})
189+
})
190+
})
191+
192+
describe('readCurrentProjectOptions', () => {
193+
it('should read the current project options', async () => {
194+
const { environment } = createMemoryEnvironment()
195+
environment.writeFile(
196+
'.cta.json',
197+
JSON.stringify({
198+
projectName: 'test-project',
199+
framework: 'test',
200+
mode: 'code-router',
201+
typescript: true,
202+
tailwind: true,
203+
git: true,
204+
packageManager: 'npm',
205+
targetDir: '',
206+
starter: undefined,
207+
chosenAddOns: [],
208+
existingAddOns: [],
209+
version: 1,
210+
}),
211+
)
212+
const options = await readCurrentProjectOptions(environment)
213+
expect(options).toEqual({
214+
chosenAddOns: [],
215+
existingAddOns: [],
216+
framework: 'test',
217+
git: true,
218+
mode: 'code-router',
219+
packageManager: 'npm',
220+
projectName: 'test-project',
221+
tailwind: true,
222+
targetDir: '',
223+
typescript: true,
224+
version: 1,
225+
})
226+
})
227+
})
228+
229+
describe('compareFilesRecursively', () => {
230+
it('should compare files recursively', async () => {
231+
vol.mkdirSync('/foo')
232+
vol.writeFileSync('/foo/.gitignore', 'foo')
233+
vol.writeFileSync('/foo/bar.txt', 'bar')
234+
vol.writeFileSync('/foo/baz.txt', 'baz')
235+
vol.writeFileSync('/foo/qux.txt', 'qux')
236+
vol.mkdirSync('/foo/bar')
237+
vol.writeFileSync('/foo/bar/baz.txt', 'baz')
238+
vol.writeFileSync('/foo/bar/qux.txt', 'qux')
239+
const changedFiles = {}
240+
await compareFilesRecursively(
241+
'/foo',
242+
() => false,
243+
{
244+
'/foo/.gitignore': '.gitignore',
245+
'/foo/bar.txt': 'bar',
246+
},
247+
changedFiles,
248+
)
249+
expect(changedFiles).toEqual({
250+
'/foo/.gitignore': 'foo',
251+
'/foo/bar/baz.txt': 'baz',
252+
'/foo/bar/qux.txt': 'qux',
253+
'/foo/baz.txt': 'baz',
254+
'/foo/qux.txt': 'qux',
255+
})
256+
})
257+
})

packages/cta-engine/tests/custom-add-ons/shared.ts

-72
This file was deleted.

0 commit comments

Comments
 (0)