mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
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
48 lines
1.1 KiB
JavaScript
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()
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|