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().toJSON(); + const gClassTokens = getGClassTokens(tree); + const wrapperClassTokens = getWrapperClassTokens(tree); + const ast = getCSS(); + + expect( + hasValidPrintVisibilityFix(ast, gClassTokens, wrapperClassTokens), + ).toBe(true); + }); + + it('does not leave the fill="#fff" attribute unmitigated', () => { + + + + + + const tree = renderer.create().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() + .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() + .toJSON(); + expect(JSON.stringify(tree)).toContain('My Branded Service'); + }); + + it('preserves the SVG element with the expected viewBox (regression check)', () => { + const tree = renderer + .create() + .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