0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Added flags helper for Acceptance Tests in AdminX (#18500)

refs https://ghost.slack.com/archives/C0568LN2CGJ/p1696495250115289

Adds a new helper function `toggleLabsFlag` that allows toggling of lab
flags within tests. This function can be used directly inside a test
case or within `beforeEach` or `beforeAll` hooks to set the initial
state before tests run.

Usage:
- To toggle a flag within a test: `toggleLabsFlag('recommendations',
false);`
- To set initial state in a hook: 
  ```javascript
  beforeEach(() => {
    toggleLabsFlag('recommendations', true);
  });

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at e138d58</samp>

This change adds a utility module for acceptance testing of experimental
features in Ghost. It allows tests to mock and change the labs settings
using `defaultLabFlags` and `toggleLabsFlag`.
This commit is contained in:
Ronald Langeveld 2023-10-05 20:12:54 +07:00 committed by GitHub
parent 8122f02f17
commit e82f845d5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,6 +47,74 @@ export const responseFixtures = {
latestPost: {posts: [{id: '1', url: `${siteFixture.site.url}/test-post/`}]}
};
let defaultLabFlags = {
adminXSettings: false,
recommendations: false,
audienceFeedback: false,
collections: false,
themeErrorsNotification: false,
outboundLinkTagging: false,
announcementBar: false,
signupForm: false,
lexicalEditor: false,
members: false
};
// Inject defaultLabFlags into responseFixtures.settings and config
let labsSetting = responseFixtures.settings.settings.find(s => s.key === 'labs');
let configSettings = responseFixtures.config.config;
if (configSettings) {
configSettings.labs = defaultLabFlags;
}
if (!labsSetting) {
// If 'labs' key doesn't exist, create it
responseFixtures.settings.settings.push({
key: 'labs',
value: JSON.stringify(defaultLabFlags)
});
} else {
// If 'labs' key exists, update its value
labsSetting.value = JSON.stringify(defaultLabFlags);
}
interface LabsSettings {
[key: string]: boolean;
}
export function toggleLabsFlag(flag: string, value: boolean) {
// Update responseFixtures.settings
labsSetting = responseFixtures.settings.settings.find(s => s.key === 'labs');
if (!labsSetting) {
throw new Error('Labs settings not found');
}
if (typeof labsSetting.value !== 'string') {
throw new Error('Labs settings value is not a string');
}
let labs: LabsSettings;
try {
labs = JSON.parse(labsSetting.value);
} catch (e) {
throw new Error('Failed to parse labs settings');
}
labs[flag] = value;
labsSetting.value = JSON.stringify(labs);
// Update responseFixtures.config
configSettings = responseFixtures.config.config;
if (configSettings && configSettings.labs) {
configSettings.labs[flag] = value;
} else {
throw new Error('Config settings or labs settings in config not found');
}
}
export const globalDataRequests = {
browseSettings: {method: 'GET', path: /^\/settings\/\?group=/, response: responseFixtures.settings},
browseConfig: {method: 'GET', path: '/config/', response: responseFixtures.config},