Last active
July 6, 2020 23:36
-
-
Save paulmelero/35f373940c99b1ce894740fed6ee3854 to your computer and use it in GitHub Desktop.
Twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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