Skip to content

Commit a8d47bc

Browse files
test: Utility function test cases added
1 parent 681366a commit a8d47bc

File tree

2 files changed

+112
-3
lines changed

2 files changed

+112
-3
lines changed

test/unit/commands/app/get.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe("app:get", () => {
103103
.it("should return manifest for specific uid passed");
104104
});
105105

106-
describe("Ask confirmation if `manifest.json` exists", () => {
106+
describe("Ask confirmation if `manifest.json` exists and go with `Yes` option", () => {
107107
test
108108
.stdout({ print: process.env.PRINT === "true" || false })
109109
.stub(ux.action, "stop", () => {})
@@ -155,6 +155,58 @@ describe("app:get", () => {
155155
.it("Should create config file with the +1 mechanism");
156156
});
157157

158+
describe("Ask confirmation if `manifest.json` exists and go with `No` option", () => {
159+
test
160+
.stdout({ print: process.env.PRINT === "true" || false })
161+
.stub(ux.action, "stop", () => {})
162+
.stub(ux.action, "start", () => {})
163+
.stub(fs, "readdirSync", () => ["manifest.json"])
164+
.stub(fs, "writeFileSync", () => new PassThrough())
165+
.stub(cliux, "inquire", async (...args: any) => {
166+
const [prompt]: any = args;
167+
const cases = {
168+
App: "App 1",
169+
Organization: "test org 1",
170+
};
171+
172+
return (cases as Record<string, any>)[prompt.name];
173+
})
174+
.stub(cliux, "confirm", async () => false)
175+
.nock(region.cma, (api) =>
176+
api
177+
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
178+
.reply(200, { organizations: mock.organizations })
179+
)
180+
.nock(`https://${developerHubBaseUrl}`, (api) =>
181+
api
182+
.get(
183+
"/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack"
184+
)
185+
.reply(200, {
186+
data: mock.apps,
187+
})
188+
)
189+
.command([
190+
"app:get",
191+
"--data-dir",
192+
join(process.cwd(), "test", "unit", "config"),
193+
])
194+
.do(({ stdout }) =>
195+
expect(stdout).to.contain(
196+
$t(messages.FILE_WRITTEN_SUCCESS, {
197+
file: join(
198+
process.cwd(),
199+
"test",
200+
"unit",
201+
"config",
202+
`${config.defaultAppFileName}.json`
203+
),
204+
})
205+
)
206+
)
207+
.it("Should overwrite config file with");
208+
});
209+
158210
describe("Pass wrong org uid through flag", () => {
159211
test
160212
.stdout({ print: process.env.PRINT === "true" || false })

test/unit/util/common-utils.test.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import { fancy } from "fancy-test";
22
import { expect } from "@oclif/test";
33
import {
4+
cliux,
45
configHandler,
56
ContentstackClient,
67
managementSDKClient,
78
} from "@contentstack/cli-utilities";
89

9-
import { getOrganizations } from "../../../src/util/common-utils";
10-
import * as mock from "../mock/common.mock.json";
10+
import config from "../../../src/config";
1111
import { LogFn } from "../../../src/types";
12+
import * as mock from "../mock/common.mock.json";
13+
import { fetchApps, getOrganizations } from "../../../src/util/common-utils";
1214

1315
const region: { cma: string; name: string; cda: string } =
1416
configHandler.get("region");
17+
const developerHubBaseUrl = (config.developerHubUrls as Record<string, any>)[
18+
region.cma
19+
];
1520

1621
describe("common utils", () => {
1722
const log: LogFn = () => {};
1823
let managementSdk: ContentstackClient;
24+
let managementAppSdk: ContentstackClient;
1925

2026
before(async () => {
2127
managementSdk = await managementSDKClient({
2228
host: region.cma.replace("https://", ""),
2329
});
30+
managementAppSdk = await managementSDKClient({
31+
host: developerHubBaseUrl,
32+
});
2433
});
2534

2635
describe("getOrganizations", () => {
@@ -81,4 +90,52 @@ describe("common utils", () => {
8190
.it("API fails with status code 400");
8291
});
8392
});
93+
94+
describe("fetchApps", () => {
95+
describe("Get list of Apps", () => {
96+
fancy
97+
.stub(cliux, "loader", () => {})
98+
.nock(`https://${developerHubBaseUrl}`, (api) =>
99+
api
100+
.get(
101+
"/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack"
102+
)
103+
.reply(200, {
104+
data: mock.apps,
105+
})
106+
)
107+
.it("Returns list of apps", async () => {
108+
const [app] = await fetchApps(
109+
{ "app-type": "stack" as any },
110+
"test-uid-1",
111+
{
112+
log,
113+
managementSdk: managementAppSdk,
114+
}
115+
);
116+
expect(app.uid).to.equal(mock.apps[0].uid);
117+
});
118+
});
119+
120+
describe("Get list of Apps API fail case", () => {
121+
fancy
122+
.stub(cliux, "loader", () => {})
123+
.nock(`https://${developerHubBaseUrl}`, (api) =>
124+
api
125+
.get(
126+
"/manifests?limit=50&asc=name&include_count=true&skip=0&target_type=stack"
127+
)
128+
.reply(400)
129+
)
130+
.do(
131+
async () =>
132+
await fetchApps({ "app-type": "stack" as any }, "test-uid-1", {
133+
log,
134+
managementSdk: managementAppSdk,
135+
})
136+
)
137+
.catch(({ message }) => expect(message).to.contains('"status":400'))
138+
.it("Returns error code with 400");
139+
});
140+
});
84141
});

0 commit comments

Comments
 (0)