Skip to content

Commit b5b3aed

Browse files
justinphstaylor
authored andcommitted
fix: fix methods, add tests
1 parent 0d3d2f4 commit b5b3aed

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

__tests__/server/server.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ describe('server', () => {
100100

101101
expect(styleComponent).toEqual(isArray);
102102
expect(styleComponent).toHaveLength(0);
103+
104+
expect(head.priority).toBeDefined();
105+
expect(head.priority.toString).toBeDefined();
106+
expect(head.priority.toString()).toEqual('');
107+
expect(head.priority.toComponent).toBeDefined();
103108
});
104109

105110
it('does not render undefined attribute values', () => {
@@ -214,6 +219,11 @@ describe('server', () => {
214219

215220
expect(styleComponent).toEqual(isArray);
216221
expect(styleComponent).toHaveLength(0);
222+
223+
expect(head.priority).toBeDefined();
224+
expect(head.priority.toString).toBeDefined();
225+
expect(head.priority.toString()).toEqual('');
226+
expect(head.priority.toComponent).toBeDefined();
217227
});
218228

219229
it('does not render undefined attribute values', () => {
@@ -229,5 +239,57 @@ describe('server', () => {
229239

230240
expect(script.toString()).toMatchSnapshot();
231241
});
242+
243+
it('prioritizes SEO tags when asked to', () => {
244+
const context = {};
245+
render(
246+
<Helmet prioritizeSeoTags>
247+
<link rel="notImportant" href="https://www.chipotle.com" />
248+
<link rel="canonical" href="https://www.tacobell.com" />
249+
<meta property="og:title" content="A very important title" />
250+
</Helmet>,
251+
context
252+
);
253+
254+
expect(context.helmet.priority.toString()).toContain(
255+
'rel="canonical" href="https://www.tacobell.com"'
256+
);
257+
expect(context.helmet.link.toString()).not.toContain(
258+
'rel="canonical" href="https://www.tacobell.com"'
259+
);
260+
261+
expect(context.helmet.priority.toString()).toContain(
262+
'property="og:title" content="A very important title"'
263+
);
264+
expect(context.helmet.meta.toString()).not.toContain(
265+
'property="og:title" content="A very important title"'
266+
);
267+
});
268+
269+
it('does not prioritize SEO unless asked to', () => {
270+
const context = {};
271+
render(
272+
<Helmet>
273+
<link rel="notImportant" href="https://www.chipotle.com" />
274+
<link rel="canonical" href="https://www.tacobell.com" />
275+
<meta property="og:title" content="A very important title" />
276+
</Helmet>,
277+
context
278+
);
279+
280+
expect(context.helmet.priority.toString()).not.toContain(
281+
'rel="canonical" href="https://www.tacobell.com"'
282+
);
283+
expect(context.helmet.link.toString()).toContain(
284+
'rel="canonical" href="https://www.tacobell.com"'
285+
);
286+
287+
expect(context.helmet.priority.toString()).not.toContain(
288+
'property="og:title" content="A very important title"'
289+
);
290+
expect(context.helmet.meta.toString()).toContain(
291+
'property="og:title" content="A very important title"'
292+
);
293+
});
232294
});
233295
});

src/server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ const mapStateOnServer = props => {
175175
let { linkTags, metaTags, scriptTags } = props;
176176
let priorityMethods = {
177177
toComponent: () => {},
178-
toString: () => {},
178+
toString: () => '',
179179
};
180+
console.log('prioritizeSeoTags', prioritizeSeoTags);
180181
if (prioritizeSeoTags) {
181182
({ priorityMethods, linkTags, metaTags, scriptTags } = getPriorityMethods(props));
182183
}

src/utils.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,17 @@ const getTagsFromPropsList = (tagName, primaryAttributes, propsList) => {
171171
.reverse();
172172
};
173173

174-
const getAnyTrueFromPropsList = (propsList, checkedTag) =>
175-
propsList.reduce((accumulator, currentPropList) =>
176-
currentPropList[checkedTag] ? true : accumulator
177-
);
174+
const getAnyTrueFromPropsList = (propsList, checkedTag) => {
175+
if (Array.isArray(propsList) && propsList.length) {
176+
for (let index = 0; index < propsList.length; index += 1) {
177+
const prop = propsList[index];
178+
if (prop[checkedTag]) {
179+
return true;
180+
}
181+
}
182+
}
183+
return false;
184+
};
178185

179186
const reducePropsToState = propsList => ({
180187
baseTag: getBaseTagFromPropsList([TAG_PROPERTIES.HREF], propsList),
@@ -208,9 +215,7 @@ const reducePropsToState = propsList => ({
208215
styleTags: getTagsFromPropsList(TAG_NAMES.STYLE, [TAG_PROPERTIES.CSS_TEXT], propsList),
209216
title: getTitleFromPropsList(propsList),
210217
titleAttributes: getAttributesFromPropsList(ATTRIBUTE_NAMES.TITLE, propsList),
211-
prioritizeSeoTags:
212-
propsList.prioritizeSeoTags &&
213-
getAnyTrueFromPropsList(propsList, HELMET_PROPS.PRIORITIZE_SEO_TAGS),
218+
prioritizeSeoTags: getAnyTrueFromPropsList(propsList, HELMET_PROPS.PRIORITIZE_SEO_TAGS),
214219
});
215220

216221
export const flattenArray = possibleArray =>

0 commit comments

Comments
 (0)