Skip to content

Instantly share code, notes, and snippets.

View aptiko's full-sized avatar

Antonis Christofides aptiko

View GitHub Profile
context.app.$hello = 'world'
Vue.prototype.$hello = 'world'
export default (context, inject) => {
inject('hello', 'world')
}
import Vue from 'vue'
Vue.prototype.$hello = 'world'
@aptiko
aptiko / vueplugin.js
Created June 18, 2021 11:10
Vue plugin essentials
import Vue from 'vue'
const myVuePlugin = {
install(Vue, options) {
Vue.prototype.$hello = 'world'
},
}
Vue.use(myVuePlugin)
myVueInstance = new Vue()
@aptiko
aptiko / LoginForm.test.js
Last active May 31, 2021 13:18
How to test custom Vue events
import { shallowMount } from '@vue/test-utils'
import LoginForm from '@/components/LoginForm.vue'
import { BootstrapVue, BModal } from 'bootstrap-vue'
describe('LoginForm', () => {
let wrapper
beforeAll(() => {
Vue.use(BootstrapVue)
const container = document.createElement('div')
@aptiko
aptiko / LoginForm.vue
Created May 31, 2021 12:54
Component needing testing of an event
<template>
<div>
<b-modal @shown="focusUsername">
<b-form-input
ref="username"
v-model="username"
:placeholder="Username"
></b-form-input>
</b-modal>
</div>
@aptiko
aptiko / initializingaxios.test.js
Created May 18, 2021 11:14
Initializing Axios
import LoginForm from '@/components/LoginForm.vue'
import axios from 'axios'
describe('LoginForm', () => {
let wrapper
beforeAll(() => {
wrapper = mount(LoginForm, {
mocks: {
$axios: axios,
@aptiko
aptiko / setupMsw.test.js
Created May 18, 2021 10:18
Setting up MSW with Jest
import { rest } from 'msw'
import { setupServer } from 'msw/node'
describe('LoginForm', () => {
let server
beforeAll(() => {
server = setupServer(
rest.post('/auth/login/', (request, response, context) => {
const username = request.body.username
@aptiko
aptiko / testwithmock.js
Created May 17, 2021 16:45
Testing store-related Nuxt functionality with mocking
import { mount } from '@vue/test-utils'
import Navbar from '@/components/Navbar.vue'
describe('Navbar', () => {
test('gets loggedOnUser from store', () => {
const wrapper = mount(Navbar, {
mocks: {
$store: { state: { loggedOnUser: 'alice' } },
},
})
expect(wrapper.vm.loggedOnUser).toBe('alice')
@aptiko
aptiko / maincode.js
Created May 17, 2021 16:44
Nuxt function that uses the store
export default {
computed: {
loggedOnUser() {
return this.$store.state.loggedOnUser
},
},
// ...
}