1
+ /*eslint eslint-comments/disable-enable-pair: error -- Remove after bottlenecks or causes of timeouts for testing have been figured out */
1
2
import { screen , waitFor } from "@testing-library/react" ;
2
3
import userEvent from "@testing-library/user-event" ;
3
4
import * as API from "api/api" ;
@@ -36,49 +37,64 @@ const renderTemplateSchedulePage = async () => {
36
37
await waitForLoaderToBeRemoved ( ) ;
37
38
} ;
38
39
40
+ // Extracts all properties from TemplateScheduleFormValues that have a key that
41
+ // ends in _ms, and makes those properties optional. Defined as mapped type to
42
+ // ensure this stays in sync as TemplateScheduleFormValues changes
43
+ type FillAndSubmitConfig = {
44
+ [ Key in keyof TemplateScheduleFormValues as Key extends `${string } _ms`
45
+ ? Key
46
+ : never ] ?: TemplateScheduleFormValues [ Key ] | undefined ;
47
+ } ;
48
+
49
+ /*eslint-disable no-console -- Start benchmarks */
39
50
const fillAndSubmitForm = async ( {
40
51
default_ttl_ms,
41
52
max_ttl_ms,
42
53
failure_ttl_ms,
43
54
time_til_dormant_ms,
44
55
time_til_dormant_autodelete_ms,
45
- } : {
46
- default_ttl_ms ?: number ;
47
- max_ttl_ms ?: number ;
48
- failure_ttl_ms ?: number ;
49
- time_til_dormant_ms ?: number ;
50
- time_til_dormant_autodelete_ms ?: number ;
51
- } ) => {
56
+ } : FillAndSubmitConfig ) => {
57
+ console . time ( "form - full function" ) ;
52
58
const user = userEvent . setup ( ) ;
53
59
60
+ console . time ( "form - ttl" ) ;
54
61
if ( default_ttl_ms ) {
55
62
const defaultTtlField = await screen . findByLabelText (
56
63
"Default autostop (hours)" ,
57
64
) ;
58
65
await user . clear ( defaultTtlField ) ;
59
66
await user . type ( defaultTtlField , default_ttl_ms . toString ( ) ) ;
60
67
}
68
+ console . timeEnd ( "form - ttl" ) ;
61
69
70
+ console . time ( "form - max_ttl" ) ;
62
71
if ( max_ttl_ms ) {
63
72
const maxTtlField = await screen . findByLabelText ( "Max lifetime (hours)" ) ;
73
+
64
74
await user . clear ( maxTtlField ) ;
65
75
await user . type ( maxTtlField , max_ttl_ms . toString ( ) ) ;
66
76
}
77
+ console . timeEnd ( "form - max_ttl" ) ;
67
78
79
+ console . time ( "form - failure_ttl" ) ;
68
80
if ( failure_ttl_ms ) {
69
81
const failureTtlField = screen . getByRole ( "checkbox" , {
70
82
name : / F a i l u r e C l e a n u p / i,
71
83
} ) ;
72
84
await user . type ( failureTtlField , failure_ttl_ms . toString ( ) ) ;
73
85
}
86
+ console . timeEnd ( "form - failure_ttl" ) ;
74
87
88
+ console . time ( "form - dormant" ) ;
75
89
if ( time_til_dormant_ms ) {
76
90
const inactivityTtlField = screen . getByRole ( "checkbox" , {
77
91
name : / D o r m a n c y T h r e s h o l d / i,
78
92
} ) ;
79
93
await user . type ( inactivityTtlField , time_til_dormant_ms . toString ( ) ) ;
80
94
}
95
+ console . timeEnd ( "form - dormant" ) ;
81
96
97
+ console . time ( "form - auto-delete" ) ;
82
98
if ( time_til_dormant_autodelete_ms ) {
83
99
const dormancyAutoDeletionField = screen . getByRole ( "checkbox" , {
84
100
name : / D o r m a n c y A u t o - D e l e t i o n / i,
@@ -88,16 +104,24 @@ const fillAndSubmitForm = async ({
88
104
time_til_dormant_autodelete_ms . toString ( ) ,
89
105
) ;
90
106
}
107
+ console . timeEnd ( "form - auto-delete" ) ;
91
108
109
+ console . time ( "form - submit" ) ;
92
110
const submitButton = await screen . findByText (
93
111
FooterFormLanguage . defaultSubmitLabel ,
94
112
) ;
95
113
await user . click ( submitButton ) ;
114
+ console . timeEnd ( "form - submit" ) ;
96
115
97
- // User needs to confirm dormancy and autodeletion fields.
116
+ console . time ( "form - confirm" ) ;
117
+ // User needs to confirm dormancy and auto-deletion fields.
98
118
const confirmButton = await screen . findByTestId ( "confirm-button" ) ;
99
119
await user . click ( confirmButton ) ;
120
+ console . timeEnd ( "form - confirm" ) ;
121
+
122
+ console . timeEnd ( "form - full function" ) ;
100
123
} ;
124
+ /*eslint-enable no-console -- End benchmarks */
101
125
102
126
describe ( "TemplateSchedulePage" , ( ) => {
103
127
beforeEach ( ( ) => {
@@ -109,15 +133,16 @@ describe("TemplateSchedulePage", () => {
109
133
jest . spyOn ( API , "getExperiments" ) . mockResolvedValue ( [ "workspace_actions" ] ) ;
110
134
} ) ;
111
135
112
- it ( "succeeds " , async ( ) => {
136
+ it ( "Calls the API when user fills in and submits a form " , async ( ) => {
113
137
await renderTemplateSchedulePage ( ) ;
114
138
jest . spyOn ( API , "updateTemplateMeta" ) . mockResolvedValueOnce ( {
115
139
...MockTemplate ,
116
140
...validFormValues ,
117
141
} ) ;
142
+
118
143
await fillAndSubmitForm ( validFormValues ) ;
119
144
await waitFor ( ( ) => expect ( API . updateTemplateMeta ) . toBeCalledTimes ( 1 ) ) ;
120
- } ) ;
145
+ } , 15_000 ) ;
121
146
122
147
test ( "default and max ttl is converted to and from hours" , async ( ) => {
123
148
await renderTemplateSchedulePage ( ) ;
0 commit comments