diff --git a/packages/components/psammead-story-promo/src/layout.test.jsx b/packages/components/psammead-story-promo/src/layout.test.jsx new file mode 100644 index 000000000..19d67112d --- /dev/null +++ b/packages/components/psammead-story-promo/src/layout.test.jsx @@ -0,0 +1,218 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import { latin } from '@bbc/gel-foundations/scripts'; +import StoryPromo, { Headline, Summary, Link } from './index'; + +const Image = Alt text; + +const Info = ({ promoType }) => ( + <> + + The headline + + + The summary + + + +); + +const renderLeading = () => { + const { container } = render( + } + promoType="leading" + />, + ); + return container; +}; + +const getCss = () => + Array.from(document.querySelectorAll('style')) + .map(s => s.textContent || '') + .join('\n'); + +const hashClass = el => { + const classes = (el.getAttribute('class') || '').split(/\s+/).filter(Boolean); + + return classes.filter(c => !c.includes('-sc-'))[0] || classes[classes.length - 1]; +}; + +const escRe = s => s.replace(/[.()/\\+*?^$|[\]{}]/g, m => `\\${m}`); + + + + + +const matchInMedia = (css, cls, minWidth, needle) => { + const pattern = new RegExp( + `@media\\s*\\(min-width:\\s*${escRe( + minWidth, + )}\\)(?:\\s*and\\s*\\(max-width:[^)]+\\))?\\s*\\{[^}]*\\.${cls}\\s*\\{[^}]*${escRe( + needle, + )}`, + ); + return pattern.test(css); +}; + + + + +const hasGridColumn1Span = (css, cls, minWidth, n) => { + const blockPattern = new RegExp( + `@media\\s*\\(min-width:\\s*${escRe( + minWidth, + )}\\)(?:\\s*and\\s*\\(max-width:[^)]+\\))?\\s*\\{([^}]*\\.${cls}\\s*\\{[^}]*\\})`, + 'g', + ); + const bad = new RegExp( + `\\.${cls}\\s*\\{[^}]*grid-column:\\s*1\\s*/\\s*span\\s+${n}\\b`, + ); + let m; + // eslint-disable-next-line no-cond-assign + while ((m = blockPattern.exec(css))) { + if (bad.test(m[1])) return true; + } + return false; +}; + +describe('StoryPromo - Leading layout', () => { + it('renders TextGridItem (info) before ImageGridItem (image) in DOM order', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + expect(wrapper.children).toHaveLength(2); + const first = wrapper.children[0]; + const second = wrapper.children[1]; + + expect(first.querySelector('h3')).not.toBeNull(); + expect(first.querySelector('img')).toBeNull(); + expect(second.querySelector('img')).not.toBeNull(); + expect(second.querySelector('h3')).toBeNull(); + }); + + it('uses 6/6 grid columns at default (mobile, < 600px) for both items', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + + + + + + expect(css).toMatch(new RegExp(`\\.${textCls}\\s*\\{[^}]*span\\s+6\\b`)); + expect(css).toMatch(new RegExp(`\\.${imageCls}\\s*\\{[^}]*span\\s+6\\b`)); + }); + + it('uses 3/3 grid columns at min-width 37.5rem (600px) for both items', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + expect(matchInMedia(css, textCls, '37.5rem', 'span 3')).toBe(true); + expect(matchInMedia(css, imageCls, '37.5rem', 'span 3')).toBe(true); + + + + + + expect(hasGridColumn1Span(css, imageCls, '37.5rem', 3)).toBe(false); + }); + + it('uses 2/4 grid columns at min-width 63rem (1008px)', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + + expect(matchInMedia(css, textCls, '63rem', 'span 2')).toBe(true); + + expect(matchInMedia(css, imageCls, '63rem', 'span 4')).toBe(true); + + + + expect(hasGridColumn1Span(css, imageCls, '63rem', 4)).toBe(false); + }); + + it('TextGridItem fallback width is 100% by default (no @supports)', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const css = getCss(); + + + expect(css).toMatch( + new RegExp(`\\.${textCls}\\s*\\{[^}]*width:\\s*100%`), + ); + }); + + it('TextGridItem fallback width is 50% at min-width 37.5rem', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const css = getCss(); + + expect(matchInMedia(css, textCls, '37.5rem', 'width:50%')).toBe(true); + }); + + it('TextGridItem fallback width is 33.33% at min-width 63rem', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const textCls = hashClass(wrapper.children[0]); + const css = getCss(); + + expect(matchInMedia(css, textCls, '63rem', 'width:33.33%')).toBe(true); + }); + + it('ImageGridItem fallback width is 100% by default', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + expect(css).toMatch( + new RegExp(`\\.${imageCls}\\s*\\{[^}]*width:\\s*100%`), + ); + }); + + it('ImageGridItem fallback width is 50% at min-width 37.5rem', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + expect(matchInMedia(css, imageCls, '37.5rem', 'width:50%')).toBe(true); + }); + + it('ImageGridItem fallback width is 66.67% at min-width 63rem', () => { + const container = renderLeading(); + const wrapper = container.firstChild; + const imageCls = hashClass(wrapper.children[1]); + const css = getCss(); + + expect(matchInMedia(css, imageCls, '63rem', 'width:66.67%')).toBe(true); + }); +}); + +describe('StoryPromo - Regular promo unaffected', () => { + it('regular promo still renders image before info', () => { + const { container } = render( + } + promoType="regular" + />, + ); + const wrapper = container.firstChild; + expect(wrapper.children).toHaveLength(2); + expect(wrapper.children[0].querySelector('img')).not.toBeNull(); + expect(wrapper.children[1].querySelector('h3')).not.toBeNull(); + }); +}); diff --git a/test_commands.json b/test_commands.json new file mode 100644 index 000000000..9d666d9bf --- /dev/null +++ b/test_commands.json @@ -0,0 +1,3 @@ +{ + "test_commands": ["npx jest packages/components/psammead-story-promo/src/layout.test.jsx --colors=false"] +} 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-story-promo/src/layout.test.jsx --colors=false +rc=$(( rc | $? )) +exit $rc