| |
| new file mode 100644 |
| |
| |
| |
| @@ -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 = <img src="https://foobar.com/image.png" alt="Alt text" />; |
| + |
| +const Info = ({ promoType }) => ( |
| + <> |
| + <Headline script={latin} promoType={promoType} service="news"> |
| + <Link href="https://www.bbc.co.uk/news">The headline</Link> |
| + </Headline> |
| + <Summary script={latin} promoType={promoType} service="news"> |
| + The summary |
| + </Summary> |
| + <time>12 March 2019</time> |
| + </> |
| +); |
| + |
| +const renderLeading = () => { |
| + const { container } = render( |
| + <StoryPromo |
| + image={Image} |
| + info={<Info promoType="leading" />} |
| + 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( |
| + <StoryPromo |
| + image={Image} |
| + info={<Info promoType="regular" />} |
| + 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(); |
| + }); |
| +}); |
| |
| new file mode 100644 |
| |
| |
| |
| @@ -0,0 +1,3 @@ |
| +{ |
| + "test_commands": ["npx jest packages/components/psammead-story-promo/src/layout.test.jsx --colors=false"] |
| +} |
| |
| new file mode 100755 |
| |
| |
| @@ -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 |
|
|