-
-
Notifications
You must be signed in to change notification settings - Fork 792
/
Copy pathcomplete.spec.ts
63 lines (55 loc) · 2.17 KB
/
complete.spec.ts
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
import { vi, test, expect, beforeEach, afterEach, SpyInstance } from 'vitest'
import { context } from '../test/helpers'
import complete from './complete'
import { Context } from './types'
let log: SpyInstance<[message?: any, ...optionalParams: any[]], void>
beforeEach(async () => {
log = vi.spyOn(console, 'log')
})
afterEach(async () => {
log.mockRestore()
})
test('unit:complete:fallback', async () => {
const ctx = context({
template: 'fallback',
project: 'fallback-app',
files: [
{ path: 'foo.txt', contents: Buffer.from('') },
{ path: 'foo/bar.txt', contents: Buffer.from('') },
{ path: 'bar.txt', contents: Buffer.from('') }
]
})
await complete(ctx)
expect(log.mock.calls[0][0]).toBe('Created a new project in `fallback-app` by the `fallback` template.\n')
expect(log.mock.calls[1][0]).toBe('- bar.txt')
expect(log.mock.calls[2][0]).toBe('- foo.txt')
expect(log.mock.calls[3][0]).toBe('- foo/bar.txt')
expect(log.mock.calls[4][0]).toBe('\nHappy hacking :)')
})
test('unit:complete:string', async () => {
const ctx = context({}, { complete: 'completed' })
await complete(ctx)
expect(log.mock.calls[0][0]).toBe('completed')
})
test('unit:complete:callback', async () => {
const callback = vi.fn<[ctx: Context], string | Promise<string> | Promise<void>>()
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback.mock.calls[0][0]).toBe(ctx)
})
test('unit:complete:callback-return', async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-parens
const callback = vi.fn<[ctx: Context], string | Promise<string> | Promise<void>>(() => 'completed')
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback).toHaveBeenCalled()
expect(log.mock.calls[0][0]).toBe('completed')
})
test('unit:complete:callback-promise', async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-parens
const callback = vi.fn<[ctx: Context], string | Promise<string> | Promise<void>>(async () => 'completed')
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback).toHaveBeenCalled()
expect(log.mock.calls[0][0]).toBe('completed')
})