From 3976dcaf967d04e109bda0b2442674b18bb8c12c Mon Sep 17 00:00:00 2001 From: Raymond Rutjes Date: Mon, 10 Apr 2017 16:43:08 +0200 Subject: [PATCH] fix(highlight): gracefully display empty string if attr is not available --- .../src/Highlight.vue | 8 +++---- .../src/__tests__/__snapshots__/index.js.snap | 2 ++ .../src/__tests__/index.js | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/vue-instantsearch-highlight/src/Highlight.vue b/packages/vue-instantsearch-highlight/src/Highlight.vue index 1011160b4..9a684b6bd 100644 --- a/packages/vue-instantsearch-highlight/src/Highlight.vue +++ b/packages/vue-instantsearch-highlight/src/Highlight.vue @@ -27,11 +27,11 @@ const attributeName = ctx.props.attributeName const tagName = ctx.props.tagName - if (!result._highlightResult || !result._highlightResult[attributeName]) { - throw new Error(`Attribute ${attributeName} is not highlighted.`) - } + let attributeValue = '' + if (result._highlightResult && result._highlightResult[attributeName]) { + attributeValue = result._highlightResult[attributeName].value + } - let attributeValue = result._highlightResult[attributeName].value if (ctx.props.escapeHtml === true) { attributeValue = escapeHtml(attributeValue) } diff --git a/packages/vue-instantsearch-highlight/src/__tests__/__snapshots__/index.js.snap b/packages/vue-instantsearch-highlight/src/__tests__/__snapshots__/index.js.snap index 9a7e15f99..291ca219e 100644 --- a/packages/vue-instantsearch-highlight/src/__tests__/__snapshots__/index.js.snap +++ b/packages/vue-instantsearch-highlight/src/__tests__/__snapshots__/index.js.snap @@ -17,3 +17,5 @@ exports[`renders proper HTML 1`] = ` con tent `; + +exports[`should render an empty string if attribute is not highlighted 1`] = ``; diff --git a/packages/vue-instantsearch-highlight/src/__tests__/index.js b/packages/vue-instantsearch-highlight/src/__tests__/index.js index 9afd8bce9..b082d13fe 100644 --- a/packages/vue-instantsearch-highlight/src/__tests__/index.js +++ b/packages/vue-instantsearch-highlight/src/__tests__/index.js @@ -109,5 +109,29 @@ test('allows unsafe output', () => { } }).$mount(); + expect(vm.$el.outerHTML).toMatchSnapshot(); +}); + +test('should render an empty string if attribute is not highlighted', () => { + const result = { + _highlightResult: {} + }; + + const vm = new Vue({ + template: '', + render(h) { + return h('highlight', { + props: { + attributeName: 'attr', + result: result + } + }); + }, + components: { + Highlight + } + }).$mount(); + + expect(vm.$el.outerHTML).toMatchSnapshot(); });