-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #289: Implement snackbar two ways * #289: Fix linting * #289: Add Snackbar to App.vue * #289: Add snackbar unit test and an example for calling from vuex
- Loading branch information
Showing
8 changed files
with
154 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
<template> | ||
<div id="app" class="page-container"> | ||
<router-view /> | ||
<Snackbar/> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import Snackbar from '@/components/Snackbar.vue' | ||
export default { | ||
components: { | ||
Snackbar | ||
} | ||
} | ||
</script> | ||
|
||
<style lang="scss"> | ||
@import 'assets/css/style.scss'; | ||
</style> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<template> | ||
<div> | ||
<md-snackbar :md-position="position" :md-active.sync="show"> | ||
{{message}} | ||
<span> | ||
<md-button v-if="action" id="snackbarAction" class="md-primary" @click.native="callAction">Retry</md-button> | ||
<md-button v-else id="snackbarRefresh" class="md-primary" @click.native="refresh">Refresh</md-button> | ||
<md-button id="snackbarClose" class="md-primary" @click.native="show = false">Close</md-button> | ||
</span> | ||
</md-snackbar> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { mapGetters } from 'vuex' | ||
export default { | ||
name: 'Snackbar', | ||
props: { | ||
position: { | ||
type: String, | ||
required: false, | ||
default: 'center' | ||
} | ||
}, | ||
data () { | ||
return { | ||
show: false, | ||
message: '', | ||
action: null | ||
} | ||
}, | ||
computed: { | ||
...mapGetters({ | ||
snackbar: 'getSnackbar' | ||
}) | ||
}, | ||
methods: { | ||
refresh () { | ||
this.$router.go(0) | ||
}, | ||
// If an action has been passed through vuex as a callback, call it | ||
callAction () { | ||
if (this.action) { | ||
this.action() | ||
} | ||
} | ||
}, | ||
watch: { | ||
snackbar (val, oldVal) { | ||
if (val.message) { | ||
this.show = true | ||
this.message = this.snackbar.message | ||
this.action = this.snackbar.action || null | ||
// Reset | ||
this.$store.commit('setSnackbar', '') | ||
} | ||
} | ||
} | ||
} | ||
</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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,8 @@ export default { | |
}, | ||
dialogBox (state) { | ||
return state.dialogBox | ||
}, | ||
getSnackbar (state) { | ||
return state.snackbar | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import createWrapper from '../../jest/script/wrapper' | ||
import { enableAutoDestroy } from '@vue/test-utils' | ||
import Snackbar from '@/components/Snackbar.vue' | ||
|
||
describe('@/components/Snackbar.vue', () => { | ||
const testMessage = 'Test snack message' | ||
let wrapper | ||
beforeEach(() => { | ||
wrapper = createWrapper(Snackbar, { }, false) | ||
wrapper.setData({ show: true }) | ||
}) | ||
|
||
enableAutoDestroy(afterEach) | ||
|
||
it('exists', () => { | ||
expect.assertions(1) | ||
expect(wrapper.exists()).toBe(true) | ||
}) | ||
|
||
it('contains a close button', async () => { | ||
expect.assertions(4) | ||
const close = wrapper.find('#snackbarClose') | ||
expect(close.exists()).toBe(true) | ||
expect(close.text()).toContain('Close') | ||
expect(wrapper.vm.show).toBe(true) | ||
await close.trigger('click') | ||
expect(wrapper.vm.show).toBe(false) | ||
}) | ||
|
||
it('contains a refresh button by default', async () => { | ||
expect.assertions(2) | ||
const refresh = wrapper.find('#snackbarRefresh') | ||
expect(refresh.exists()).toBe(true) | ||
expect(refresh.text()).toContain('Refresh') | ||
}) | ||
|
||
it('renders snackbar message from Vuex', async () => { | ||
expect.assertions(1) | ||
await wrapper.vm.$store.commit('setSnackbar', { | ||
message: testMessage | ||
}) | ||
wrapper.vm.$nextTick() | ||
expect(wrapper.text()).toContain(testMessage) | ||
}) | ||
|
||
it('changes available action when passed from Vuex', async () => { | ||
expect.assertions(3) | ||
const mockFn = jest.fn() | ||
await wrapper.vm.$store.commit('setSnackbar', { | ||
message: testMessage, | ||
action: mockFn | ||
}) | ||
wrapper.vm.$nextTick() | ||
const action = wrapper.find('#snackbarAction') | ||
expect(action.exists()).toBe(true) | ||
expect(action.text()).toContain('Retry') | ||
await action.trigger('click') | ||
expect(mockFn).toHaveBeenCalled() | ||
}) | ||
}) |