|
| 1 | +import { fancy } from "fancy-test"; |
| 2 | +import { expect } from "@oclif/test"; |
| 3 | +import { |
| 4 | + cliux, |
| 5 | + FlagInput, |
| 6 | + configHandler, |
| 7 | + ContentstackClient, |
| 8 | + managementSDKClient, |
| 9 | +} from "@contentstack/cli-utilities"; |
| 10 | + |
| 11 | +import { LogFn } from "../../../src/types"; |
| 12 | +import * as mock from "../mock/common.mock.json"; |
| 13 | +import messages, { $t } from "../../../src/messages"; |
| 14 | +import { |
| 15 | + getOrg, |
| 16 | + getAppName, |
| 17 | + getDirName, |
| 18 | + getDeveloperHubUrl, |
| 19 | +} from "../../../src/util"; |
| 20 | +import * as commonUtils from "../../../src/util/common-utils"; |
| 21 | +import { join } from "path"; |
| 22 | + |
| 23 | +const region: { cma: string; name: string; cda: string } = |
| 24 | + configHandler.get("region"); |
| 25 | + |
| 26 | +describe("inquirer util", () => { |
| 27 | + const log: LogFn = () => {}; |
| 28 | + let managementSdk: ContentstackClient; |
| 29 | + |
| 30 | + before(async () => { |
| 31 | + managementSdk = await managementSDKClient({ |
| 32 | + host: region.cma.replace("https://", ""), |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("getAppName", () => { |
| 37 | + describe("show prompt to get name from user", () => { |
| 38 | + fancy |
| 39 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 40 | + .stub(cliux, "inquire", async () => "Test name") |
| 41 | + .it("Returns name string", async () => { |
| 42 | + const name = await getAppName(); |
| 43 | + expect(name).to.equal("Test name"); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("Check user input length validation", () => { |
| 48 | + fancy |
| 49 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 50 | + .stdin("\n") |
| 51 | + .do(async () => { |
| 52 | + setTimeout(() => { |
| 53 | + process.stdin.emit("data", "Test 1\n"); |
| 54 | + }, 1); |
| 55 | + await getAppName("t1"); |
| 56 | + }) |
| 57 | + .it("Returns validation message", ({ stdout }) => { |
| 58 | + expect(stdout).to.contains( |
| 59 | + $t(messages.INVALID_NAME, { min: "3", max: "20" }) |
| 60 | + ); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("getDirName", () => { |
| 66 | + describe("Show prompt to get name from user", () => { |
| 67 | + fancy |
| 68 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 69 | + .stub(cliux, "inquire", async () => "test") |
| 70 | + .it("returns path", async () => { |
| 71 | + const path = await getDirName(join(process.cwd(), "test")); |
| 72 | + expect(path).to.equal(join(process.cwd(), "test")); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("Check user input directory length validation", () => { |
| 77 | + fancy |
| 78 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 79 | + .stdin("\n") |
| 80 | + .do(async () => { |
| 81 | + setTimeout(() => { |
| 82 | + process.stdin.emit("data", "test-1\n"); |
| 83 | + }, 1); |
| 84 | + await getDirName(join(process.cwd(), "t1")); |
| 85 | + }) |
| 86 | + .it("returns validation message", ({ stdout }) => { |
| 87 | + expect(stdout).to.contains( |
| 88 | + $t(messages.INVALID_NAME, { min: "3", max: "50" }) |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe("Validate if provided directory exist", () => { |
| 94 | + fancy |
| 95 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 96 | + .stdin("test\n") |
| 97 | + .do(async () => { |
| 98 | + setTimeout(() => { |
| 99 | + process.stdin.emit("data", "test-1\n"); |
| 100 | + }, 1); |
| 101 | + await getDirName(join(process.cwd(), "test")); |
| 102 | + }) |
| 103 | + .it("returns validation message", ({ stdout }) => { |
| 104 | + expect(stdout).to.contains(messages.DIR_EXIST); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("getOrg", () => { |
| 110 | + describe("Select an organization from list", () => { |
| 111 | + fancy |
| 112 | + .stub(commonUtils, "getOrganizations", async () => mock.organizations) |
| 113 | + .stub(cliux, "inquire", async () => "test org 1") |
| 114 | + .it("Returns a organization uid", async () => { |
| 115 | + const org = await getOrg({} as FlagInput, { |
| 116 | + log, |
| 117 | + managementSdk, |
| 118 | + }); |
| 119 | + expect(org).to.equal(mock.organizations[0].uid); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("Passing wrong organization uid through flag", () => { |
| 124 | + fancy |
| 125 | + .stub(commonUtils, "getOrganizations", async () => mock.organizations) |
| 126 | + .do( |
| 127 | + async () => |
| 128 | + await getOrg({ org: "test org 3" as any } as FlagInput, { |
| 129 | + log, |
| 130 | + managementSdk, |
| 131 | + }) |
| 132 | + ) |
| 133 | + .catch((ctx) => |
| 134 | + expect(ctx.message).to.contain(messages.ORG_UID_NOT_FOUND) |
| 135 | + ) |
| 136 | + .it("fails with error `org uid not found`"); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe("getDeveloperHubUrl", () => { |
| 141 | + describe("Get developer hub base url", () => { |
| 142 | + fancy |
| 143 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 144 | + .stub(configHandler, "get", async () => ({ |
| 145 | + cma: "", |
| 146 | + name: "Test", |
| 147 | + })) |
| 148 | + .stub(cliux, "inquire", async () => "https://dummy.marketplace.com") |
| 149 | + .it("Returns developer hub base url", async () => { |
| 150 | + const url = await getDeveloperHubUrl(); |
| 151 | + expect(url).to.equal("dummy.marketplace.com"); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe("Validate marketplace url if empty.?", () => { |
| 156 | + fancy |
| 157 | + .stdout({ print: process.env.PRINT === "true" || false }) |
| 158 | + .stub(configHandler, "get", async () => ({ |
| 159 | + cma: "", |
| 160 | + name: "Test", |
| 161 | + })) |
| 162 | + .stdin("\n") |
| 163 | + .do(async () => { |
| 164 | + setTimeout(() => { |
| 165 | + process.stdin.emit("data", "dummy.marketplace.com\n"); |
| 166 | + }, 1); |
| 167 | + await getDeveloperHubUrl(); |
| 168 | + }) |
| 169 | + .it( |
| 170 | + "Prints URL validation message and asks for new input", |
| 171 | + ({ stdout }) => { |
| 172 | + expect(stdout).to.contains(messages.BASE_URL_EMPTY); |
| 173 | + } |
| 174 | + ); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments