1
- import React from 'react'
2
1
import {
3
2
fireEvent ,
4
- lastBody ,
5
- renderWithRouter ,
3
+ lastPostBody ,
4
+ renderAppAt ,
6
5
screen ,
7
6
waitFor ,
8
7
} from '../../test-utils'
9
8
import fetchMock from 'fetch-mock'
10
9
11
10
import { org , project } from '@oxide/api-mocks'
12
11
13
- import { ProjectCreateForm } from '../ProjectCreatePage'
14
-
15
12
const projectsUrl = `/api/organizations/${ org . name } /projects`
13
+ const projectUrl = `${ projectsUrl } /${ project . name } `
14
+ const instancesUrl = `${ projectUrl } /instances?limit=10`
16
15
17
16
const submitButton = ( ) =>
18
17
screen . getByRole ( 'button' , { name : 'Create project' } )
@@ -22,40 +21,38 @@ function enterName(value: string) {
22
21
fireEvent . change ( nameInput , { target : { value } } )
23
22
}
24
23
25
- let successSpy : jest . Mock
24
+ const renderPage = ( ) => {
25
+ // fetch projects list for org layout sidebar on project create
26
+ fetchMock . get ( projectsUrl , { status : 200 , body : { items : [ ] } } )
27
+ const result = renderAppAt ( `/orgs/${ org . name } /projects/new` )
28
+ enterName ( 'valid-name' )
29
+ return result
30
+ }
26
31
27
32
describe ( 'ProjectCreateForm' , ( ) => {
28
- beforeEach ( ( ) => {
29
- successSpy = jest . fn ( )
30
- renderWithRouter (
31
- < ProjectCreateForm orgName = { org . name } onSuccess = { successSpy } />
32
- )
33
- enterName ( 'valid-name' )
34
- } )
35
-
36
33
afterEach ( ( ) => {
37
34
fetchMock . reset ( )
38
35
} )
39
36
40
37
it ( 'disables submit button on submit and enables on response' , async ( ) => {
41
38
const mock = fetchMock . post ( projectsUrl , { status : 201 } )
39
+ renderPage ( )
42
40
43
41
const submit = submitButton ( )
44
42
expect ( submit ) . not . toBeDisabled ( )
45
43
46
44
fireEvent . click ( submit )
47
45
48
- expect ( mock . called ( ) ) . toBeFalsy ( )
49
46
await waitFor ( ( ) => expect ( submit ) . toBeDisabled ( ) )
50
- expect ( mock . done ( ) ) . toBeTruthy ( )
51
- expect ( submit ) . not . toBeDisabled ( )
47
+ expect ( mock . called ( undefined , 'POST' ) ) . toBeTruthy ( )
52
48
} )
53
49
54
50
it ( 'shows message for known error code in project create code map' , async ( ) => {
55
51
fetchMock . post ( projectsUrl , {
56
52
status : 400 ,
57
53
body : { error_code : 'ObjectAlreadyExists' } ,
58
54
} )
55
+ renderPage ( )
59
56
60
57
fireEvent . click ( submitButton ( ) )
61
58
@@ -69,13 +66,15 @@ describe('ProjectCreateForm', () => {
69
66
status : 401 ,
70
67
body : { error_code : 'Forbidden' } ,
71
68
} )
69
+ renderPage ( )
72
70
73
71
fireEvent . click ( submitButton ( ) )
74
72
75
73
await screen . findByText ( 'Action not authorized' )
76
74
} )
77
75
78
76
it ( 'shows field-level validation error and does not POST' , async ( ) => {
77
+ renderPage ( )
79
78
enterName ( 'Invalid-name' )
80
79
fireEvent . click ( submitButton ( ) )
81
80
@@ -87,6 +86,7 @@ describe('ProjectCreateForm', () => {
87
86
status : 400 ,
88
87
body : { error_code : 'UnknownCode' } ,
89
88
} )
89
+ renderPage ( )
90
90
91
91
fireEvent . click ( submitButton ( ) )
92
92
@@ -95,26 +95,33 @@ describe('ProjectCreateForm', () => {
95
95
96
96
it ( 'posts form on submit' , async ( ) => {
97
97
const mock = fetchMock . post ( projectsUrl , { status : 201 } )
98
+ renderPage ( )
98
99
99
100
fireEvent . click ( submitButton ( ) )
100
101
101
102
await waitFor ( ( ) =>
102
- expect ( lastBody ( mock ) ) . toEqual ( { name : 'valid-name' , description : '' } )
103
+ expect ( lastPostBody ( mock ) ) . toEqual ( {
104
+ name : 'valid-name' ,
105
+ description : '' ,
106
+ } )
103
107
)
104
108
} )
105
109
106
- it ( 'calls onSuccess on success' , async ( ) => {
107
- const mock = fetchMock . post ( projectsUrl , {
110
+ it ( 'navigates to project instances page on success' , async ( ) => {
111
+ fetchMock . post ( projectsUrl , {
108
112
status : 201 ,
109
113
body : project ,
110
114
} )
115
+ fetchMock . get ( projectUrl , { status : 200 } )
116
+ // instances fetch after success
117
+ fetchMock . get ( instancesUrl , { status : 200 , body : { items : [ ] } } )
111
118
112
- expect ( successSpy ) . not . toHaveBeenCalled ( )
119
+ const { history } = renderPage ( )
120
+ const projectPath = `/orgs/${ org . name } /projects/${ project . name } /instances`
121
+ expect ( history . location . pathname ) . not . toEqual ( projectPath )
113
122
114
123
fireEvent . click ( submitButton ( ) )
115
124
116
- await waitFor ( ( ) => expect ( mock . called ( ) ) . toBeTruthy ( ) )
117
- await waitFor ( ( ) => expect ( mock . done ( ) ) . toBeTruthy ( ) )
118
- await waitFor ( ( ) => expect ( successSpy ) . toHaveBeenCalled ( ) )
125
+ await waitFor ( ( ) => expect ( history . location . pathname ) . toEqual ( projectPath ) )
119
126
} )
120
127
} )
0 commit comments