try to test App

This commit is contained in:
Moriz Wahl 2021-10-28 10:55:46 +02:00
parent d889df51fe
commit b15b509e5e
3 changed files with 84 additions and 13 deletions

67
admin/src/App.spec.js Normal file
View File

@ -0,0 +1,67 @@
import { mount } from '@vue/test-utils'
import App from './App'
const localVue = global.localVue
const storeCommitMock = jest.fn()
const mocks = {
$store: {
commit: storeCommitMock,
},
}
const storageMock = () => {
let storage = {}
return {
setItem: function(key, value) {
console.log('SET CALLED')
storage[key] = value || ''
},
getItem: function(key) {
console.log('GET CALLED')
return key in storage ? storage[key] : null
}
}
}
// window.localStorage = storageMock()
describe('App', () => {
let wrapper
const Wrapper = () => {
return mount(App, { localVue, mocks })
}
describe('mount', () => {
beforeEach(() => {
wrapper = Wrapper()
})
it('has a div with id "app"', () => {
expect(wrapper.find('div#app').exists()).toBeTruthy()
})
})
describe('window localStorage is undefined', () => {
it('does not commit a token to the store', () => {
expect(storeCommitMock).not.toBeCalled()
})
})
describe('with token in local storage', () => {
beforeEach(() => {
console.log('Test', window.localStorage)
window.localStorage = { 'foo': 'bar' }
console.log('Test', window.localStorage)
//window.localStorage.setItem('vuex', { token: 1234 })
})
it('commits the token to the store', () => {
expect(storeCommitMock).toBeCalledWith('token', 1234)
})
})
})

View File

@ -3,17 +3,18 @@
</template>
<script>
export default {
name: 'App',
methods: {
async readToken() {
if (window.localStorage && window.localStorage.vuex) {
this.$store.commit('token', JSON.parse(window.localStorage.vuex).token)
}
},
},
created() {
this.readToken()
},
}
export default {
name: 'App',
methods: {
async readToken() {
console.log('App', window.localStorage, window.localStorage.getItem('vuex'))
if (window.localStorage && window.localStorage.vuex) {
this.$store.commit('token', JSON.parse(window.localStorage.vuex).token)
}
},
},
created() {
this.readToken()
},
}
</script>

View File

@ -2,6 +2,9 @@ import { createLocalVue } from '@vue/test-utils'
import Vue from 'vue'
import { BootstrapVue } from 'bootstrap-vue'
// without this async calls are not working
import 'regenerator-runtime'
global.localVue = createLocalVue()
global.localVue.use(BootstrapVue)