Skip to content

Commit

Permalink
test: [M3-8619] - Fix Cypress test flake in `create-linode-from-image…
Browse files Browse the repository at this point in the history
….spec.ts` (#10983)

* Fix region selection test failures when selecting a region with a name similar to another region

* Added changeset: Improve region selection RegEx to resolve test failures when selecting certain regions
  • Loading branch information
jdamore-linode authored Sep 23, 2024
1 parent f36485d commit c721a17
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-10983-tests-1726865807776.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tests
---

Improve region selection RegEx to resolve test failures when selecting certain regions ([#10983](https://github.com/linode/manager/pull/10983))
25 changes: 19 additions & 6 deletions packages/manager/cypress/support/ui/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ import { getRegionById, getRegionByLabel } from 'support/util/regions';
import type { SelectorMatcherOptions } from '@testing-library/cypress';
import type { Region } from '@linode/api-v4';

/**
* Returns a regular expression object to match against region select items.
*
* This expression accounts for these cases:
* - Gecko LA is disabled (region ID is present in line with label)
* - Gecko LA is enabled (region ID is not in line with label, rendered in separate element)
* - Avoids selecting similarly named regions (e.g. "UK, London" and "UK, London 2")
*
* @param region - Region for which to return RegEx.
*
* @returns Regular expression object to match menu item of given Region.
*/
// TODO Remove this and use exact string matching once Gecko feature flag is retired.
const getRegionItemRegEx = (region: Region) => {
return new RegExp(`${region.label}(\\s?\\(${region.id}\\)|$)`);
};

/**
* Autocomplete UI element.
*/
Expand Down Expand Up @@ -90,9 +107,7 @@ export const regionSelect = {
*/
findItemByRegionId: (regionId: string, searchRegions?: Region[]) => {
const region = getRegionById(regionId, searchRegions);
return autocompletePopper.findByTitle(
new RegExp(`${region.label}\\s?(\(${region.id}\))?`)
);
return autocompletePopper.findByTitle(getRegionItemRegEx(region));
},

/**
Expand All @@ -107,8 +122,6 @@ export const regionSelect = {
*/
findItemByRegionLabel: (regionLabel: string, searchRegions?: Region[]) => {
const region = getRegionByLabel(regionLabel, searchRegions);
return autocompletePopper.findByTitle(
new RegExp(`${region.label}\\s?(\(${region.id}\))?`)
);
return autocompletePopper.findByTitle(getRegionItemRegEx(region));
},
};

0 comments on commit c721a17

Please sign in to comment.