Ocelot-Social/webapp/mixins/scrollToAnchor.spec.js
roschaefer 9da40c4895 fix: avoid many scrollTo calls for n components
Thank you @vbelolapotkov for pointing out the flaws here:
https://github.com/Human-Connection/Human-Connection/pull/1756#discussion_r329361572

So here is my attempt to fix it:
* Install `vue-scrollto` which relies on `requestAnimationFrame`
  - apparently this is better on Safari and IE? 🤔
  - Mocking out entire modules is easier in jest:
    https://jestjs.io/docs/en/bypassing-module-mocks
* Require `checkAnchor` to be implemented on the component
2019-10-01 11:55:18 +02:00

48 lines
1.1 KiB
JavaScript

import { scrollTo } from 'vue-scrollto'
import scrollToAnchor from './scrollToAnchor'
jest.mock('vue-scrollto')
let component
describe('scrollToAnchor', () => {
beforeEach(() => {
jest.useFakeTimers()
scrollTo.mockClear()
})
describe('scrollToAnchor', () => {
const action = hash => {
let {
watch: { $route },
} = scrollToAnchor
$route.bind(component)({ hash })
jest.runAllTimers()
}
describe('given anchor `commentId-4711`', () => {
beforeEach(() => {
component = {
anchor: 'commentId-4711',
checkAnchor(anchor) {
return this.anchor === anchor
},
}
})
describe('$route.hash === anchor', () => {
it('calls window.scroll', () => {
action('commentId-4711')
expect(scrollTo).toHaveBeenCalled()
})
})
describe('$route.hash !== anchor', () => {
it('skips window.scroll', () => {
action('commentId-4712')
expect(scrollTo).not.toHaveBeenCalled()
})
})
})
})
})