From 8159b95313f470edb9063440344d3b1110618111 Mon Sep 17 00:00:00 2001 From: James Chan Date: Fri, 16 Feb 2024 11:44:50 +0000 Subject: [PATCH] Fix failing tests --- app/assets/javascripts/live_search.js | 6 ++++-- spec/javascripts/live_search_spec.js | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index 6faaa3249a..f52e6ecc09 100644 --- a/app/assets/javascripts/live_search.js +++ b/app/assets/javascripts/live_search.js @@ -121,7 +121,8 @@ if (this.$resultsWrapper) { this.$resultsWrapper.setAttribute('data-search-query', this.currentKeywords()) var sortedBy = this.$resultsWrapper.querySelector('.js-order-results') - if (sortedBy) { + // Check that the sortedBy element exists and contains option elements + if (sortedBy && sortedBy.options.length > 0) { this.$resultsWrapper.setAttribute('data-ecommerce-variant', sortedBy.options[sortedBy.selectedIndex].text) } } @@ -147,7 +148,8 @@ if (this.$resultsWrapper) { this.$resultsWrapper.setAttribute('data-ga4-search-query', this.currentKeywords()) var sortedBy = this.$resultsWrapper.querySelector('.js-order-results') - if (sortedBy) { + // Check that the sortedBy element exists and contains option elements + if (sortedBy && sortedBy.options.length > 0) { this.$resultsWrapper.setAttribute('data-ga4-ecommerce-variant', sortedBy.options[sortedBy.selectedIndex].text) } } diff --git a/spec/javascripts/live_search_spec.js b/spec/javascripts/live_search_spec.js index e10834d274..0103ccb78e 100644 --- a/spec/javascripts/live_search_spec.js +++ b/spec/javascripts/live_search_spec.js @@ -635,7 +635,7 @@ describe('liveSearch', function () { liveSearch.state = { search: 'state' } expect($('#order option').length).toBe(2) - $('#order').remove() + $('option').remove() expect($('#order option').length).toBe(0) // We receive new data, which adds the sort options to the DOM. liveSearch.updateSortOptions(responseWithSortOptions, $.param(liveSearch.state))