Skip to content

Commit 874fd0e

Browse files
authored
Merge pull request #13 from swiftype/dont-use-raw-values
Removed use of raw values in results
2 parents 05223ff + d19123f commit 874fd0e

File tree

5 files changed

+111
-30
lines changed

5 files changed

+111
-30
lines changed

src/components/Result.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ function Result({ fields, onClickLink, title, url }) {
55
return (
66
<li className="result">
77
<div className="result__header">
8-
{!url && (
9-
<span
10-
className="result__title"
11-
dangerouslySetInnerHTML={{ __html: title }}
12-
/>
13-
)}
14-
{url && (
15-
<a
16-
className="result__title"
17-
dangerouslySetInnerHTML={{ __html: title }}
18-
href={url}
19-
onClick={onClickLink}
20-
target="_blank"
21-
/>
22-
)}
8+
{title &&
9+
!url && (
10+
<span
11+
className="result__title"
12+
dangerouslySetInnerHTML={{ __html: title }}
13+
/>
14+
)}
15+
{title &&
16+
url && (
17+
<a
18+
className="result__title"
19+
dangerouslySetInnerHTML={{ __html: title }}
20+
href={url}
21+
onClick={onClickLink}
22+
target="_blank"
23+
/>
24+
)}
2325
</div>
2426
<div className="result__body">
2527
<ul className="result__details">
@@ -40,7 +42,7 @@ function Result({ fields, onClickLink, title, url }) {
4042

4143
Result.propTypes = {
4244
fields: PropTypes.object.isRequired,
43-
title: PropTypes.string.isRequired,
45+
title: PropTypes.string,
4446
url: PropTypes.string
4547
};
4648

src/components/Result.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import Result from "./Result";
33
import { shallow } from "enzyme";
44

55
const requiredProps = {
6-
fields: { field: "value" },
7-
title: "Title"
6+
fields: { field: "value" }
87
};
98

109
it("renders correctly when there is a URL", () => {
@@ -14,7 +13,19 @@ it("renders correctly when there is a URL", () => {
1413
expect(wrapper).toMatchSnapshot();
1514
});
1615

17-
it("renders correctly when there is not a URL", () => {
16+
it("renders correctly when there is not a URL or title", () => {
1817
const wrapper = shallow(<Result {...requiredProps} />);
1918
expect(wrapper).toMatchSnapshot();
2019
});
20+
21+
it("renders correctly when there is a title", () => {
22+
const wrapper = shallow(<Result {...requiredProps} title="Title" />);
23+
expect(wrapper).toMatchSnapshot();
24+
});
25+
26+
it("renders correctly when there is a title and url", () => {
27+
const wrapper = shallow(
28+
<Result {...requiredProps} title="Title" url="http://www.example.com" />
29+
);
30+
expect(wrapper).toMatchSnapshot();
31+
});

src/components/__snapshots__/Result.test.js.snap

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,50 @@ exports[`renders correctly when there is a URL 1`] = `
66
>
77
<div
88
className="result__header"
9+
/>
10+
<div
11+
className="result__body"
912
>
10-
<a
13+
<ul
14+
className="result__details"
15+
>
16+
<li
17+
key="field"
18+
>
19+
<span
20+
className="result__key"
21+
>
22+
field
23+
</span>
24+
25+
<span
26+
className="result__value"
27+
dangerouslySetInnerHTML={
28+
Object {
29+
"__html": "value",
30+
}
31+
}
32+
/>
33+
</li>
34+
</ul>
35+
</div>
36+
</li>
37+
`;
38+
39+
exports[`renders correctly when there is a title 1`] = `
40+
<li
41+
className="result"
42+
>
43+
<div
44+
className="result__header"
45+
>
46+
<span
1147
className="result__title"
1248
dangerouslySetInnerHTML={
1349
Object {
1450
"__html": "Title",
1551
}
1652
}
17-
href="http://www.example.com"
18-
target="_blank"
1953
/>
2054
</div>
2155
<div
@@ -47,20 +81,22 @@ exports[`renders correctly when there is a URL 1`] = `
4781
</li>
4882
`;
4983

50-
exports[`renders correctly when there is not a URL 1`] = `
84+
exports[`renders correctly when there is a title and url 1`] = `
5185
<li
5286
className="result"
5387
>
5488
<div
5589
className="result__header"
5690
>
57-
<span
91+
<a
5892
className="result__title"
5993
dangerouslySetInnerHTML={
6094
Object {
6195
"__html": "Title",
6296
}
6397
}
98+
href="http://www.example.com"
99+
target="_blank"
64100
/>
65101
</div>
66102
<div
@@ -91,3 +127,39 @@ exports[`renders correctly when there is not a URL 1`] = `
91127
</div>
92128
</li>
93129
`;
130+
131+
exports[`renders correctly when there is not a URL or title 1`] = `
132+
<li
133+
className="result"
134+
>
135+
<div
136+
className="result__header"
137+
/>
138+
<div
139+
className="result__body"
140+
>
141+
<ul
142+
className="result__details"
143+
>
144+
<li
145+
key="field"
146+
>
147+
<span
148+
className="result__key"
149+
>
150+
field
151+
</span>
152+
153+
<span
154+
className="result__value"
155+
dangerouslySetInnerHTML={
156+
Object {
157+
"__html": "value",
158+
}
159+
}
160+
/>
161+
</li>
162+
</ul>
163+
</div>
164+
</li>
165+
`;

src/config/config-helper.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ export function getUrlFieldTemplate() {
5353
export function getResultTitle(result) {
5454
const titleField = getTitleField();
5555

56-
return (
57-
result.getSnippet(titleField) ||
58-
result.getRaw(titleField) ||
59-
result.getRaw("id") // As a last resort, just show ID if nothing else
60-
);
56+
return result.getSnippet(titleField);
6157
}
6258

6359
export function getResultUrl(result) {

src/containers/Results.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function capitalizeFirstLetter(string) {
1919
*/
2020
function formatResultFields(result) {
2121
return Object.keys(result.data).reduce((acc, n) => {
22-
let value = result.getSnippet(n) || result.getRaw(n);
22+
let value = result.getSnippet(n);
2323
value = Array.isArray(value) ? value.join(", ") : value;
2424
acc[`${capitalizeFirstLetter(n)}`] = value;
2525
return acc;

0 commit comments

Comments
 (0)