Skip to content

Commit d1520eb

Browse files
committed
add unit tests
1 parent c54405f commit d1520eb

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

packages/http/fetch/test/common/fetchRequestAdapter.ts

+60
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,24 @@ import { HttpClient } from "../../src/httpClient";
1313
import { getResponse } from "../testUtils";
1414
import { createMockEntityFromDiscriminatorValue } from "./mockEntity";
1515
import { MockParseNode, MockParseNodeFactory } from "./mockParseNodeFactory";
16+
import { JsonParseNode, JsonParseNodeFactory } from "@microsoft/kiota-serialization-json";
17+
import { TextParseNodeFactory } from "@microsoft/kiota-serialization-text";
18+
import { FormParseNodeFactory } from "@microsoft/kiota-serialization-form";
1619

1720
// eslint-disable-next-line no-var
1821
var Response = Response;
1922
if (typeof Response !== "object") {
2023
Response = getResponse();
2124
}
2225

26+
const TestEnumObject = {
27+
A: "a",
28+
B: "b",
29+
C: "c"
30+
} as const;
31+
32+
type TestEnum = (typeof TestEnumObject)[keyof typeof TestEnumObject];
33+
2334
describe("FetchRequestAdapter.ts", () => {
2435
describe("getClaimsFromResponse", () => {
2536
it("should get claims from response header", async () => {
@@ -124,6 +135,55 @@ describe("FetchRequestAdapter.ts", () => {
124135
});
125136
}
126137
});
138+
describe("send enum", () => {
139+
for (const statusCode of [200, 201, 202, 203]) {
140+
const enumResponse = "a";
141+
it(`should return object for status code ${statusCode}`, async () => {
142+
const mockHttpClient = new HttpClient();
143+
mockHttpClient.executeFetch = async (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>) => {
144+
const response = new Response(enumResponse, {
145+
status: statusCode,
146+
} as ResponseInit);
147+
response.headers.set("Content-Type", "text/plain");
148+
return Promise.resolve(response);
149+
};
150+
const mockFactory = new TextParseNodeFactory();//new JsonParseNodeFactory();
151+
const requestAdapter = new FetchRequestAdapter(new AnonymousAuthenticationProvider(), mockFactory, undefined, mockHttpClient);
152+
const requestInformation = new RequestInformation();
153+
requestInformation.URL = "https://www.example.com";
154+
requestInformation.httpMethod = HttpMethod.GET;
155+
const result = await requestAdapter.sendEnum(requestInformation, TestEnumObject, undefined);
156+
assert.isDefined(result);
157+
assert.equal(result, TestEnumObject.A);
158+
});
159+
}
160+
});
161+
describe("send and deserialize collection of enum", () => {
162+
for (const statusCode of [200, 201, 202, 203]) {
163+
const enumResponse = `["a","b","c","e","f"]`;
164+
it(`should return object for status code ${statusCode}`, async () => {
165+
const mockHttpClient = new HttpClient();
166+
mockHttpClient.executeFetch = async (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>) => {
167+
const response = new Response(enumResponse, {
168+
status: statusCode,
169+
} as ResponseInit);
170+
response.headers.set("Content-Type", "application/json");
171+
return Promise.resolve(response);
172+
};
173+
const mockFactory = new JsonParseNodeFactory();
174+
const requestAdapter = new FetchRequestAdapter(new AnonymousAuthenticationProvider(), mockFactory, undefined, mockHttpClient);
175+
const requestInformation = new RequestInformation();
176+
requestInformation.URL = "https://www.example.com";
177+
requestInformation.httpMethod = HttpMethod.GET;
178+
const result = await requestAdapter.sendCollectionOfEnum(requestInformation, TestEnumObject, undefined);
179+
assert.isDefined(result);
180+
assert.equal(result?.length, 3);
181+
assert.equal(result![0], TestEnumObject.A);
182+
assert.equal(result![1], TestEnumObject.B);
183+
assert.equal(result![2], TestEnumObject.C);
184+
});
185+
}
186+
});
127187
describe("Throws API error", () => {
128188
it("should throw API error", async () => {
129189
const mockHttpClient = new HttpClient();

0 commit comments

Comments
 (0)