File size: 5,869 Bytes
e75e43f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | diff --git a/packages/components/psammead-brand/src/print.test.jsx b/packages/components/psammead-brand/src/print.test.jsx
new file mode 100644
index 000000000..76d1d3445
--- /dev/null
+++ b/packages/components/psammead-brand/src/print.test.jsx
@@ -0,0 +1,172 @@
+import React from 'react';
+import renderer from 'react-test-renderer';
+import 'jest-styled-components';
+// eslint-disable-next-line import/no-extraneous-dependencies
+import { getCSS } from 'jest-styled-components/src/utils';
+import Brand from './index';
+
+const findByType = (node, type) => {
+ if (!node || typeof node !== 'object') return null;
+ if (Array.isArray(node)) {
+ for (let i = 0; i < node.length; i += 1) {
+ const f = findByType(node[i], type);
+ if (f) return f;
+ }
+ return null;
+ }
+ if (node.type === type) return node;
+ if (!node.children) return null;
+ for (let i = 0; i < node.children.length; i += 1) {
+ const f = findByType(node.children[i], type);
+ if (f) return f;
+ }
+ return null;
+};
+
+
+const getGClassTokens = tree => {
+ const g = findByType(tree, 'g');
+ if (!g || !g.props || !g.props.className) return [];
+ return String(g.props.className).trim().split(/\s+/);
+};
+
+
+const getWrapperClassTokens = tree => {
+ if (!tree || !tree.props || !tree.props.className) return [];
+ return String(tree.props.className).trim().split(/\s+/);
+};
+
+
+const collectPrintMediaRules = ast => {
+ if (!ast || !ast.stylesheet) return [];
+ return ast.stylesheet.rules.filter(
+ r => r.type === 'media' && /print/.test(r.media || ''),
+ );
+};
+
+
+
+const selectorTargetsG = (selector, gClassTokens) => {
+ if (typeof selector !== 'string') return false;
+ if (gClassTokens.some(c => selector.includes(`.${c}`))) return true;
+
+ if (/(^|[\s>+~])g\s*$/.test(selector.trim())) return true;
+ return false;
+};
+
+
+
+const selectorTargetsWrapper = (selector, wrapperClassTokens) =>
+ typeof selector === 'string' &&
+ wrapperClassTokens.some(c => selector.includes(`.${c}`));
+
+
+
+
+
+
+
+const hasValidPrintVisibilityFix = (ast, gClassTokens, wrapperClassTokens) => {
+ const printRules = collectPrintMediaRules(ast);
+ for (let i = 0; i < printRules.length; i += 1) {
+ const media = printRules[i];
+ if (!media.rules) continue; // eslint-disable-line no-continue
+ for (let j = 0; j < media.rules.length; j += 1) {
+ const rule = media.rules[j];
+ if (rule.type !== 'rule' || !rule.declarations) continue; // eslint-disable-line no-continue
+
+ const hasFill = rule.declarations.some(d => d.property === 'fill');
+ const hasBg = rule.declarations.some(
+ d => d.property === 'background-color',
+ );
+ const hasPrintColorAdjust = rule.declarations.some(
+ d =>
+ (d.property === 'print-color-adjust' ||
+ d.property === '-webkit-print-color-adjust') &&
+ d.value === 'exact',
+ );
+
+ const selectors = rule.selectors || [];
+
+
+ if (
+ hasFill &&
+ selectors.some(s => selectorTargetsG(s, gClassTokens))
+ ) {
+ return true;
+ }
+
+
+ if (
+ hasBg &&
+ hasPrintColorAdjust &&
+ selectors.some(s => selectorTargetsWrapper(s, wrapperClassTokens))
+ ) {
+ return true;
+ }
+ }
+ }
+ return false;
+};
+
+describe('Brand print visibility fix', () => {
+ it('ensures the SVG logo remains visible when printing', () => {
+ const tree = renderer.create(<Brand brandName="BBC News" />).toJSON();
+ const gClassTokens = getGClassTokens(tree);
+ const wrapperClassTokens = getWrapperClassTokens(tree);
+ const ast = getCSS();
+
+ expect(
+ hasValidPrintVisibilityFix(ast, gClassTokens, wrapperClassTokens),
+ ).toBe(true);
+ });
+
+ it('does not leave the <g> fill="#fff" attribute unmitigated', () => {
+
+
+
+
+
+ const tree = renderer.create(<Brand brandName="BBC News" />).toJSON();
+ const g = findByType(tree, 'g');
+ expect(g).not.toBeNull();
+
+ const fillAttr = g.props.fill;
+ const gClassTokens = getGClassTokens(tree);
+ const wrapperClassTokens = getWrapperClassTokens(tree);
+ const ast = getCSS();
+ const cssLevelFix = hasValidPrintVisibilityFix(
+ ast,
+ gClassTokens,
+ wrapperClassTokens,
+ );
+
+ const attrLevelFix = fillAttr !== '#fff' && fillAttr !== '#FFFFFF';
+ expect(cssLevelFix || attrLevelFix).toBe(true);
+ });
+
+ it('preserves the BBC News link href (regression check)', () => {
+ const tree = renderer
+ .create(<Brand brandName="BBC News" />)
+ .toJSON();
+ const a = findByType(tree, 'a');
+ expect(a).not.toBeNull();
+ expect(a.props.href).toBe('https://www.bbc.co.uk/news');
+ });
+
+ it('preserves the rendered brand name (regression check)', () => {
+ const tree = renderer
+ .create(<Brand brandName="My Branded Service" />)
+ .toJSON();
+ expect(JSON.stringify(tree)).toContain('My Branded Service');
+ });
+
+ it('preserves the SVG element with the expected viewBox (regression check)', () => {
+ const tree = renderer
+ .create(<Brand brandName="BBC News" />)
+ .toJSON();
+ const svg = findByType(tree, 'svg');
+ expect(svg).not.toBeNull();
+ expect(svg.props.viewBox).toBe('0 0 167.95 24');
+ });
+});
diff --git a/test_commands.json b/test_commands.json
new file mode 100644
index 000000000..4393faeba
--- /dev/null
+++ b/test_commands.json
@@ -0,0 +1,3 @@
+{
+ "test_commands": ["npx jest packages/components/psammead-brand/src/print.test.jsx --verbose"]
+}
diff --git a/mimo_test_command.sh b/mimo_test_command.sh
new file mode 100755
--- /dev/null
+++ b/mimo_test_command.sh
@@ -0,0 +1,8 @@
+#!/usr/bin/env bash
+
+set -uo pipefail
+cd /testbed
+rc=0
+npx jest packages/components/psammead-brand/src/print.test.jsx --verbose
+rc=$(( rc | $? ))
+exit $rc
|