Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin E2E: Fix isFeatureEnabled fixture #691

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('/');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Load start page so that page init scripts is executed before other fixtures.

await use(page);
};

Expand Down
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
Loading