feat: Add frontend mocking using MockServiceWorker
Some checks failed
Build / build (pull_request) Failing after 8s
Check formatting / check-formatting (pull_request) Failing after 0s

This commit is contained in:
Magnus Leßmann (@MarkL4YG) 2025-01-07 12:56:39 +01:00
parent 95638d8ce8
commit b27623db26
Signed by: Mark.TwoFive
GPG key ID: E906A79D91006ECD
4 changed files with 35 additions and 6 deletions

View file

@ -73,7 +73,9 @@
rounded="lg" rounded="lg"
title="Internet certificate support" title="Internet certificate support"
variant="text" variant="text"
/> >
{{ loggedInUser?.name }}
</v-card>
</v-col> </v-col>
</v-row> </v-row>
</v-responsive> </v-responsive>
@ -81,5 +83,12 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import {onMounted, ref} from "vue";
const loggedInUser = ref<{name: string} | null>(null);
onMounted(async () => {
loggedInUser.value = await (await fetch('http://localhost:8080/api/v1/users/self')).json();
})
// //
</script> </script>

View file

@ -1,9 +1,14 @@
import { registerPlugins } from '@/plugins' import { registerPlugins } from '@/plugins'
import { setupBackendMocking } from '@/plugins/mock-service-worker';
import App from './App.vue' import App from './App.vue'
import { createApp } from 'vue' import { createApp } from 'vue'
if (import.meta.env.DEV) {
setupBackendMocking().then(worker => worker.start());
}
const app = createApp(App) const app = createApp(App)
registerPlugins(app) registerPlugins(app)

View file

@ -0,0 +1,19 @@
import {http, HttpResponse} from "msw";
import {setupWorker} from "msw/browser";
export const MOCK_BASEURL = "http://localhost:8080";
const setupHandlers = async () => {
return [
http.get(`${MOCK_BASEURL}/api/v1/users/self`, () => HttpResponse.json({
id: window.crypto.randomUUID(),
name: 'Max Mustermann',
mail: 'testmail@example.com',
})),
]
}
export async function setupBackendMocking() {
const handlers = await setupHandlers();
return setupWorker(...handlers);
}

View file

@ -1,7 +1,6 @@
import Components from 'unplugin-vue-components/vite' import Components from 'unplugin-vue-components/vite'
import Vue from '@vitejs/plugin-vue' import Vue from '@vitejs/plugin-vue'
import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify' import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import basicSSL from "@vitejs/plugin-basic-ssl";
import ViteFonts from 'unplugin-fonts/vite' import ViteFonts from 'unplugin-fonts/vite'
import VueRouter from 'unplugin-vue-router/vite' import VueRouter from 'unplugin-vue-router/vite'
@ -10,9 +9,6 @@ import { fileURLToPath, URL } from 'node:url'
export default defineConfig({ export default defineConfig({
plugins: [ plugins: [
basicSSL({
name: 'dev-cert'
}),
VueRouter(), VueRouter(),
Vue({ Vue({
template: { transformAssetUrls }, template: { transformAssetUrls },