Skip to content

Commit

Permalink
Merge pull request #10824 from qmonmert/related/10822
Browse files Browse the repository at this point in the history
Vue: add tests for AxiosHttp
  • Loading branch information
pascalgrimaud authored Sep 14, 2024
2 parents 05dd9af + 07e3b88 commit b825bb2
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -110,6 +111,8 @@ public JHipsterModule buildVueModule(JHipsterModuleProperties properties) {
.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
Expand Down
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
Original file line number Diff line number Diff line change
@@ -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>;
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ void shouldCreateVueModule() {
.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
}

Expand Down

0 comments on commit b825bb2

Please sign in to comment.