# extendify/3.1.0/tests/unit/Shared/lib/media-views.test.js

Extendify, version 3.1.0. 73 lines.

- Page: https://pluginprobe.com/plugins/extendify/3.1.0/code/tests/unit/Shared/lib/media-views.test.js
- Raw: https://pluginprobe.com/plugins/extendify/3.1.0/raw/tests/unit/Shared/lib/media-views.test.js
- Modified: 2026-06-11T18:37:12+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/extendify/3.1.0/code/tests/unit/Shared/lib/media-views.test.js#L10-L20`.

```javascript
import {
	addCustomMediaViewsCss,
	removeCustomMediaViewsCss,
} from '@shared/lib/media-views';

// media-views is a cross-bundle shared helper: the Agent (ImageUploader,
// UpdateImageConfirm) and QuickEdit (InlineEditor, SiteIdentityModal) both
// mount it on opening a wp.media picker and unmount it on cleanup. These pin
// the two guards both consumers lean on — a safe no-op when wp.media's
// stylesheet is absent, and a short-circuit when the override is already
// mounted. The CSS-rewrite injection itself walks the browser CSSOM
// (rule.style spread) which jsdom can't run, so that half is covered
// behaviorally by the image-flows Playwright spec, not here.
const STYLE_ID = 'media-views-important';
const SHEET_ID = 'media-views-css';

// Stand in for wp.media's own enqueued stylesheet.
const mountMediaViewsSheet = () => {
	const sheet = document.createElement('style');
	sheet.id = SHEET_ID;
	sheet.textContent = '.media-frame { color: red; }';
	document.head.appendChild(sheet);
};

describe('media-views shared helper', () => {
	afterEach(() => {
		document.getElementById(STYLE_ID)?.remove();
		document.getElementById(SHEET_ID)?.remove();
	});

	describe('addCustomMediaViewsCss', () => {
		it('is a safe no-op when wp.media stylesheet is absent', () => {
			expect(document.getElementById(SHEET_ID)).toBeNull();

			expect(() => addCustomMediaViewsCss()).not.toThrow();

			expect(document.getElementById(STYLE_ID)).toBeNull();
		});

		it('short-circuits when the override is already mounted', () => {
			// Both a real sheet and a prior override are present; the
			// id-guard must return before touching the sheet — otherwise the
			// CSSOM walk runs (and would re-inject a duplicate id).
			mountMediaViewsSheet();
			const existing = document.createElement('style');
			existing.id = STYLE_ID;
			document.head.appendChild(existing);

			addCustomMediaViewsCss();

			expect(document.querySelectorAll(`#${STYLE_ID}`)).toHaveLength(1);
			expect(document.getElementById(STYLE_ID)).toBe(existing);
		});
	});

	describe('removeCustomMediaViewsCss', () => {
		it('removes an injected style element', () => {
			const style = document.createElement('style');
			style.id = STYLE_ID;
			document.head.appendChild(style);

			removeCustomMediaViewsCss();

			expect(document.getElementById(STYLE_ID)).toBeNull();
		});

		it('is a safe no-op when nothing was injected', () => {
			expect(() => removeCustomMediaViewsCss()).not.toThrow();
			expect(document.getElementById(STYLE_ID)).toBeNull();
		});
	});
});

```
