Repository: jhipster/jhipster-lite
Pull request #10824 (2024-09-13 20:38:12)

Vue: add tests for AxiosHttp
Related to #10822 

![image](https://github.com/user-attachments/assets/0f7d5412-552c-4bcb-abf0-c102cd69c341)



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 fe81ff5b0..82002221f 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
@@ -20,6 +20,7 @@ public class VueModulesFactory {
   private static final JHipsterSource SOURCE_COMMON = from("client/common");
 
   private static final JHipsterDestination MAIN_DESTINATION = to("src/main/webapp/app");
+  private static final JHipsterDestination TEST_DESTINATION = to("src/test/webapp");
 
   private static final String IMPORT_NEEDLE = "// jhipster-needle-main-ts-import";
   private static final String PROVIDER_NEEDLE = "// jhipster-needle-main-ts-provider";
@@ -114,6 +115,8 @@ public class VueModulesFactory {
           .and()
         .add(SOURCE.template("webapp/app/router.ts"), MAIN_DESTINATION.append("router.ts"))
         .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"))
         .and()
       .build();
     //@formatter:on


Test patch:
diff --git a/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts.mustache b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts.mustache
new file mode 100644
index 000000000..3276aa635
--- /dev/null
+++ b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts.mustache
@@ -0,0 +1,120 @@
+import { describe, it, expect } from 'vitest';
+import { AxiosResponse } from 'axios';
+import { AxiosHttp } from '@/shared/http/infrastructure/secondary/AxiosHttp';
+import { dataAxiosResponse, stubAxiosInstance } from './AxiosStub';
+
+interface Payload {
+  payload: string;
+}
+
+const fakePayload = (): Payload => ({
+  payload: 'content',
+});
+
+interface Result {
+  result: string;
+}
+
+const fakeResult = (): Result => ({
+  result: 'content',
+});
+
+const responseResult = (): AxiosResponse => dataAxiosResponse(fakeResult());
+
+const expectForQuerying = (uri: string, result: AxiosResponse<Result>) => {
+  expect(result.data).toEqual(fakeResult());
+  expect(uri).toBe('/uri');
+};
+
+describe('axiosHttp', () => {
+  describe('GET', () => {
+    it('should get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.get.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.get<Result>('/uri');
+
+      const [uri] = axiosInstance.get.getCall(0).args;
+      expect(result.data).toEqual(fakeResult());
+      expect(uri).toBe('/uri');
+    });
+
+    it('should get content with params', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.get.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      await axiosHttp.get<Result>('/uri', { params: { beer: 'chips' } });
+
+      const [, config] = axiosInstance.get.getCall(0).args;
+      expect(config.params.beer).toBe('chips');
+    });
+  });
+
+  describe('PUT', () => {
+    it('should only get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.put.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.put<Result>('/uri');
+
+      const [uri] = axiosInstance.put.getCall(0).args;
+      expect(result.data).toEqual(fakeResult());
+      expect(uri).toBe('/uri');
+    });
+
+    it('should send and get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.put.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.put<Result, Payload>('/uri', fakePayload());
+
+      const [uri, payload] = axiosInstance.put.getCall(0).args;
+      expect(payload).toEqual<Payload>(fakePayload());
+      expectForQuerying(uri, result);
+    });
+  });
+
+  describe('POST', () => {
+    it('should only get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.post.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.post<Result>('/uri');
+
+      const [uri] = axiosInstance.post.getCall(0).args;
+      expect(result.data).toEqual(fakeResult());
+      expect(uri).toBe('/uri');
+    });
+
+    it('should send and get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.post.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.post<Result, Payload>('/uri', fakePayload());
+
+      const [uri, payload] = axiosInstance.post.getCall(0).args;
+      expect(payload).toEqual<Payload>(fakePayload());
+      expectForQuerying(uri, result);
+    });
+  });
+
+  describe('DELETE', () => {
+    it('should get content', async () => {
+      const axiosInstance = stubAxiosInstance();
+      axiosInstance.delete.resolves(responseResult());
+      const axiosHttp = new AxiosHttp(axiosInstance);
+
+      const result = await axiosHttp.delete<Result>('/uri');
+
+      const [uri] = axiosInstance.delete.getCall(0).args;
+      expect(result.data).toEqual(fakeResult());
+      expect(uri).toBe('/uri');
+    });
+  });
+});
diff --git a/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache
new file mode 100644
index 000000000..ce73e6eb2
--- /dev/null
+++ b/src/main/resources/generator/client/vue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache
@@ -0,0 +1,22 @@
+import { AxiosInstance, AxiosResponse } from 'axios';
+import sinon, { SinonStub } from 'sinon';
+
+export interface AxiosStubInstance extends AxiosInstance {
+  get: SinonStub;
+  put: SinonStub;
+  post: SinonStub;
+  delete: SinonStub;
+}
+
+export const stubAxiosInstance = (): AxiosStubInstance =>
+  ({
+    get: sinon.stub(),
+    put: sinon.stub(),
+    post: sinon.stub(),
+    delete: sinon.stub(),
+  }) as AxiosStubInstance;
+
+export const dataAxiosResponse = <T>(data: T): AxiosResponse<T> =>
+  ({
+    data,
+  }) as AxiosResponse<T>;
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 42122ebf8..ac8f3e98a 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
@@ -81,7 +81,9 @@ class VueModulesFactoryTest {
       .hasPrefixedFiles("src/main/webapp/app", "env.d.ts", "AppVue.vue", "injections.ts", "router.ts", "main.ts")
       .hasPrefixedFiles("src/main/webapp/app/home","infrastructure/primary/HomepageVue.vue", "application/HomeRouter.ts")
       .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/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");
     //@formatter:on
   }