-
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10824 from qmonmert/related/10822
Vue: add tests for AxiosHttp
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
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
120 changes: 120 additions & 0 deletions
120
...bapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosHttp.spec.ts.mustache
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,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'); | ||
}); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...ue/webapp/app/test/webapp/unit/shared/http/infrastructure/secondary/AxiosStub.ts.mustache
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,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>; |
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