-
Notifications
You must be signed in to change notification settings - Fork 8
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
268 additions
and
22 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/pages/apps/[id]/environments/[env-id]/config/_components/TabAPIKey.spec.tsx
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,97 @@ | ||
import type { Scope } from "nock"; | ||
import { RenderResult, waitFor } from "@testing-library/react"; | ||
import { fireEvent, render } from "@testing-library/react"; | ||
import mockApp from "~/testing/data/mock_app"; | ||
import mockEnvironments from "~/testing/data/mock_environments"; | ||
import { | ||
mockFetchAPIKeys, | ||
mockDeleteAPIKey, | ||
} from "~/testing/nocks/nock_api_keys"; | ||
import TabAPIKey from "./TabAPIKey"; | ||
|
||
interface WrapperProps { | ||
app?: App; | ||
environments?: Environment[]; | ||
hash?: string; | ||
setRefreshToken?: () => void; | ||
} | ||
|
||
describe("~/pages/apps/[id]/environments/[env-id]/config/_components/TabAPIKey.tsx", () => { | ||
let wrapper: RenderResult; | ||
let fetchScope: Scope; | ||
let currentApp: App; | ||
let currentEnv: Environment; | ||
let currentEnvs: Environment[]; | ||
|
||
const createWrapper = ({ app, environments }: WrapperProps) => { | ||
currentApp = app || mockApp(); | ||
currentApp.id = "1644802351"; // Same as api key id | ||
|
||
currentEnvs = environments || mockEnvironments({ app: currentApp }); | ||
currentEnv = currentEnvs[0]; | ||
|
||
fetchScope = mockFetchAPIKeys({ | ||
appId: currentApp.id, | ||
envId: currentEnv.id!, | ||
}); | ||
|
||
wrapper = render(<TabAPIKey app={currentApp} environment={currentEnv} />); | ||
}; | ||
|
||
test("should fetch api keys", async () => { | ||
createWrapper({}); | ||
|
||
await waitFor(() => { | ||
expect(fetchScope.isDone()).toBe(true); | ||
}); | ||
|
||
const subheader = | ||
"This key will allow you to interact with our API and modify this environment."; | ||
|
||
// Header | ||
expect(wrapper.getByText("API Key")).toBeTruthy(); | ||
expect(wrapper.getByText(subheader)).toBeTruthy(); | ||
|
||
// API Keys | ||
expect(wrapper.getByText("Default")).toBeTruthy(); | ||
expect(wrapper.getByText("CI")).toBeTruthy(); | ||
}); | ||
|
||
test("should delete api key", async () => { | ||
createWrapper({}); | ||
|
||
await waitFor(() => { | ||
expect(wrapper.getByText("CI")).toBeTruthy(); | ||
}); | ||
|
||
fireEvent.click(wrapper.getAllByLabelText("Remove API Key").at(1)!); | ||
|
||
await waitFor(() => { | ||
expect(wrapper.getByText("Confirm action")).toBeTruthy(); | ||
}); | ||
|
||
const scope = mockDeleteAPIKey({ | ||
appId: currentApp.id, | ||
envId: currentEnv.id!, | ||
keyId: "9868814106", | ||
}); | ||
|
||
// Should refetch api keys | ||
const fetchScope2 = mockFetchAPIKeys({ | ||
appId: currentApp.id, | ||
envId: currentEnv.id!, | ||
response: { keys: [] }, | ||
}); | ||
|
||
fireEvent.click(wrapper.getByText("Yes, continue")); | ||
|
||
await waitFor(() => { | ||
expect(scope.isDone()).toBe(true); | ||
expect(fetchScope2.isDone()).toBe(true); | ||
// Should close modal | ||
expect(() => wrapper.getByText("Confirm action")).toThrow(); | ||
// Should no longer have API keys | ||
expect(() => wrapper.getByText("Default")).toThrow(); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -4,4 +4,5 @@ declare interface APIKey { | |
appId: string; | ||
token: string; | ||
envId?: string; | ||
scope: "env"; | ||
} |
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,26 @@ | ||
interface APIKeyResponse { | ||
keys: APIKey[]; | ||
} | ||
|
||
export default (): APIKeyResponse => ({ | ||
keys: [ | ||
{ | ||
appId: "1644802351", | ||
envId: "1429333243019", | ||
id: "8224011755", | ||
scope: "env", | ||
name: "Default", | ||
token: | ||
"SK_fkopNcPhEizN5sV7cZx69tQ1VJZSV6ZiYBdpt6qiojkOB6xbUkCfGbBjR9IxqK", | ||
}, | ||
{ | ||
appId: "1644802351", | ||
envId: "1429333243019", | ||
id: "9868814106", | ||
scope: "env", | ||
name: "CI", | ||
token: | ||
"SK_m9nvOqzHFXiNrGEzvEQfdJK6isRCLnHpUulb5URvaSMme8fDeNUzPdhzVQLczu", | ||
}, | ||
], | ||
}); |
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,40 @@ | ||
import nock from "nock"; | ||
import * as data from "../data"; | ||
|
||
const endpoint = process.env.API_DOMAIN || ""; | ||
|
||
interface MockFetchAPIKeysProps { | ||
appId: string; | ||
envId: string; | ||
status?: number; | ||
response?: { keys: APIKey[] }; | ||
} | ||
|
||
export const mockFetchAPIKeys = ({ | ||
appId, | ||
envId, | ||
status = 200, | ||
response = data.mockAPIKeysResponse(), | ||
}: MockFetchAPIKeysProps) => { | ||
return nock(endpoint) | ||
.get(`/app/${appId}/env/${envId}/api-keys`) | ||
.reply(status, response); | ||
}; | ||
|
||
interface MockDeleteAPIKeyProps { | ||
appId: string; | ||
envId: string; | ||
keyId: string; | ||
status?: number; | ||
} | ||
|
||
export const mockDeleteAPIKey = ({ | ||
appId, | ||
envId, | ||
keyId, | ||
status = 200, | ||
}: MockDeleteAPIKeyProps) => { | ||
return nock(endpoint) | ||
.delete(`/app/${appId}/env/${envId}/api-key?keyId=${keyId}`) | ||
.reply(status, { ok: true }); | ||
}; |