Skip to content

Commit

Permalink
Merge pull request #153 from shopware/next-17124/correct-onlyOnFeatur…
Browse files Browse the repository at this point in the history
…e-command

fix commands for feature flags
  • Loading branch information
jleifeld authored Sep 27, 2021
2 parents cd78598 + 37f0844 commit 714f50f
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions cypress/support/commands/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,23 +692,48 @@ Cypress.Commands.add(
);

/**
* @memberOf Cypress.Chainable#
* @name featureIsActive
* @function
* @param {window} win - Browser window object
* @param {string} feature - The feature name
* @return {boolean}
* @param {String} feature
*/
function featureIsActive(win, feature) {
if (
win !== undefined &&
win.Shopware !== undefined &&
win.Shopware.Feature !== undefined
) {
return win.Shopware.Feature.isActive(feature);
}
Cypress.Commands.add('featureIsActive', (feature) => {
cy.window().then((win) =>{
// Check directly if admin is already loaded
if (
win !== undefined &&
win.Shopware !== undefined &&
win.Shopware.Feature !== undefined &&
typeof win.Shopware.Feature.isActive === 'function'
) {
return cy.wrap(win.Shopware.Feature.isActive(feature));
}

// Otherwise save current URL
// and go to admin and check feature flags
cy.url().then(previousUrl => {
cy.visit(`${Cypress.env('admin')}#/login`);
cy.get('#app').should('exist');

cy.window().then(win => {
let isActive = false;

if (
win !== undefined &&
win.Shopware !== undefined &&
win.Shopware.Feature !== undefined
) {
isActive = win.Shopware.Feature.isActive(feature);
} else {
isActive = false;
}

return false;
}
win.location.href = previousUrl;

return cy.wrap(isActive);
})
})
})
});

/**
* Skip test on active feature
Expand All @@ -719,9 +744,7 @@ function featureIsActive(win, feature) {
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
Cypress.Commands.add('skipOnFeature', (feature, cb) => {
cy.window().then((win) => {
const isActive = featureIsActive(win, feature);

cy.featureIsActive(feature).then(isActive => {
if (isActive) {
cy.log(
`Skipping test because feature flag '${feature}' is activated.`
Expand All @@ -740,18 +763,16 @@ Cypress.Commands.add('skipOnFeature', (feature, cb) => {
* @param {String} feature - Skip test if feature is inactive
* @param {() => void} cb - Optional, run the given callback if the condition passes
*/
Cypress.Commands.add('onlyOnFeature', (feature, cb) => {
cy.window().then((win) => {
const isActive = featureIsActive(win, feature);

Cypress.Commands.add('onlyOnFeature', (feature, cb) => {
cy.featureIsActive(feature).then(isActive => {
if (isActive) {
cy.log(
`Running test because feature flag '${feature}' is activated.`
);
}

cy.onlyOn(isActive, cb);
});
})
});

/**
Expand Down

0 comments on commit 714f50f

Please sign in to comment.