Skip to content

Commit 22ded37

Browse files
authored
Merge pull request #9733 from jrafanie/fix-tenant-cypress-missing-intercept-waits
Fix bug where a GET triggered by a test, is processed after the test
2 parents c09cd18 + 27612aa commit 22ded37

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

cypress/e2e/ui/Settings/Application-Settings/tenant.cy.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ function confirmUiNavigation(callback) {
6868
});
6969
}
7070

71+
function openTenantFormAndWaitForValidation(configOption) {
72+
cy.interceptApi({
73+
method: 'GET',
74+
alias: 'tenantValidationApi',
75+
urlPattern: /\/api\/tenants\?filter\[\]=name=.*&expand=resources/,
76+
triggerFn: () => cy.toolbar(CONFIG_TOOLBAR_BUTTON, configOption),
77+
});
78+
}
79+
80+
function openEditTenantFormAndWaitForLoad(configOption) {
81+
// Tenant edit form triggers two API calls in sequence:
82+
// 1. Load existing tenant data (happens first in useEffect, in ops-tenant-form.jsx)
83+
// 2. Form validation API (happens after load completes and form renders with validateOnMount: true)
84+
// The form doesn't render until isLoading=false, so validation always happens after the form loads.
85+
cy.intercept('GET', /\/api\/tenants\/\d+\?expand=resources&attributes=/).as('tenantLoadApi');
86+
cy.intercept('GET', /\/api\/tenants\?filter\[\]=name=.*&expand=resources/).as('tenantValidationApi');
87+
88+
cy.toolbar(CONFIG_TOOLBAR_BUTTON, configOption);
89+
90+
// Wait for both API calls to complete in order
91+
// Load API must complete first (component logic ensures this)
92+
cy.wait('@tenantLoadApi');
93+
cy.wait('@tenantValidationApi');
94+
}
95+
7196
function cancelFormWithOptionalFlashCheck(assertFlashMessage = true) {
7297
cy.getFormButtonByTypeWithText({
7398
buttonText: CANCEL_BUTTON_TEXT,
@@ -162,7 +187,7 @@ function validateFormElements(isEditForm = true) {
162187
}
163188

164189
function createAndSelectChildTenant() {
165-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, ADD_CHILD_TENANT_CONFIG_OPTION);
190+
openTenantFormAndWaitForValidation(ADD_CHILD_TENANT_CONFIG_OPTION);
166191
updateNameAndDescription(
167192
INITIAL_CHILD_TENANT_NAME,
168193
INITIAL_CHILD_TENANT_DESCRIPTION
@@ -186,7 +211,7 @@ function resetParentTenantForm() {
186211
// Check if the text matches the edited parent tenant name, if yes reset
187212
if (text === EDITED_TENANT_NAME_VALUE) {
188213
cy.wrap(item).click();
189-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, EDIT_TENANT_CONFIG_OPTION);
214+
openEditTenantFormAndWaitForLoad(EDIT_TENANT_CONFIG_OPTION);
190215
updateNameAndDescription(
191216
INITIAL_PARENT_TENANT_NAME,
192217
INITIAL_PARENT_TENANT_DESCRIPTION
@@ -229,7 +254,7 @@ function deleteAccordionItems(accordionsToDelete) {
229254
}
230255

231256
function addProjectToTenant() {
232-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, ADD_PROJECT_CONFIG_OPTION);
257+
openTenantFormAndWaitForValidation(ADD_PROJECT_CONFIG_OPTION);
233258
updateNameAndDescription(PROJECT_NAME_VALUE, EDITED_DESCRIPTION_VALUE);
234259
saveFormWithOptionalFlashCheck({
235260
button: ADD_BUTTON_TEXT,
@@ -275,7 +300,7 @@ describe('Automate Tenant form operations: Settings > Application Settings > Acc
275300
describe('Validate Parent Tenant operations: Edit, Add Project, Manage Quotas', () => {
276301
describe('Validate Edit parent tenant', () => {
277302
beforeEach(() => {
278-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, EDIT_TENANT_CONFIG_OPTION);
303+
openEditTenantFormAndWaitForLoad(EDIT_TENANT_CONFIG_OPTION);
279304
});
280305

281306
afterEach(() => {
@@ -347,7 +372,7 @@ describe('Automate Tenant form operations: Settings > Application Settings > Acc
347372
describe('Validate Child Tenant operations: Add, Edit, Add Project, Manage Quotas', () => {
348373
describe('Validate Add child tenant function', () => {
349374
beforeEach(() => {
350-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, ADD_CHILD_TENANT_CONFIG_OPTION);
375+
openTenantFormAndWaitForValidation(ADD_CHILD_TENANT_CONFIG_OPTION);
351376
});
352377

353378
afterEach(() => {
@@ -364,7 +389,7 @@ describe('Automate Tenant form operations: Settings > Application Settings > Acc
364389
INITIAL_CHILD_TENANT_DESCRIPTION
365390
);
366391
cancelFormWithOptionalFlashCheck();
367-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, ADD_CHILD_TENANT_CONFIG_OPTION);
392+
openTenantFormAndWaitForValidation(ADD_CHILD_TENANT_CONFIG_OPTION);
368393
updateNameAndDescription(
369394
INITIAL_CHILD_TENANT_NAME,
370395
INITIAL_CHILD_TENANT_DESCRIPTION
@@ -387,7 +412,7 @@ describe('Automate Tenant form operations: Settings > Application Settings > Acc
387412
button: ADD_BUTTON_TEXT,
388413
flashMessageSnippet: FLASH_MESSAGE_ADDED,
389414
});
390-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, ADD_CHILD_TENANT_CONFIG_OPTION);
415+
openTenantFormAndWaitForValidation(ADD_CHILD_TENANT_CONFIG_OPTION);
391416
cy.getFormInputFieldByIdAndType({ inputId: 'name' }).type(
392417
INITIAL_CHILD_TENANT_NAME
393418
);
@@ -401,7 +426,7 @@ describe('Automate Tenant form operations: Settings > Application Settings > Acc
401426
describe('Validate Edit child tenant', () => {
402427
beforeEach(() => {
403428
createAndSelectChildTenant();
404-
cy.toolbar(CONFIG_TOOLBAR_BUTTON, EDIT_TENANT_CONFIG_OPTION);
429+
openEditTenantFormAndWaitForLoad(EDIT_TENANT_CONFIG_OPTION);
405430
});
406431

407432
afterEach(() => {

0 commit comments

Comments
 (0)