-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update inquirer test and removed unwanted manifest files
- Loading branch information
1 parent
2ebdf09
commit efbd52c
Showing
6 changed files
with
136 additions
and
133 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,166 +1,173 @@ | ||
import { expect } from 'chai'; | ||
import fancy from 'fancy-test'; | ||
import sinon from 'sinon'; | ||
import { expect } from "chai"; | ||
import nock from "nock"; | ||
import { join } from "path"; | ||
import sinon from "sinon"; | ||
import { runCommand } from "@oclif/test"; | ||
import messages, { $t } from "../../../src/messages"; | ||
import * as mock from "../mock/common.mock.json"; | ||
import fs from "fs"; | ||
import { | ||
getOrg, | ||
getAppName, | ||
getDirName, | ||
getDeveloperHubUrl, | ||
} from "../../../src/util"; | ||
import { | ||
cliux, | ||
FlagInput, | ||
configHandler, | ||
ContentstackClient, | ||
managementSDKClient, | ||
} from '@contentstack/cli-utilities'; | ||
import * as mock from '../mock/common.mock.json'; | ||
import messages, { $t } from '../../../src/messages'; | ||
import { | ||
getOrg, | ||
getAppName, | ||
getDirName, | ||
getDeveloperHubUrl, | ||
} from '../../../src/util'; | ||
import * as commonUtils from '../../../src/util/common-utils'; | ||
import { join } from 'path'; | ||
} from "@contentstack/cli-utilities"; | ||
|
||
const region: { cma: string; name: string; cda: string } = configHandler.get('region'); | ||
const region = configHandler.get("region"); | ||
|
||
describe('Utility Functions', () => { | ||
describe("Utility Functions", () => { | ||
let sandbox: sinon.SinonSandbox; | ||
let managementSdk: ContentstackClient; | ||
|
||
before(async () => { | ||
beforeEach(async () => { | ||
sandbox = sinon.createSandbox(); | ||
managementSdk = await managementSDKClient({ | ||
host: region.cma.replace('https://', ''), | ||
host: region.cma.replace("https://", ""), | ||
}); | ||
}); | ||
|
||
describe('getAppName', () => { | ||
describe('show prompt to get name from user', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(cliux, 'inquire', sinon.stub().resolves('Test name')) | ||
.it('should return the name provided by the user', async () => { | ||
const name = await getAppName(); | ||
expect(name).to.equal('Test name'); | ||
}); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe("getAppName", () => { | ||
describe("show prompt to get name from user", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").resolves("Test name"); | ||
}); | ||
it("should return the name provided by the user", async () => { | ||
const name = await getAppName(); | ||
expect(name).to.equal("Test name"); | ||
}); | ||
}); | ||
|
||
describe('Check user input length validation', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stdin('\n') | ||
.do(async () => { | ||
setTimeout(() => { | ||
process.stdin.emit('data', 'Te\n'); | ||
}, 1); | ||
await getAppName('t1'); | ||
}) | ||
.it('should return validation message for short input', ({ stdout }) => { | ||
expect(stdout).to.contain($t(messages.INVALID_NAME, { min: '3', max: '20' })); | ||
describe("Check user input length validation", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").callsFake(async (getApp: any) => { | ||
return getApp.validate("t1"); | ||
}); | ||
}); | ||
it("should return validation message for short input", async () => { | ||
const stdout = await getAppName(); | ||
expect(stdout).to.contain( | ||
$t(messages.INVALID_NAME, { min: "3", max: "20" }) | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getDirName', () => { | ||
describe('Show prompt to get directory name from user', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(cliux, 'inquire', sinon.stub().resolves('test')) | ||
.it('should return the directory name provided by the user', async () => { | ||
const path = await getDirName(join(process.cwd(), 'test')); | ||
expect(path).to.equal(join(process.cwd(), 'test')); | ||
}); | ||
describe("getDirName", () => { | ||
describe("Show prompt to get directory name from user", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").resolves("test"); | ||
}); | ||
|
||
it("should return the directory name provided by the user", async () => { | ||
const path = await getDirName(join(process.cwd(), "test")); | ||
expect(path).to.equal(join(process.cwd(), "test")); | ||
}); | ||
}); | ||
|
||
describe('Check directory length validation', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stdin('\n') | ||
.do(async () => { | ||
setTimeout(() => { | ||
process.stdin.emit('data', 't\n'); | ||
}, 1); | ||
await getDirName(join(process.cwd(), 't')); | ||
}) | ||
.it('should return validation message for short input', ({ stdout }) => { | ||
expect(stdout).to.contain($t(messages.INVALID_NAME, { min: '3', max: '50' })); | ||
describe("Check directory length validation", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").callsFake(async (prompt: any) => { | ||
const validation = prompt.validate("t"); | ||
return validation; | ||
}); | ||
}); | ||
|
||
it("should return validation message for short input", async () => { | ||
try { | ||
await getDirName(join(process.cwd(), "t")); | ||
} catch (err: any) { | ||
expect(err.message).to.contain( | ||
$t(messages.INVALID_NAME, { min: "3", max: "30" }) | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
describe('Validate if provided directory exists', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stdin('test\n') | ||
.do(async () => { | ||
setTimeout(() => { | ||
process.stdin.emit('data', 'test\n'); | ||
}, 1); | ||
await getDirName(join(process.cwd(), 'test')); | ||
}) | ||
.it('should return validation message if directory already exists', ({ stdout }) => { | ||
expect(stdout).to.contain(messages.DIR_EXIST); | ||
}); | ||
describe("Validate if provided directory exists", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").resolves("test"); | ||
sandbox.stub(fs, "existsSync").returns(true); | ||
}); | ||
|
||
it("should return validation message if directory already exists", async () => { | ||
try { | ||
await getDirName(join(process.cwd(), "test")); | ||
} catch (err: any) { | ||
expect(err.message).to.contain(messages.DIR_EXIST); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getOrg', () => { | ||
describe('Select an organization from list', () => { | ||
fancy | ||
.stub(commonUtils, 'getOrganizations', sinon.stub().resolves(mock.organizations)) | ||
.stub(cliux, 'inquire', sinon.stub().resolves('test org 1')) | ||
.it('should return the organization UID', async () => { | ||
const org = await getOrg({} as FlagInput, { log: () => {}, managementSdk }); | ||
expect(org).to.equal(mock.organizations[0].uid); | ||
describe("getOrg", () => { | ||
beforeEach(() => { | ||
sandbox.stub(cliux, "inquire").resolves("test org 1"); | ||
nock(region.cma) | ||
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0") | ||
.reply(200, { organizations: mock.organizations }); | ||
}); | ||
describe("Select an organization from list", () => { | ||
it("should return the organization UID", async () => { | ||
const org = await getOrg({} as FlagInput, { | ||
log: () => {}, | ||
managementSdk, | ||
}); | ||
expect(org).to.equal(mock.organizations[0].uid); | ||
}); | ||
}); | ||
|
||
describe('Passing wrong organization uid through flag', () => { | ||
fancy | ||
.stub(commonUtils, 'getOrganizations', sinon.stub().resolves(mock.organizations)) | ||
.do(async () => { | ||
try { | ||
await getOrg({ org: 'test org 3' } as unknown as FlagInput, { log: () => {}, managementSdk }); | ||
throw new Error('Expected error not thrown'); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
expect(err.message).to.contain(messages.ORG_UID_NOT_FOUND); | ||
} else { | ||
throw new Error('Caught value is not an instance of Error'); | ||
} | ||
} | ||
}) | ||
.it('should fail with error `org uid not found`'); | ||
describe("Passing wrong organization uid through flag", () => { | ||
it("should fail with error `org uid not found`", async () => { | ||
try { | ||
await getOrg({ org: "test org 3" } as unknown as FlagInput, { | ||
log: () => {}, | ||
managementSdk, | ||
}); | ||
} catch (err: any) { | ||
expect(err.message).to.contain(messages.ORG_UID_NOT_FOUND); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getDeveloperHubUrl', () => { | ||
describe('Get developer hub base URL', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configHandler, 'get', sinon.stub().returns({ | ||
cma: 'https://api.example.com', | ||
name: 'Test', | ||
})) | ||
.stub(cliux, 'inquire', sinon.stub().resolves('https://api.example.com')) | ||
.it('should return the developer hub base URL', () => { | ||
const url = getDeveloperHubUrl(); | ||
expect(url).to.equal('developerhub-api.example.com'); | ||
describe("getDeveloperHubUrl", () => { | ||
describe("Get developer hub base URL", () => { | ||
beforeEach(() => { | ||
sandbox.stub(configHandler, "get").returns({ | ||
cma: "https://api.example.com", | ||
name: "Test", | ||
}); | ||
sandbox.stub(cliux, "inquire").callsFake(async () => { | ||
return "https://api.example.com"; | ||
}); | ||
}); | ||
it("should return the developer hub base URL", async () => { | ||
const url = await getDeveloperHubUrl(); | ||
expect(url).to.equal("developerhub-api.example.com"); | ||
}); | ||
}); | ||
|
||
describe('Validate marketplace URL if empty', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configHandler, 'get', sinon.stub().returns({ | ||
cma: 'https://dummy.marketplace.com', | ||
name: 'Test', | ||
})) | ||
.stdin('\n') | ||
.do(async () => { | ||
setTimeout(() => { | ||
process.stdin.emit('data', 'dummy.marketplace.com\n'); | ||
}, 1); | ||
getDeveloperHubUrl(); | ||
}) | ||
.it('should print URL validation message and ask for new input', ({ stdout }) => { | ||
expect(stdout).to.contain(''); | ||
}); | ||
describe("Validate marketplace URL if empty", () => { | ||
it("should print URL validation message and ask for new input", async () => { | ||
sandbox.stub(cliux, "inquire").resolves("invalid-url"); | ||
try { | ||
await runCommand("app:create", { | ||
root: process.cwd(), | ||
}); | ||
} catch (err: any) { | ||
expect(err.message).to.contain("Please enter a valid URL"); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |