1
1
import { beforeEach , describe , expect , it } from "vitest" ;
2
- import { run as addCommand , args } from "../../src/commands/add.js" ;
2
+ import * as add from "../../src/commands/add.js" ;
3
3
import { GADGET_GLOBAL_ACTIONS_QUERY , GADGET_META_MODELS_QUERY } from "../../src/services/app/api/operation.js" ;
4
4
import {
5
5
CREATE_ACTION_MUTATION ,
8
8
CREATE_ROUTE_MUTATION ,
9
9
} from "../../src/services/app/edit/operation.js" ;
10
10
import { ArgError } from "../../src/services/command/arg.js" ;
11
- import { makeContext } from "../__support__/context.js" ;
11
+ import { makeArgs } from "../__support__/arg.js" ;
12
+ import { testCtx } from "../__support__/context.js" ;
12
13
import { expectError } from "../__support__/error.js" ;
13
14
import { makeSyncScenario } from "../__support__/filesync.js" ;
14
15
import { nockApiResponse , nockEditResponse } from "../__support__/graphql.js" ;
@@ -28,9 +29,7 @@ describe("add", () => {
28
29
expectVariables : { path : "modelA" , fields : [ ] } ,
29
30
} ) ;
30
31
31
- const ctx = makeContext ( { parse : args , argv : [ "add" , "model" , "modelA" ] } ) ;
32
-
33
- await addCommand ( ctx ) ;
32
+ await add . run ( testCtx , makeArgs ( add . args , "add" , "model" , "modelA" ) ) ;
34
33
} ) ;
35
34
36
35
it ( "can add a model with fields" , async ( ) => {
@@ -46,15 +45,11 @@ describe("add", () => {
46
45
} ,
47
46
} ) ;
48
47
49
- const ctx = makeContext ( { parse : args , argv : [ "add" , "model" , "modelA" , "newField:string" , "newField2:boolean" ] } ) ;
50
-
51
- await addCommand ( ctx ) ;
48
+ await add . run ( testCtx , makeArgs ( add . args , "add" , "model" , "modelA" , "newField:string" , "newField2:boolean" ) ) ;
52
49
} ) ;
53
50
54
51
it ( "requires a model path" , async ( ) => {
55
- const ctx = makeContext ( { parse : args , argv : [ "add" , "model" ] } ) ;
56
-
57
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
52
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "model" ) ) ) ;
58
53
expect ( error ) . toBeInstanceOf ( ArgError ) ;
59
54
expect ( error . sprint ( ) ) . toMatchInlineSnapshot ( `
60
55
"✘ Failed to add model, missing model path
@@ -65,9 +60,7 @@ describe("add", () => {
65
60
} ) ;
66
61
67
62
it . each ( [ "field;string" , "field:" , ":" , "" ] ) ( 'returns ArgErrors when field argument is "%s"' , async ( invalidFieldArgument ) => {
68
- const ctx = makeContext ( { parse : args , argv : [ "add" , "model" , "newModel" , invalidFieldArgument ] } ) ;
69
-
70
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
63
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "model" , "modelA" , invalidFieldArgument ) ) ) ;
71
64
expect ( error ) . toBeInstanceOf ( ArgError ) ;
72
65
expect ( error . sprint ( ) ) . toContain ( "is not a valid field definition" ) ;
73
66
} ) ;
@@ -84,15 +77,11 @@ describe("add", () => {
84
77
} ,
85
78
} ) ;
86
79
87
- const ctx = makeContext ( { parse : args , argv : [ "add" , "field" , "modelA/newField:string" ] } ) ;
88
-
89
- await addCommand ( ctx ) ;
80
+ await add . run ( testCtx , makeArgs ( add . args , "add" , "field" , "modelA/newField:string" ) ) ;
90
81
} ) ;
91
82
92
83
it ( "returns an ArgError if there's no input" , async ( ) => {
93
- const ctx = makeContext ( { parse : args , argv : [ "add" , "field" ] } ) ;
94
-
95
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
84
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "field" ) ) ) ;
96
85
expect ( error ) . toBeInstanceOf ( ArgError ) ;
97
86
expect ( error . sprint ( ) ) . toMatchInlineSnapshot ( `
98
87
"✘ Failed to add field, invalid field path definition
@@ -103,17 +92,13 @@ describe("add", () => {
103
92
} ) ;
104
93
105
94
it . each ( [ "user" , "user/" ] ) ( "returns missing field definition ArgError if the input is %s" , async ( partialInput ) => {
106
- const ctx = makeContext ( { parse : args , argv : [ "add" , "field" , partialInput ] } ) ;
107
-
108
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
95
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "field" , partialInput ) ) ) ;
109
96
expect ( error ) . toBeInstanceOf ( ArgError ) ;
110
97
expect ( error . sprint ( ) ) . toContain ( "Failed to add field, invalid field definition" ) ;
111
98
} ) ;
112
99
113
100
it . each ( [ "user/field" , "user/field:" , "user/:" ] ) ( "returns missing field type ArgError if the input is %s" , async ( partialInput ) => {
114
- const ctx = makeContext ( { parse : args , argv : [ "add" , "field" , partialInput ] } ) ;
115
-
116
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
101
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "field" , partialInput ) ) ) ;
117
102
expect ( error ) . toBeInstanceOf ( ArgError ) ;
118
103
expect ( error . sprint ( ) ) . toContain ( "is not a valid field definition" ) ;
119
104
} ) ;
@@ -167,15 +152,11 @@ describe("add", () => {
167
152
expectVariables : { path : "actionA" } ,
168
153
} ) ;
169
154
170
- const ctx = makeContext ( { parse : args , argv : [ "add" , "action" , "actionA" ] } ) ;
171
-
172
- await addCommand ( ctx ) ;
155
+ await add . run ( testCtx , makeArgs ( add . args , "add" , "action" , "actionA" ) ) ;
173
156
} ) ;
174
157
175
158
it ( "requires an action name/path" , async ( ) => {
176
- const ctx = makeContext ( { parse : args , argv : [ "add" , "action" ] } ) ;
177
-
178
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
159
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "action" ) ) ) ;
179
160
expect ( error ) . toBeInstanceOf ( ArgError ) ;
180
161
expect ( error . sprint ( ) ) . toMatchInlineSnapshot ( `
181
162
"✘ Failed to add action, missing action path
@@ -195,15 +176,11 @@ describe("add", () => {
195
176
expectVariables : { method : "GET" , path : "routeA" } ,
196
177
} ) ;
197
178
198
- const ctx = makeContext ( { parse : args , argv : [ "add" , "route" , "GET" , "routeA" ] } ) ;
199
-
200
- await addCommand ( ctx ) ;
179
+ await add . run ( testCtx , makeArgs ( add . args , "add" , "route" , "GET" , "routeA" ) ) ;
201
180
} ) ;
202
181
203
182
it ( "requires a method argument" , async ( ) => {
204
- const ctx = makeContext ( { parse : args , argv : [ "add" , "route" ] } ) ;
205
-
206
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
183
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "route" ) ) ) ;
207
184
expect ( error ) . toBeInstanceOf ( ArgError ) ;
208
185
expect ( error . sprint ( ) ) . toMatchInlineSnapshot ( `
209
186
"✘ Failed to add route, missing route method
@@ -214,9 +191,7 @@ describe("add", () => {
214
191
} ) ;
215
192
216
193
it ( "requires a route name" , async ( ) => {
217
- const ctx = makeContext ( { parse : args , argv : [ "add" , "route" , "GET" ] } ) ;
218
-
219
- const error = await expectError ( ( ) => addCommand ( ctx ) ) ;
194
+ const error = await expectError ( ( ) => add . run ( testCtx , makeArgs ( add . args , "add" , "route" , "GET" ) ) ) ;
220
195
expect ( error ) . toBeInstanceOf ( ArgError ) ;
221
196
expect ( error . sprint ( ) ) . toMatchInlineSnapshot ( `
222
197
"✘ Failed to add route, missing route path
0 commit comments