File size: 7,580 Bytes
7ce6911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 = <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();
+  });
+});
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