Repository: jhipster/jhipster-lite
Pull request #10835 (2024-09-14 09:35:02)

Vue: add tests for HomeRouter
Related to #10822 

![image](https://github.com/user-attachments/assets/a86d8f29-dba9-4b66-8cba-c42c25b8b90f)



Patch:
diff --git a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java
index 6105ff42e..4183a93bc 100644
--- a/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java
+++ b/src/main/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactory.java
@@ -113,6 +113,7 @@ public class VueModulesFactory {
         .add(SOURCE.template("Dummy.spec.ts"), to("src/test/webapp/unit/Dummy.spec.ts"))
         .add(APP_SOURCE.template("test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts.mustache"), TEST_DESTINATION.append("unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts"))
         .add(APP_SOURCE.template("test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache"), TEST_DESTINATION.append("unit/shared/http/infrastructure/secondary/AxiosStub.ts"))
+        .add(APP_SOURCE.template("test/webapp/unit/router/HomeRouter.spec.ts.mustache"), TEST_DESTINATION.append("unit/router/HomeRouter.spec.ts"))
         .and()
       .build();
     //@formatter:on
diff --git a/src/main/resources/generator/client/vue/webapp/app/router.ts.mustache b/src/main/resources/generator/client/vue/webapp/app/router.ts.mustache
index 37d248cd3..9528a705d 100644
--- a/src/main/resources/generator/client/vue/webapp/app/router.ts.mustache
+++ b/src/main/resources/generator/client/vue/webapp/app/router.ts.mustache
@@ -1,7 +1,7 @@
 import { createRouter, createWebHistory } from 'vue-router';
 import { homeRoutes } from '@/home/application/HomeRouter';
 
-const routes = [...homeRoutes()];
+export const routes = [...homeRoutes()];
 
 const router = createRouter({
   history: createWebHistory(),


Test patch:
diff --git a/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/router/HomeRouter.spec.ts.mustache b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/router/HomeRouter.spec.ts.mustache
new file mode 100644
index 000000000..3e2359512
--- /dev/null
+++ b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/router/HomeRouter.spec.ts.mustache
@@ -0,0 +1,45 @@
+import { mount, VueWrapper } from '@vue/test-utils';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { createRouter, createWebHistory, Router } from 'vue-router';
+import { routes } from '@/router';
+import HomepageVue from '@/home/infrastructure/primary/HomepageVue.vue';
+
+let router: Router;
+beforeEach(async () => {
+  router = createRouter({
+    history: createWebHistory(),
+    routes,
+  });
+});
+
+const wrap = (): VueWrapper => {
+  return mount(HomepageVue, {
+    global: {
+      plugins: [router],
+    },
+  });
+};
+
+describe('Router', () => {
+  describe('Navigation on HomepageVue', () => {
+    it('should navigate on HomepageVue when the URL is /', async () => {
+      router.push('/');
+
+      await router.isReady();
+
+      const wrapper = wrap();
+
+      expect(wrapper.html()).toContain('Vue 3 + TypeScript + Vite');
+    });
+
+    it('should navigate on HomepageVue when the URL is /home', async () => {
+      router.push('/home');
+
+      await router.isReady();
+
+      const wrapper = wrap();
+
+      expect(wrapper.html()).toContain('Vue 3 + TypeScript + Vite');
+    });
+  });
+});
diff --git a/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java
index ac8f3e98a..30b4a5a07 100644
--- a/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java
+++ b/src/test/java/tech/jhipster/lite/generator/client/vue/core/domain/VueModulesFactoryTest.java
@@ -83,7 +83,8 @@ class VueModulesFactoryTest {
       .hasPrefixedFiles("src/main/webapp/content/images", "JHipster-Lite-neon-green.png", "VueLogo.png")
       .hasFiles("src/test/webapp/unit/Dummy.spec.ts")
       .hasFiles("src/test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts")
-      .hasFiles("src/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts");
+      .hasFiles("src/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts")
+      .hasFiles("src/test/webapp/unit/router/HomeRouter.spec.ts");
     //@formatter:on
   }