diff --git a/src/components/SearchGrid.vue b/src/components/SearchGrid.vue index 23b16dc226e..e1483030c38 100644 --- a/src/components/SearchGrid.vue +++ b/src/components/SearchGrid.vue @@ -1,40 +1,25 @@ diff --git a/src/pages/search.vue b/src/pages/search.vue index 23b14015a5e..bbc24517ce4 100644 --- a/src/pages/search.vue +++ b/src/pages/search.vue @@ -17,6 +17,10 @@ + @@ -40,6 +44,7 @@ import local from '~/utils/local' import { screenWidth } from '~/utils/get-browser-info' import { ALL_MEDIA, IMAGE } from '~/constants/media' import { mapActions, mapMutations } from 'vuex' +import debounce from 'lodash.debounce' const BrowsePage = { name: 'browse-page', @@ -56,6 +61,12 @@ const BrowsePage = { await this.setSearchTypeFromUrl({ url }) await this.setFiltersFromUrl({ url }) }, + data: () => ({ + showScrollButton: false, + }), + created() { + this.debounceScrollHandling = debounce(this.checkScrollLength, 100) + }, mounted() { const localFilterState = () => local.get(process.env.filterStorageKey) @@ -68,6 +79,10 @@ const BrowsePage = { this.setFilterVisibility({ isFilterVisible: isDesktop() ? localFilterState() : false, }) + window.addEventListener('scroll', this.debounceScrollHandling) + }, + beforeDestroy() { + window.removeEventListener('scroll', this.debounceScrollHandling) }, computed: { query() { @@ -113,6 +128,9 @@ const BrowsePage = { this.$route.path === '/search/' || this.$route.path === '/search/image' ) }, + checkScrollLength() { + this.showScrollButton = window.scrollY > 70 + }, }, watch: { query(newQuery) { diff --git a/test/unit/specs/components/search-grid-wrapper.spec.js b/test/unit/specs/components/search-grid-wrapper.spec.js index cedce2ac668..cd5bf29af7d 100644 --- a/test/unit/specs/components/search-grid-wrapper.spec.js +++ b/test/unit/specs/components/search-grid-wrapper.spec.js @@ -13,13 +13,5 @@ describe('Search Grid Wrapper', () => { it('renders correct content', () => { const wrapper = render(SearchGrid, options) expect(wrapper.find('[data-testid="search-grid"]').element).toBeDefined() - expect(wrapper.find('[data-testid="scroll-button"]').element).toBeDefined() - }) - - it('renders the scroll button when the page scrolls down', () => { - const wrapper = render(SearchGrid, options) - window.scrollY = 80 - wrapper.vm.checkScrollLength() - expect(wrapper.vm.showScrollButton).toBe(true) }) }) diff --git a/test/unit/specs/pages/browse-page.spec.js b/test/unit/specs/pages/browse-page.spec.js new file mode 100644 index 00000000000..c5caab767d0 --- /dev/null +++ b/test/unit/specs/pages/browse-page.spec.js @@ -0,0 +1,40 @@ +import BrowsePage from '~/pages/search' +import render from '../../test-utils/render' + +const options = { + stubs: { + AppModal: true, + FilterDisplay: true, + NuxtChild: true, + ScrollButton: true, + SearchGridFilter: true, + SearchGridForm: true, + SearchTypeTabs: true, + SearchGridManualLoad: true, + }, + mocks: { + $store: { + commit: jest.fn(), + state: { query: { q: 'foo' }, isFilterVisible: false }, + }, + $router: { path: { name: 'search-image' } }, + $route: { path: '/search/image' }, + }, +} + +describe('Search Grid Wrapper', () => { + it('renders correct content', () => { + const wrapper = render(BrowsePage, options) + expect(wrapper.find('[data-testid="scroll-button"]').element).toBeDefined() + window.scrollY = 50 + wrapper.vm.checkScrollLength() + expect(wrapper.vm.showScrollButton).toBe(false) + }) + + it('renders the scroll button when the page scrolls down', () => { + const wrapper = render(BrowsePage, options) + window.scrollY = 80 + wrapper.vm.checkScrollLength() + expect(wrapper.vm.showScrollButton).toBe(true) + }) +})