Skip to content

Commit 0d45dd8

Browse files
author
vilmibm
committed
finish edit tests
1 parent 15cf786 commit 0d45dd8

File tree

1 file changed

+76
-13
lines changed

1 file changed

+76
-13
lines changed

pkg/cmd/gist/edit/edit_test.go

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ func Test_editRun(t *testing.T) {
9393
},
9494
},
9595
},
96-
//askStubs: func(as *prompt.AskStubber) {
97-
// as.StubOne("new file content")
98-
//},
9996
httpStubs: func(reg *httpmock.Registry) {
10097
reg.Register(httpmock.REST("POST", "gists/1234"),
10198
httpmock.StatusStringResponse(201, "{}"))
@@ -113,8 +110,73 @@ func Test_editRun(t *testing.T) {
113110
},
114111
},
115112
},
116-
// TODO multiple files, submit
117-
// TODO multiple files, cancel
113+
{
114+
name: "multiple files, submit",
115+
askStubs: func(as *prompt.AskStubber) {
116+
as.StubOne("unix.md")
117+
as.StubOne("Submit")
118+
},
119+
gist: &shared.Gist{
120+
ID: "1234",
121+
Description: "catbug",
122+
Files: map[string]*shared.GistFile{
123+
"cicada.txt": {
124+
Filename: "cicada.txt",
125+
Content: "bwhiizzzbwhuiiizzzz",
126+
Type: "text/plain",
127+
},
128+
"unix.md": {
129+
Filename: "unix.md",
130+
Content: "meow",
131+
Type: "application/markdown",
132+
},
133+
},
134+
},
135+
httpStubs: func(reg *httpmock.Registry) {
136+
reg.Register(httpmock.REST("POST", "gists/1234"),
137+
httpmock.StatusStringResponse(201, "{}"))
138+
},
139+
wantParams: map[string]interface{}{
140+
"description": "catbug",
141+
"updated_at": "0001-01-01T00:00:00Z",
142+
"public": false,
143+
"files": map[string]interface{}{
144+
"cicada.txt": map[string]interface{}{
145+
"content": "bwhiizzzbwhuiiizzzz",
146+
"filename": "cicada.txt",
147+
"type": "text/plain",
148+
},
149+
"unix.md": map[string]interface{}{
150+
"content": "new file content",
151+
"filename": "unix.md",
152+
"type": "application/markdown",
153+
},
154+
},
155+
},
156+
},
157+
{
158+
name: "multiple files, cancel",
159+
askStubs: func(as *prompt.AskStubber) {
160+
as.StubOne("unix.md")
161+
as.StubOne("Cancel")
162+
},
163+
wantErr: true,
164+
gist: &shared.Gist{
165+
ID: "1234",
166+
Files: map[string]*shared.GistFile{
167+
"cicada.txt": {
168+
Filename: "cicada.txt",
169+
Content: "bwhiizzzbwhuiiizzzz",
170+
Type: "text/plain",
171+
},
172+
"unix.md": {
173+
Filename: "unix.md",
174+
Content: "meow",
175+
Type: "application/markdown",
176+
},
177+
},
178+
},
179+
},
118180
}
119181

120182
for _, tt := range tests {
@@ -161,21 +223,22 @@ func Test_editRun(t *testing.T) {
161223

162224
t.Run(tt.name, func(t *testing.T) {
163225
err := editRun(tt.opts)
226+
reg.Verify(t)
164227
if tt.wantErr {
165228
assert.Error(t, err)
166229
return
167230
}
168231
assert.NoError(t, err)
169232

170-
reg.Verify(t)
171-
172-
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
173-
reqBody := make(map[string]interface{})
174-
err = json.Unmarshal(bodyBytes, &reqBody)
175-
if err != nil {
176-
t.Fatalf("error decoding JSON: %v", err)
233+
if tt.wantParams != nil {
234+
bodyBytes, _ := ioutil.ReadAll(reg.Requests[1].Body)
235+
reqBody := make(map[string]interface{})
236+
err = json.Unmarshal(bodyBytes, &reqBody)
237+
if err != nil {
238+
t.Fatalf("error decoding JSON: %v", err)
239+
}
240+
assert.Equal(t, tt.wantParams, reqBody)
177241
}
178-
assert.Equal(t, tt.wantParams, reqBody)
179242
})
180243
}
181244
}

0 commit comments

Comments
 (0)