From 6c5db094254f9c022151a8c140f8d6d8e861b164 Mon Sep 17 00:00:00 2001 From: Phan An Date: Thu, 12 May 2022 11:29:55 +0200 Subject: [PATCH] feat(test): add BtnScrollToTop component tests --- .../components/ui/to-top-button.spec.ts | 2 +- .../js/components/ui/BtnScrollToTop.spec.ts | 22 +++++++ .../js/components/ui/BtnScrollToTop.vue | 53 ++++++++++++++++ .../js/components/ui/ScrollToTopButton.vue | 60 ------------------- .../__snapshots__/BtnScrollToTop.spec.ts.snap | 3 + .../js/composables/useInfiniteScroll.ts | 2 +- 6 files changed, 80 insertions(+), 62 deletions(-) create mode 100644 resources/assets/js/components/ui/BtnScrollToTop.spec.ts create mode 100644 resources/assets/js/components/ui/BtnScrollToTop.vue delete mode 100644 resources/assets/js/components/ui/ScrollToTopButton.vue create mode 100644 resources/assets/js/components/ui/__snapshots__/BtnScrollToTop.spec.ts.snap diff --git a/resources/assets/js/__tests__/components/ui/to-top-button.spec.ts b/resources/assets/js/__tests__/components/ui/to-top-button.spec.ts index 7d2dc645..c7535e26 100644 --- a/resources/assets/js/__tests__/components/ui/to-top-button.spec.ts +++ b/resources/assets/js/__tests__/components/ui/to-top-button.spec.ts @@ -1,4 +1,4 @@ -import Component from '@/components/ui/ScrollToTopButton.vue' +import Component from '../../../components/ui/BtnScrollToTop.vue' import { $ } from '@/utils' import { mock } from '@/__tests__/__helpers__' import { shallow } from '@/__tests__/adapter' diff --git a/resources/assets/js/components/ui/BtnScrollToTop.spec.ts b/resources/assets/js/components/ui/BtnScrollToTop.spec.ts new file mode 100644 index 00000000..8c581213 --- /dev/null +++ b/resources/assets/js/components/ui/BtnScrollToTop.spec.ts @@ -0,0 +1,22 @@ +import { expect, it } from 'vitest' +import { fireEvent } from '@testing-library/vue' +import { $ } from '@/utils' +import ComponentTestCase from '@/__tests__/ComponentTestCase' +import BtnScrollToTop from './BtnScrollToTop.vue' + +new class extends ComponentTestCase { + protected test () { + it('renders', () => { + expect(this.render(BtnScrollToTop).html()).toMatchSnapshot() + }) + + it('scrolls to top', async () => { + const mock = this.mock($, 'scrollTo') + const { getByTitle } = this.render(BtnScrollToTop) + + await fireEvent.click(getByTitle('Scroll to top')) + + expect(mock).toHaveBeenCalled() + }) + } +} diff --git a/resources/assets/js/components/ui/BtnScrollToTop.vue b/resources/assets/js/components/ui/BtnScrollToTop.vue new file mode 100644 index 00000000..d27dbf5c --- /dev/null +++ b/resources/assets/js/components/ui/BtnScrollToTop.vue @@ -0,0 +1,53 @@ + + + + + diff --git a/resources/assets/js/components/ui/ScrollToTopButton.vue b/resources/assets/js/components/ui/ScrollToTopButton.vue deleted file mode 100644 index 51460345..00000000 --- a/resources/assets/js/components/ui/ScrollToTopButton.vue +++ /dev/null @@ -1,60 +0,0 @@ - - - - - diff --git a/resources/assets/js/components/ui/__snapshots__/BtnScrollToTop.spec.ts.snap b/resources/assets/js/components/ui/__snapshots__/BtnScrollToTop.spec.ts.snap new file mode 100644 index 00000000..3fd4cf68 --- /dev/null +++ b/resources/assets/js/components/ui/__snapshots__/BtnScrollToTop.spec.ts.snap @@ -0,0 +1,3 @@ +// Vitest Snapshot v1 + +exports[`renders 1`] = `""`; diff --git a/resources/assets/js/composables/useInfiniteScroll.ts b/resources/assets/js/composables/useInfiniteScroll.ts index b357cbab..3816c931 100644 --- a/resources/assets/js/composables/useInfiniteScroll.ts +++ b/resources/assets/js/composables/useInfiniteScroll.ts @@ -6,7 +6,7 @@ import { defineAsyncComponent, ref } from 'vue' * the wrapper element: @scroll="scrolling" */ export const useInfiniteScroll = (perPage = 30) => { - const ToTopButton = defineAsyncComponent(() => import('@/components/ui/ScrollToTopButton.vue')) + const ToTopButton = defineAsyncComponent(() => import('@/components/ui/BtnScrollToTop.vue')) const scroller = ref() const displayedItemCount = ref(perPage)