Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 3976dca

Browse files
committed
fix(highlight): gracefully display empty string if attr is not available
1 parent 06e7c1f commit 3976dca

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

packages/vue-instantsearch-highlight/src/Highlight.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
const attributeName = ctx.props.attributeName
2828
const tagName = ctx.props.tagName
2929
30-
if (!result._highlightResult || !result._highlightResult[attributeName]) {
31-
throw new Error(`Attribute ${attributeName} is not highlighted.`)
32-
}
30+
let attributeValue = ''
31+
if (result._highlightResult && result._highlightResult[attributeName]) {
32+
attributeValue = result._highlightResult[attributeName].value
33+
}
3334
34-
let attributeValue = result._highlightResult[attributeName].value
3535
if (ctx.props.escapeHtml === true) {
3636
attributeValue = escapeHtml(attributeValue)
3737
}

packages/vue-instantsearch-highlight/src/__tests__/__snapshots__/index.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ exports[`renders proper HTML 1`] = `
1717
<span class="ais-highlight">con
1818
<mark>ten</mark>t</span>
1919
`;
20+
21+
exports[`should render an empty string if attribute is not highlighted 1`] = `<span class="ais-highlight"></span>`;

packages/vue-instantsearch-highlight/src/__tests__/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,29 @@ test('allows unsafe output', () => {
109109
}
110110
}).$mount();
111111

112+
expect(vm.$el.outerHTML).toMatchSnapshot();
113+
});
114+
115+
test('should render an empty string if attribute is not highlighted', () => {
116+
const result = {
117+
_highlightResult: {}
118+
};
119+
120+
const vm = new Vue({
121+
template: '<highlight attributeName="attr" :result="result">',
122+
render(h) {
123+
return h('highlight', {
124+
props: {
125+
attributeName: 'attr',
126+
result: result
127+
}
128+
});
129+
},
130+
components: {
131+
Highlight
132+
}
133+
}).$mount();
134+
135+
112136
expect(vm.$el.outerHTML).toMatchSnapshot();
113137
});

0 commit comments

Comments
 (0)