-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor create-app prompts with tests (#32)
- Loading branch information
Showing
18 changed files
with
841 additions
and
216 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Refactor create-app prompts and add unit tests | ||
links: | ||
- https://github.com/palantir/osdk-ts/pull/32 |
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
76 changes: 76 additions & 0 deletions
76
packages/create-app/src/prompts/promptApplicationUrl.test.ts
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,76 @@ | ||
/* | ||
* Copyright 2023 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { afterEach, expect, test, vi } from "vitest"; | ||
import { consola } from "../consola.js"; | ||
import { promptApplicationUrl } from "./promptApplicationUrl.js"; | ||
|
||
vi.mock("../consola.js"); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
const valid = "https://app.com"; | ||
|
||
test("it accepts valid application url from prompt", async () => { | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("yes"); | ||
vi.mocked(consola).prompt.mockResolvedValueOnce(valid); | ||
expect(await promptApplicationUrl({})).toEqual(valid); | ||
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test("it prompts again if answered value is invalid", async () => { | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("yes"); | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("invalid"); | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("ftp://abc.com"); | ||
vi.mocked(consola).prompt.mockResolvedValueOnce(valid); | ||
expect(await promptApplicationUrl({})).toEqual(valid); | ||
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(4); | ||
}); | ||
|
||
test("it accepts valid initial value without prompt", async () => { | ||
expect(await promptApplicationUrl({ applicationUrl: valid })).toEqual(valid); | ||
expect(vi.mocked(consola).prompt).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test("it prompts if initial value is invalid", async () => { | ||
vi.mocked(consola).prompt.mockResolvedValueOnce(valid); | ||
expect(await promptApplicationUrl({ applicationUrl: "invalid" })).toEqual( | ||
valid, | ||
); | ||
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("it strips trailing slash from url", async () => { | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("yes"); | ||
vi.mocked(consola).prompt.mockResolvedValueOnce(valid + "/"); | ||
expect(await promptApplicationUrl({})).toEqual(valid); | ||
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
test("it skips prompting application url if told to fill in later", async () => { | ||
vi.mocked(consola).prompt.mockResolvedValueOnce("no"); | ||
expect(await promptApplicationUrl({})).toEqual(undefined); | ||
expect(vi.mocked(consola).prompt).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("it skips prompting completely if told to", async () => { | ||
expect(await promptApplicationUrl({ skipApplicationUrl: true })).toEqual( | ||
undefined, | ||
); | ||
expect(vi.mocked(consola).prompt).not.toHaveBeenCalled(); | ||
}); |
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,63 @@ | ||
/* | ||
* Copyright 2023 Palantir Technologies, Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { consola } from "../consola.js"; | ||
import { italic } from "../highlight.js"; | ||
|
||
export async function promptApplicationUrl( | ||
{ skipApplicationUrl, applicationUrl }: { | ||
skipApplicationUrl?: boolean; | ||
applicationUrl?: string; | ||
}, | ||
): Promise<string | undefined> { | ||
if (skipApplicationUrl) { | ||
return undefined; | ||
} | ||
|
||
if (applicationUrl == null) { | ||
const skip = (await consola.prompt( | ||
`Do you know the URL your production application will be hosted on? This is required to create a production build of your application with the correct OAuth redirect URL.`, | ||
{ | ||
type: "select", | ||
options: [ | ||
{ label: "Yes, prefill it for me", value: "yes" }, | ||
{ label: "No, I will fill it in myself later", value: "no" }, | ||
], | ||
}, | ||
// Types for "select" are wrong the value is returned rather than the option object | ||
// https://github.com/unjs/consola/pull/238 | ||
)) as unknown as "yes" | "no"; | ||
|
||
if (skip === "no") { | ||
return undefined; | ||
} | ||
} | ||
|
||
while (applicationUrl == null || !/^https?:\/\//.test(applicationUrl)) { | ||
if (applicationUrl != null) { | ||
consola.fail("Please enter a valid application URL"); | ||
} | ||
applicationUrl = await consola.prompt( | ||
`Enter the URL your production application will be hosted on:\n${ | ||
italic( | ||
"(Example https://myapp.example.palantirfoundry.com/)", | ||
) | ||
}`, | ||
{ type: "text" }, | ||
); | ||
} | ||
return applicationUrl.replace(/\/$/, ""); | ||
} |
Oops, something went wrong.