Skip to content

Commit

Permalink
Plugin E2E: Fix isFeatureEnabled fixture (#691)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunker authored Jan 23, 2024
1 parent 123dde8 commit 95270a4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-e2e/src/fixtures/isFeatureToggleEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type FeatureToggleFixture = TestFixture<
PluginFixture & PluginOptions & PlaywrightCombinedArgs
>;

const isFeatureToggleEnabled: FeatureToggleFixture = async ({ page }, use) => {
const isFeatureToggleEnabled: FeatureToggleFixture = async ({ page, grafanaVersion }, use) => {
await use(async <T = object>(featureToggle: keyof T) => {
const featureToggles: T = await page.evaluate('window.grafanaBootData.settings.featureToggles');
return Boolean(featureToggles[featureToggle]);
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-e2e/src/fixtures/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const page: PageFixture = async ({ page, featureToggles }, use) => {
console.error('Failed to set feature toggles', error);
}
}
await page.goto('/');
await use(page);
};

Expand Down
25 changes: 5 additions & 20 deletions packages/plugin-e2e/src/fixtures/scripts/overrideFeatureToggles.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// this script is evaluated in the browser context, so we cannot use typescript
export const overrideFeatureToggles = (featureToggles) => {
const timeout = 5;
const localStorageKey = 'grafana.featureToggles';

const waitForGrafanaBootData = (cb) => {
if (window.grafanaBootData) {
Expand All @@ -11,28 +10,14 @@ export const overrideFeatureToggles = (featureToggles) => {
}
};

const versionGte = (version, major, minor) => {
const [majorVersion, minorVersion] = version.split('.');
return Number(majorVersion) >= major && Number(minorVersion) >= minor;
};

// wait for Grafana boot data to be added to the window object
waitForGrafanaBootData(() => {
const version = window?.grafanaBootData?.settings?.buildInfo?.version;

// since Grafana 10.1.0, Grafana reads feature toggles from localStorage
// since this script runs in the browser context, we don't have access to semver.gte function that is used in other places of this package
if (versionGte(version, 10, 1)) {
const value = Object.entries(featureToggles)
.map(([key, value]) => `${key}=${value}`)
.join(',');
localStorage.setItem(localStorageKey, value);
} else {
// override feature toggles with the ones provided by the test
window.grafanaBootData.settings.featureToggles = {
...window.grafanaBootData.settings.featureToggles,
...featureToggles,
};
}
// override feature toggles with the ones provided by the test
window.grafanaBootData.settings.featureToggles = {
...window.grafanaBootData.settings.featureToggles,
...featureToggles,
};
});
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { expect, test } from '../../../src';
import { ProvisionFile } from '../../../src/types';

const TRUTHY_CUSTOM_TOGGLE = 'custom_toggle1';
const FALSY_CUSTOM_TOGGLE = 'custom_toggle2';
// override the feature toggles defined in playwright.config.ts only for tests in this file
test.use({
featureToggles: {
redshiftAsyncQueryDataSupport: true,
[TRUTHY_CUSTOM_TOGGLE]: true,
[FALSY_CUSTOM_TOGGLE]: false,
},
});

test('should set feature toggles correctly', async ({ isFeatureToggleEnabled }) => {
expect(await isFeatureToggleEnabled(TRUTHY_CUSTOM_TOGGLE)).toBeTruthy();
expect(await isFeatureToggleEnabled(FALSY_CUSTOM_TOGGLE)).toBeFalsy();
});

test('async query data handler should return a `finished` status', async ({
selectors,
panelEditPage,
Expand Down

0 comments on commit 95270a4

Please sign in to comment.