forked from bcgov/supreme-court-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: snackbar store feat: raise snacks globally
- Loading branch information
Showing
6 changed files
with
118 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<template> | ||
<v-snackbar | ||
v-model="snackbarStore.isVisible" | ||
:timeout="-1" | ||
:color="snackbarStore.color" | ||
location="bottom right" | ||
> | ||
<h3>{{ snackbarStore.title }}</h3> | ||
<span>{{ snackbarStore.message }}</span> | ||
<template v-slot:actions> | ||
<v-icon class="mx-2" :icon="mdiCloseCircle" @click="close" /> | ||
</template> | ||
</v-snackbar> | ||
</template> | ||
|
||
<script setup> | ||
import { useSnackbarStore } from '@/stores/SnackBarStore'; | ||
import { mdiCloseCircle } from '@mdi/js'; | ||
const snackbarStore = useSnackbarStore(); | ||
const close = () => { | ||
snackbarStore.isVisible = false; | ||
}; | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { defineStore } from 'pinia'; | ||
import { ref } from 'vue'; | ||
|
||
export const useSnackbarStore = defineStore('snackbar', () => { | ||
const isVisible = ref(false); | ||
const message = ref(''); | ||
const color = ref('success'); | ||
const title = ref(''); | ||
|
||
const showSnackbar = (msg = '', col = 'success', ti = '') => { | ||
message.value = msg; | ||
color.value = col; | ||
title.value = ti; | ||
isVisible.value = true; | ||
}; | ||
|
||
return { isVisible, message, color, showSnackbar, title }; | ||
}); |
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,31 @@ | ||
import { mount } from '@vue/test-utils'; | ||
import { useSnackbarStore } from '@/stores/SnackBarStore'; | ||
import { describe, it, expect, beforeEach } from 'vitest'; | ||
import Snackbar from '@/components/shared/Snackbar.vue'; | ||
import { setActivePinia, createPinia } from 'pinia' | ||
|
||
describe('Snackbar.vue', () => { | ||
let store: ReturnType<typeof useSnackbarStore>; | ||
|
||
beforeEach(() => { | ||
setActivePinia(createPinia()); | ||
store = useSnackbarStore(); | ||
}); | ||
|
||
it('renders snackbar with correct props', () => { | ||
store.showSnackbar('Test message', 'error', 'Test title'); | ||
const wrapper = mount(Snackbar); | ||
|
||
expect(wrapper.find('h3').text()).toBe('Test title'); | ||
expect(wrapper.text()).toContain('Test message'); | ||
expect(store.isVisible).toBe(true); | ||
}); | ||
|
||
it('closes snackbar when close button is clicked', async () => { | ||
const wrapper = mount(Snackbar); | ||
|
||
await wrapper.find('v-snackbar__actions v-icon').trigger('click'); | ||
|
||
expect(store.isVisible).toBe(false); | ||
}); | ||
}); |
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,35 @@ | ||
import { setActivePinia, createPinia } from 'pinia' | ||
import { useSnackbarStore } from '@/stores/SnackBarStore'; | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
|
||
describe('SnackBarStore', () => { | ||
let store: ReturnType<typeof useSnackbarStore>; | ||
|
||
beforeEach(() => { | ||
setActivePinia(createPinia()) | ||
store = useSnackbarStore(); | ||
}); | ||
|
||
it('initializes with default values', () => { | ||
expect(store.isVisible).toBe(false); | ||
expect(store.message).toBe(''); | ||
expect(store.color).toBe('success'); | ||
expect(store.title).toBe(''); | ||
}); | ||
|
||
it('shows snackbar with given message, color, and title', () => { | ||
store.showSnackbar('Test message', 'error', 'Test title'); | ||
expect(store.isVisible).toBe(true); | ||
expect(store.message).toBe('Test message'); | ||
expect(store.color).toBe('error'); | ||
expect(store.title).toBe('Test title'); | ||
}); | ||
|
||
it('shows snackbar with default values when no arguments are passed', () => { | ||
store.showSnackbar(); | ||
expect(store.isVisible).toBe(true); | ||
expect(store.message).toBe(''); | ||
expect(store.color).toBe('success'); | ||
expect(store.title).toBe(''); | ||
}); | ||
}); |