-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
312 additions
and
15 deletions.
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
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
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,91 @@ | ||
import { MockClient } from "../../http/MockClient"; | ||
import { ScheduleClient } from "../ScheduleClient"; | ||
import { GaroonRequestConfigBuilder } from "../../GaroonRequestConfigBuilder"; | ||
import { errorResponseHandler } from "../../GaroonRestAPIClient"; | ||
|
||
describe("ScheduleClient", () => { | ||
let mockClient: MockClient; | ||
let scheduleClient: ScheduleClient; | ||
|
||
beforeEach(() => { | ||
const requestConfigBuilder = new GaroonRequestConfigBuilder({ | ||
baseUrl: "https://example.cybozu.com/g", | ||
auth: { | ||
type: "session", | ||
}, | ||
}); | ||
mockClient = new MockClient({ requestConfigBuilder, errorResponseHandler }); | ||
scheduleClient = new ScheduleClient(mockClient); | ||
}); | ||
|
||
describe("getEvent", () => { | ||
beforeEach(async () => { | ||
await scheduleClient.getEvent({ id: 1 }); | ||
}); | ||
it("should pass the path to the http client", () => { | ||
expect(mockClient.getLogs()[0].path).toBe("/api/v1/schedule/events/1"); | ||
}); | ||
it("should send a get request", () => { | ||
expect(mockClient.getLogs()[0].method).toBe("get"); | ||
}); | ||
it("should pass an empty object as a param to the http client", () => { | ||
expect(mockClient.getLogs()[0].params).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe("getEvents", () => { | ||
describe("without parameter", () => { | ||
beforeEach(async () => { | ||
await scheduleClient.getEvents(); | ||
}); | ||
it("should pass the path to the http client", () => { | ||
expect(mockClient.getLogs()[0].path).toBe("/api/v1/schedule/events"); | ||
}); | ||
it("should send a get request", () => { | ||
expect(mockClient.getLogs()[0].method).toBe("get"); | ||
}); | ||
it("should pass an empty object as a param to the http client", () => { | ||
expect(mockClient.getLogs()[0].params).toEqual({}); | ||
}); | ||
}); | ||
describe("with parameter", () => { | ||
beforeEach(async () => { | ||
await scheduleClient.getEvents({ | ||
limit: 100, | ||
offset: 0, | ||
fields: ["id", "creator"], | ||
orderBy: { | ||
property: "createdAt", | ||
order: "asc", | ||
}, | ||
rangeStart: "2017-10-19T00:10:30Z", | ||
rangeEnd: "2017-10-19T01:10:30Z", | ||
target: 1, | ||
targetType: "user", | ||
keyword: "test", | ||
excludeFromSearch: ["subject", "company"], | ||
}); | ||
}); | ||
it("should pass the path to the http client", () => { | ||
expect(mockClient.getLogs()[0].path).toBe("/api/v1/schedule/events"); | ||
}); | ||
it("should send a get request", () => { | ||
expect(mockClient.getLogs()[0].method).toBe("get"); | ||
}); | ||
it("should pass limit, offset, fields, orderBy, rangeStart, rangeEnd, target, targetType, keyword and excludeFromSearch as a param to the http client", () => { | ||
expect(mockClient.getLogs()[0].params).toEqual({ | ||
limit: 100, | ||
offset: 0, | ||
fields: "id,creator", | ||
orderBy: "createdAt asc", | ||
rangeStart: "2017-10-19T00:10:30Z", | ||
rangeEnd: "2017-10-19T01:10:30Z", | ||
target: 1, | ||
targetType: "user", | ||
keyword: "test", | ||
excludeFromSearch: "subject,company", | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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,123 @@ | ||
import { | ||
HttpClient, | ||
RequestConfigBuilder, | ||
ErrorResponseHandler, | ||
} from "./HttpClientInterface"; | ||
import FormData from "form-data"; | ||
|
||
type Log = { | ||
method: "get" | "post" | "put" | "delete"; | ||
path: string; | ||
params: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
|
||
export class MockClient implements HttpClient { | ||
private readonly errorResponseHandler: ErrorResponseHandler; | ||
private readonly requestConfigBuilder: RequestConfigBuilder; | ||
logs: Log[]; | ||
responses: Array<Record<string, unknown>>; | ||
|
||
constructor({ | ||
errorResponseHandler, | ||
requestConfigBuilder, | ||
}: { | ||
errorResponseHandler: ErrorResponseHandler; | ||
requestConfigBuilder: RequestConfigBuilder; | ||
}) { | ||
this.errorResponseHandler = errorResponseHandler; | ||
this.requestConfigBuilder = requestConfigBuilder; | ||
this.logs = []; | ||
this.responses = []; | ||
} | ||
|
||
public mockResponse(mock: Record<string, unknown>) { | ||
this.responses.push(mock); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
private createResponse<T extends object>(): T { | ||
const response = this.responses.shift() || {}; | ||
if (response instanceof Error) { | ||
this.errorResponseHandler(response); | ||
} | ||
return response as T; | ||
} | ||
|
||
public async get<T extends Record<string, unknown>>( | ||
path: string, | ||
params: any | ||
): Promise<T> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"get", | ||
path, | ||
params | ||
); | ||
this.logs.push({ method: requestConfig.method, path, params }); | ||
return this.createResponse<T>(); | ||
} | ||
public async getData(path: string, params: any): Promise<ArrayBuffer> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"get", | ||
path, | ||
params | ||
); | ||
this.logs.push({ method: requestConfig.method, path, params }); | ||
return this.createResponse<ArrayBuffer>(); | ||
} | ||
public async post<T extends Record<string, unknown>>( | ||
path: string, | ||
params: any | ||
): Promise<T> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"post", | ||
path, | ||
params | ||
); | ||
this.logs.push({ method: requestConfig.method, path, params }); | ||
return this.createResponse<T>(); | ||
} | ||
public async postData<T extends Record<string, unknown>>( | ||
path: string, | ||
formData: FormData | ||
): Promise<T> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"post", | ||
path, | ||
formData | ||
); | ||
this.logs.push({ | ||
method: requestConfig.method, | ||
path, | ||
params: { formData }, | ||
}); | ||
return this.createResponse<T>(); | ||
} | ||
public async put<T extends Record<string, unknown>>( | ||
path: string, | ||
params: any | ||
): Promise<T> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"put", | ||
path, | ||
params | ||
); | ||
this.logs.push({ method: requestConfig.method, path, params }); | ||
return this.createResponse<T>(); | ||
} | ||
public async delete<T extends Record<string, unknown>>( | ||
path: string, | ||
params: any | ||
): Promise<T> { | ||
const requestConfig = await this.requestConfigBuilder.build( | ||
"delete", | ||
path, | ||
params | ||
); | ||
this.logs.push({ method: requestConfig.method, path, params }); | ||
return this.createResponse<T>(); | ||
} | ||
public getLogs(): Log[] { | ||
return this.logs; | ||
} | ||
} |
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