mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-20 01:11:25 +00:00
Refactor Global state into Context Files (#128)
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import { AuthType } from "../AuthType";
|
||||
import { configContext, resetConfigContext, updateConfigContext } from "../ConfigContext";
|
||||
import { DatabaseAccount } from "../Contracts/DataModels";
|
||||
import { Collection } from "../Contracts/ViewModels";
|
||||
import DocumentId from "../Explorer/Tree/DocumentId";
|
||||
import { ResourceProviderClient } from "../ResourceProvider/ResourceProviderClient";
|
||||
import { updateUserContext } from "../UserContext";
|
||||
import {
|
||||
_createMongoCollectionWithARM,
|
||||
deleteDocument,
|
||||
getEndpoint,
|
||||
queryDocuments,
|
||||
readDocument,
|
||||
updateDocument
|
||||
updateDocument,
|
||||
_createMongoCollectionWithARM
|
||||
} from "./MongoProxyClient";
|
||||
import { AuthType } from "../AuthType";
|
||||
import { Collection } from "../Contracts/ViewModels";
|
||||
import { config } from "../Config";
|
||||
import { CosmosClient } from "./CosmosClient";
|
||||
import { ResourceProviderClient } from "../ResourceProvider/ResourceProviderClient";
|
||||
import DocumentId from "../Explorer/Tree/DocumentId";
|
||||
import { DatabaseAccount } from "../Contracts/DataModels";
|
||||
jest.mock("../ResourceProvider/ResourceProviderClient.ts");
|
||||
|
||||
const databaseId = "testDB";
|
||||
@@ -62,13 +62,15 @@ const databaseAccount = {
|
||||
tableEndpoint: "foo",
|
||||
cassandraEndpoint: "foo"
|
||||
}
|
||||
};
|
||||
} as DatabaseAccount;
|
||||
|
||||
describe("MongoProxyClient", () => {
|
||||
describe("queryDocuments", () => {
|
||||
beforeEach(() => {
|
||||
delete config.BACKEND_ENDPOINT;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
resetConfigContext();
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -88,7 +90,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("builds the correct proxy URL in development", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
queryDocuments(databaseId, collection, true, "{}");
|
||||
expect(window.fetch).toHaveBeenCalledWith(
|
||||
"https://localhost:1234/api/mongo/explorer/resourcelist?db=testDB&coll=testCollection&resourceUrl=bardbs%2FtestDB%2Fcolls%2FtestCollection%2Fdocs%2F&rid=testCollectionrid&rtype=docs&sid=&rg=&dba=foo&pk=pk",
|
||||
@@ -98,8 +100,10 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
describe("readDocument", () => {
|
||||
beforeEach(() => {
|
||||
delete config.MONGO_BACKEND_ENDPOINT;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
resetConfigContext();
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -119,7 +123,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("builds the correct proxy URL in development", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
readDocument(databaseId, collection, documentId);
|
||||
expect(window.fetch).toHaveBeenCalledWith(
|
||||
"https://localhost:1234/api/mongo/explorer?db=testDB&coll=testCollection&resourceUrl=bardb%2FtestDB%2Fdb%2FtestCollection%2FtestId&rid=testId&rtype=docs&sid=&rg=&dba=foo&pk=pk",
|
||||
@@ -129,8 +133,10 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
describe("createDocument", () => {
|
||||
beforeEach(() => {
|
||||
delete config.MONGO_BACKEND_ENDPOINT;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
resetConfigContext();
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -150,7 +156,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("builds the correct proxy URL in development", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
readDocument(databaseId, collection, documentId);
|
||||
expect(window.fetch).toHaveBeenCalledWith(
|
||||
"https://localhost:1234/api/mongo/explorer?db=testDB&coll=testCollection&resourceUrl=bardb%2FtestDB%2Fdb%2FtestCollection%2FtestId&rid=testId&rtype=docs&sid=&rg=&dba=foo&pk=pk",
|
||||
@@ -160,8 +166,10 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
describe("updateDocument", () => {
|
||||
beforeEach(() => {
|
||||
delete config.MONGO_BACKEND_ENDPOINT;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
resetConfigContext();
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -181,7 +189,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("builds the correct proxy URL in development", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
updateDocument(databaseId, collection, documentId, "{}");
|
||||
expect(window.fetch).toHaveBeenCalledWith(
|
||||
"https://localhost:1234/api/mongo/explorer?db=testDB&coll=testCollection&resourceUrl=bardb%2FtestDB%2Fdb%2FtestCollection%2Fdocs%2FtestId&rid=testId&rtype=docs&sid=&rg=&dba=foo&pk=pk",
|
||||
@@ -191,8 +199,10 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
describe("deleteDocument", () => {
|
||||
beforeEach(() => {
|
||||
delete config.MONGO_BACKEND_ENDPOINT;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
resetConfigContext();
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -212,7 +222,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("builds the correct proxy URL in development", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
deleteDocument(databaseId, collection, documentId);
|
||||
expect(window.fetch).toHaveBeenCalledWith(
|
||||
"https://localhost:1234/api/mongo/explorer?db=testDB&coll=testCollection&resourceUrl=bardb%2FtestDB%2Fdb%2FtestCollection%2Fdocs%2FtestId&rid=testId&rtype=docs&sid=&rg=&dba=foo&pk=pk",
|
||||
@@ -222,9 +232,11 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
describe("getEndpoint", () => {
|
||||
beforeEach(() => {
|
||||
delete config.MONGO_BACKEND_ENDPOINT;
|
||||
resetConfigContext();
|
||||
delete window.authType;
|
||||
CosmosClient.databaseAccount(databaseAccount as any);
|
||||
updateUserContext({
|
||||
databaseAccount
|
||||
});
|
||||
window.dataExplorer = {
|
||||
extensionEndpoint: () => "https://main.documentdb.ext.azure.com",
|
||||
serverId: () => ""
|
||||
@@ -237,7 +249,7 @@ describe("MongoProxyClient", () => {
|
||||
});
|
||||
|
||||
it("returns a development endpoint", () => {
|
||||
config.MONGO_BACKEND_ENDPOINT = "https://localhost:1234";
|
||||
updateConfigContext({ MONGO_BACKEND_ENDPOINT: "https://localhost:1234" });
|
||||
const endpoint = getEndpoint(databaseAccount as DatabaseAccount);
|
||||
expect(endpoint).toEqual("https://localhost:1234/api/mongo/explorer");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user