-
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathmake_provider.spec.ts
160 lines (128 loc) · 5.13 KB
/
make_provider.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { test } from '@japa/runner'
import { AceFactory } from '../../factories/core/ace.js'
import MakeProvider from '../../commands/make/provider.js'
import { StubsFactory } from '../../factories/stubs.js'
test.group('Make provider', () => {
test('create provider class', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')
const command = await ace.create(MakeProvider, ['app'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})
await assert.fileEquals('providers/app_provider.ts', contents)
assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) update adonisrc.ts file',
stream: 'stdout',
},
])
await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
})
test('do not display prompt when --register flag is used', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')
const command = await ace.create(MakeProvider, ['app', '--register'])
await command.exec()
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})
await assert.fileEquals('providers/app_provider.ts', contents)
assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) update adonisrc.ts file',
stream: 'stdout',
},
])
await assert.fileContains('adonisrc.ts', `() => import('#providers/app_provider')`)
})
test('do not register provider when --no-register flag is used', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')
const command = await ace.create(MakeProvider, ['app', '--no-register'])
await command.exec()
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})
await assert.fileEquals('providers/app_provider.ts', contents)
assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
])
await assert.fileEquals('adonisrc.ts', `export default defineConfig({})`)
})
test('create provider class for a specific environment', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')
const command = await ace.create(MakeProvider, ['app', '-e=web', '-e=repl'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()
const { contents } = await new StubsFactory().prepare('make/provider/main.stub', {
entity: ace.app.generators.createEntity('app'),
})
assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create providers/app_provider.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) update adonisrc.ts file',
stream: 'stdout',
},
])
await assert.fileEquals('providers/app_provider.ts', contents)
await assert.fileContains('adonisrc.ts', [
`() => import('#providers/app_provider')`,
`environment: ['web', 'repl']`,
])
})
test('show error when selected environment is invalid', async ({ assert, fs }) => {
await fs.createJson('tsconfig.json', {})
await fs.create('adonisrc.ts', `export default defineConfig({})`)
const ace = await new AceFactory().make(fs.baseUrl)
await ace.app.init()
ace.ui.switchMode('raw')
const command = await ace.create(MakeProvider, ['app', '--environments=foo'])
command.prompt.trap('Do you want to register the provider in .adonisrc.ts file?').accept()
await command.exec()
assert.deepEqual(ace.ui.logger.getLogs(), [
{
message:
'[ red(error) ] Invalid environment(s) "foo". Only "web,console,test,repl" are allowed',
stream: 'stderr',
},
])
})
})