|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import { assert, describe, it, beforeEach } from "vitest"; |
| 9 | +import { apiClientProxifier } from "../../src/apiClientProxifier"; |
| 10 | +import type { RequestAdapter } from "../../src/requestAdapter"; |
| 11 | + |
| 12 | +// Mock RequestAdapter for testing |
| 13 | +class MockRequestAdapter implements RequestAdapter { |
| 14 | + public baseUrl = "https://example.com"; |
| 15 | + public send = () => Promise.resolve({}); |
| 16 | + public sendCollection = () => Promise.resolve([]); |
| 17 | + public sendEnum = () => Promise.resolve({}); |
| 18 | + public sendCollectionOfEnum = () => Promise.resolve([]); |
| 19 | + public sendCollectionOfPrimitive = () => Promise.resolve([]); |
| 20 | + public sendPrimitive = () => Promise.resolve({}); |
| 21 | + public sendNoResponseContent = () => Promise.resolve(); |
| 22 | + public enableBackingStore = () => {}; |
| 23 | + public getSerializationWriterFactory = () => ({}) as any; |
| 24 | + public convertToNativeRequest = () => Promise.resolve({} as any); |
| 25 | +} |
| 26 | + |
| 27 | +describe("apiClientProxifier", () => { |
| 28 | + let requestAdapter: RequestAdapter; |
| 29 | + let pathParameters: Record<string, unknown>; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + requestAdapter = new MockRequestAdapter(); |
| 33 | + pathParameters = { baseurl: "https://graph.microsoft.com/v1.0" }; |
| 34 | + }); |
| 35 | + |
| 36 | + describe("then property handling", () => { |
| 37 | + it("should return undefined when accessing 'then' property", () => { |
| 38 | + const navigationMetadata = { |
| 39 | + users: { |
| 40 | + requestsMetadata: { |
| 41 | + get: { |
| 42 | + uriTemplate: "{+baseurl}/users", |
| 43 | + adapterMethodName: "sendCollection" as const, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + const proxy = apiClientProxifier(requestAdapter, pathParameters, navigationMetadata); |
| 50 | + |
| 51 | + // Access the 'then' property - this should return undefined |
| 52 | + const thenProperty = (proxy as any).then; |
| 53 | + |
| 54 | + assert.isUndefined(thenProperty); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should not throw error when accessing 'then' property with navigation metadata", () => { |
| 58 | + const navigationMetadata = { |
| 59 | + users: { |
| 60 | + requestsMetadata: { |
| 61 | + get: { |
| 62 | + uriTemplate: "{+baseurl}/users", |
| 63 | + adapterMethodName: "sendCollection" as const, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + const proxy = apiClientProxifier(requestAdapter, pathParameters, navigationMetadata); |
| 70 | + |
| 71 | + // This should not throw an error |
| 72 | + assert.doesNotThrow(() => { |
| 73 | + const thenProperty = (proxy as any).then; |
| 74 | + return thenProperty; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should still throw error for unknown navigation properties other than 'then'", () => { |
| 79 | + const navigationMetadata = { |
| 80 | + users: { |
| 81 | + requestsMetadata: { |
| 82 | + get: { |
| 83 | + uriTemplate: "{+baseurl}/users", |
| 84 | + adapterMethodName: "sendCollection" as const, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + const proxy = apiClientProxifier(requestAdapter, pathParameters, navigationMetadata); |
| 91 | + |
| 92 | + assert.throws( |
| 93 | + () => { |
| 94 | + // Accessing an unknown property should still throw |
| 95 | + (proxy as any).unknownProperty; |
| 96 | + }, |
| 97 | + Error, |
| 98 | + "couldn't find navigation property unknownProperty", |
| 99 | + ); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments