Skip to content

Instantly share code, notes, and snippets.

@fraaalk
Last active June 19, 2019 08:42
Show Gist options
  • Select an option

  • Save fraaalk/0d034b00e4524e4d0163288e030b4d80 to your computer and use it in GitHub Desktop.

Select an option

Save fraaalk/0d034b00e4524e4d0163288e030b4d80 to your computer and use it in GitHub Desktop.
<script>
import _mixins from './mixins'
export default {
mixins: [..._mixins]
methods: {
activate() {
this.ensureJsLib('foo', 'bar')
}
}
}
</script>
export default {
methods: {
/**
* Checks for the lib name in the window object
* and loads the script source in case its missing
* @param {String} libName
* @param {String} libSource
*/
ensureJsLib(libName, libSource) {
if (!window[libName]) {
const externalLib = document.createElement('script')
externalLib.setAttribute('src', libSource)
document.head.appendChild(externalLib)
}
}
}
}
import _props from './props'
import _computed from './computed'
import _components from './components'
import _data from './data'
import _methods from './methods'
export default [
_data,
_components,
_props,
_computed,
_methods
]
import { shallowMount } from '@vue/test-utils'
import Component from '@/components/component.vue'
const factory = (params = {}) => {
return shallowMount(Component, params)
}
describe('Component', () => {
it('loads lib on activate', () => {
const spy = jest.spyOn(Component.methods, 'ensureJsLib')
const wrapper = factory()
wrapper.vm.activate()
expect(spy).toHaveBeenCalled()
spy.mockClear()
})
})
@fraaalk
Copy link
Author

fraaalk commented Jun 19, 2019

Cannot spy the ensureJsLib property because it is not a function; undefined given instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment