Skip to content

Instantly share code, notes, and snippets.

@gangsthub
Last active July 6, 2020 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gangsthub/35f373940c99b1ce894740fed6ee3854 to your computer and use it in GitHub Desktop.
Save gangsthub/35f373940c99b1ce894740fed6ee3854 to your computer and use it in GitHub Desktop.
Twitter
/// <reference types="jest" />
import { mount } from '@vue/test-utils'
import IntersectionComponent from './IntersectionComponent.vue'
const mountComponent = props =>
mount(IntersectionComponent, {
propsData: {
callback: jest.fn().mockImplementation(isVisible => isVisible),
...props
}
})
describe('IntersectionComponent', () => {
let wrapper
beforeEach(() => {
wrapper = mountComponent()
})
it('reacts after the callback is called (by the directive)', () => {
// act
wrapper.vm.visibilityOptions.callback(true)
// assert
expect(wrapper.emitted()['visibility-changed'][0]).toEqual([true])
expect(wrapper.vm.isVisible).toBe(true)
})
})
<template>
<component :is="tag" v-observe-visibility="!disable && visibilityOptions">
<slot />
</component>
</template>
<script>
import { pipe } from '../utils.js'
import { ObserveVisibility } from 'vue-observe-visibility'
export default {
name: 'IntersectionComponent',
directives: {
ObserveVisibility
},
props: {
tag: {
type: String,
default: 'div'
},
// from here on, you'll find all the options from
// https://github.com/Akryum/vue-observe-visibility#usage
callback: {
type: Function,
required: true
},
once: {
type: Boolean,
default: true
},
throttle: {
type: Number,
default: 300
},
throttleOptions: {
type: Object,
default: () => null
},
intersection: {
type: Object,
default: () => ({
rootMargin: '100px'
})
},
disable: {
type: Boolean,
default: false
}
},
data() {
return {
isVisible: false
}
},
computed: {
visibilityOptions() {
return {
callback: pipe(this.saveState, this.emit, this.callback),
once: this.once,
throttle: this.throttle,
throttleOptions: this.throttleOptions,
intersection: this.intersection
}
}
},
methods: {
emit(isVisible) {
this.$emit('visibility-changed', isVisible)
return isVisible // for "chaining" executions
},
saveState(isVisible) {
this.isVisible = isVisible
return isVisible // for "chaining" executions
}
}
}
</script>
declare module '*.vue' {
import Vue from 'vue';
namespace $store {
import { Store } from 'vuex';
}
namespace $router {
import Router from 'vue-router';
}
export default Vue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment