Skip to content
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 @@ -5,10 +5,9 @@ import React from 'react';

import {renderInEntry} from 'support';
import {renderInContentElement} from 'pageflow-scrolled/testHelpers';
import {useFakeTranslations} from 'pageflow/testHelpers';
import {useFakeFeatures, useFakeTranslations} from 'pageflow/testHelpers';
import {screen} from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import {features} from 'pageflow/frontend';

describe('ExternalLink', () => {
it('renders link with href', () => {
Expand Down Expand Up @@ -286,8 +285,7 @@ describe('ExternalLink', () => {
});

describe('srcset', () => {
beforeEach(() => features.enable('frontend', ['image_srcset']));
afterEach(() => features.enabledFeatureNames = []);
useFakeFeatures('frontend', ['image_srcset']);

it('uses medium and large srcset for linkWidth m', () => {
const {getByRole} = renderInEntry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import '@testing-library/jest-dom/extend-expect'

import {InlineImage} from 'contentElements/inlineImage/InlineImage';
import {features} from 'pageflow/frontend';
import {useFakeFeatures} from 'pageflow/testHelpers';
import {usePortraitOrientation} from 'frontend/usePortraitOrientation';
jest.mock('frontend/usePortraitOrientation');

Expand Down Expand Up @@ -239,8 +240,7 @@ describe('InlineImage', () => {
});

describe('srcset', () => {
beforeEach(() => features.enable('frontend', ['image_srcset']));
afterEach(() => features.enabledFeatureNames = []);
useFakeFeatures('frontend', ['image_srcset']);

function renderInlineImage({contentElementWidth = 0, ...seedOptions} = {}) {
const result = renderInContentElement(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {editor} from 'pageflow-scrolled/editor';
import {ScrolledEntry} from 'editor/models/ScrolledEntry';
import {factories, normalizeSeed} from 'support';
import {features} from 'pageflow/frontend';
import {useFakeFeatures} from 'pageflow/testHelpers';

describe('ContentElement', () => {
describe('getAvailablePositions', () => {
beforeEach(() => {
features.enable('frontend', ['backdrop_content_elements']);
useFakeFeatures('frontend', ['backdrop_content_elements']);

beforeEach(() => {
editor.contentElementTypes.register('inlineImage', {});
editor.contentElementTypes.register('soundDisclaimer', {supportedPositions: ['inline']});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import '@testing-library/jest-dom/extend-expect';
import {act, waitFor} from '@testing-library/react';
import {features} from 'pageflow/frontend';
import {useFakeFeatures} from 'pageflow/testHelpers';

import {useInlineEditingPageObjects, renderEntry} from 'support/pageObjects/inlineEditing';

import badgeStyles from 'review/Badge.module.css';

describe('inline editing comment badges', () => {
useInlineEditingPageObjects();

beforeEach(() => {
features.enable('frontend', ['commenting']);
});
useFakeFeatures('frontend', ['commenting']);

it('does not display comment icon when element is not selected', () => {
const {queryByRole} = renderEntry({
Expand Down
57 changes: 57 additions & 0 deletions package/spec/testHelpers/useFakeFeatures-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {useFakeFeatures} from 'testHelpers/useFakeFeatures';

import {features} from 'pageflow/frontend';

describe('useFakeFeatures', () => {
describe('with one feature', () => {
useFakeFeatures('frontend', ['commenting']);

it('marks the feature as enabled', () => {
expect(features.isEnabled('commenting')).toBe(true);
});
});

describe('with multiple features', () => {
useFakeFeatures('frontend', ['commenting', 'image_srcset']);

it('enables all listed features', () => {
expect(features.isEnabled('commenting')).toBe(true);
expect(features.isEnabled('image_srcset')).toBe(true);
});
});

describe('with registered functions', () => {
const fn = jest.fn();
features.register('frontend', 'with_callback', fn);

useFakeFeatures('frontend', ['with_callback']);

it('invokes scope-registered callbacks for enabled features', () => {
expect(fn).toHaveBeenCalled();
});
});

describe('when called multiple times', () => {
useFakeFeatures('frontend', ['commenting']);
useFakeFeatures('editor', ['special_content_element']);

it('enables features from all calls', () => {
expect(features.isEnabled('commenting')).toBe(true);
expect(features.isEnabled('special_content_element')).toBe(true);
});
});

describe('cleanup', () => {
describe('inner describe enabling a feature', () => {
useFakeFeatures('frontend', ['scoped_feature']);

it('enables the feature inside', () => {
expect(features.isEnabled('scoped_feature')).toBe(true);
});
});

it('does not leak enabled features to sibling tests', () => {
expect(features.isEnabled('scoped_feature')).toBe(false);
});
});
});
1 change: 1 addition & 0 deletions package/src/testHelpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './dominos/ui';
export * from './factories';
export * from './fakeBrowserAgent';
export * from './setupGlobals';
export * from './useFakeFeatures';
export * from './useFakeTranslations';
export * from './renderBackboneView';
47 changes: 47 additions & 0 deletions package/src/testHelpers/useFakeFeatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {features} from 'pageflow/frontend';

/**
* Enable feature flags for the surrounding describe block.
*
* Each `beforeEach` calls `features.enable(scope, names)`, which both
* flips `features.isEnabled` to true for the given names and runs any
* functions registered via `features.register` for them. Each
* `afterEach` restores the set of enabled features to whatever was
* enabled before the helper was invoked, so suites do not leak state
* into each other.
*
* Multiple calls within the same describe block (or across nested
* describes) compose: each call adds its own features on top of
* whatever previous `useFakeFeatures` calls already enabled.
*
* @param {String} scope -
* Name of the scope to enable the features in (e.g. `'frontend'`,
* `'editor'`).
* @param {String[]} names -
* Feature names to enable in the given scope.
*
* @example
* import {useFakeFeatures} from 'pageflow/testHelpers';
* import {features} from 'pageflow/frontend';
*
* describe('...', () => {
* useFakeFeatures('frontend', ['commenting']);
*
* it('...', () => {
* expect(features.isEnabled('commenting')).toBe(true);
* });
* });
*/
export function useFakeFeatures(scope, names) {
let originalEnabledFeatureNames;

beforeEach(() => {
originalEnabledFeatureNames = features.enabledFeatureNames;

features.enable(scope, names);
});

afterEach(() => {
features.enabledFeatureNames = originalEnabledFeatureNames;
});
}
Loading