|
| 1 | +import {type BOFeatureFlagInterface} from '@interfaces/BO/advancedParameters/featureFlag'; |
| 2 | +import BOBasePage from '@pages/BO/BOBasePage'; |
| 3 | +import {type Page} from '@playwright/test'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Feature flag page, contains functions that can be used on the page |
| 7 | + * @class |
| 8 | + * @extends BOBasePage |
| 9 | + */ |
| 10 | +class BOFeatureFlag extends BOBasePage implements BOFeatureFlagInterface { |
| 11 | + public readonly pageTitle: string; |
| 12 | + |
| 13 | + public readonly featureFlagAdminAPI: string; |
| 14 | + |
| 15 | + private readonly featureFlagSwitchButton: (status: string, feature: string, toggle: number) => string; |
| 16 | + |
| 17 | + private readonly submitButton: (status: string) => string; |
| 18 | + |
| 19 | + private readonly alertSuccess: string; |
| 20 | + |
| 21 | + private readonly modalSubmitFeatureFlag: string; |
| 22 | + |
| 23 | + private readonly enableExperimentalFeatureButton: string; |
| 24 | + |
| 25 | + /** |
| 26 | + * @constructs |
| 27 | + * Setting up texts and selectors to use on import page |
| 28 | + */ |
| 29 | + constructor() { |
| 30 | + super(); |
| 31 | + |
| 32 | + this.pageTitle = `New & experimental features • ${global.INSTALL.SHOP_NAME}`; |
| 33 | + this.successfulUpdateMessage = 'Update successful'; |
| 34 | + |
| 35 | + // Feature Flag |
| 36 | + this.featureFlagAdminAPI = 'admin_api'; |
| 37 | + // Selectors |
| 38 | + this.featureFlagSwitchButton = (status: string, feature: string, toggle: number) => `#feature_flag_${ |
| 39 | + status}_feature_flags_${feature}_enabled_${toggle}`; |
| 40 | + this.submitButton = (status: string) => `#feature_flag_${status}_submit`; |
| 41 | + this.alertSuccess = 'div.alert.alert-success[role="alert"]'; |
| 42 | + this.modalSubmitFeatureFlag = '#modal-confirm-submit-feature-flag'; |
| 43 | + this.enableExperimentalFeatureButton = `${this.modalSubmitFeatureFlag} button.btn-confirm-submit`; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Enable/Disable feature flag |
| 48 | + * @param page {Page} Browser tab |
| 49 | + * @param featureFlag {string} |
| 50 | + * @param toEnable {boolean} True if we need to enable new product page |
| 51 | + * @returns {Promise<string>} |
| 52 | + */ |
| 53 | + async setFeatureFlag(page: Page, featureFlag: string, toEnable: boolean = true): Promise<string> { |
| 54 | + let isStable: boolean; |
| 55 | + |
| 56 | + switch (featureFlag) { |
| 57 | + case this.featureFlagAdminAPI: |
| 58 | + isStable = false; |
| 59 | + break; |
| 60 | + default: |
| 61 | + throw new Error(`The feature flag ${featureFlag} is not defined`); |
| 62 | + } |
| 63 | + |
| 64 | + const selector: string = this.featureFlagSwitchButton(isStable ? 'stable' : 'beta', featureFlag, toEnable ? 1 : 0); |
| 65 | + |
| 66 | + const isChecked = await this.isChecked(page, selector); |
| 67 | + |
| 68 | + if (isChecked) { |
| 69 | + // Return the successful message to simulate all went good (no need to change the value here) |
| 70 | + return this.successfulUpdateMessage; |
| 71 | + } |
| 72 | + |
| 73 | + await this.setChecked(page, selector); |
| 74 | + await this.waitForSelectorAndClick(page, this.submitButton(isStable ? 'stable' : 'beta')); |
| 75 | + // The confirmation modal is only displayed for experimental/beta feature flags |
| 76 | + if (toEnable && !isStable) { |
| 77 | + await this.waitForVisibleSelector(page, this.modalSubmitFeatureFlag); |
| 78 | + await this.clickAndWaitForLoadState(page, this.enableExperimentalFeatureButton); |
| 79 | + } |
| 80 | + |
| 81 | + return this.getTextContent(page, this.alertSuccess, true); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +module.exports = new BOFeatureFlag(); |
0 commit comments