diff --git a/test/commands/endpoint.test.ts b/test/commands/endpoint.test.ts index 6bde94b6..c2890ac8 100644 --- a/test/commands/endpoint.test.ts +++ b/test/commands/endpoint.test.ts @@ -1,7 +1,9 @@ import { expect, test } from "@oclif/test"; import { ShellConfig, getRootConfigPath } from "../../src/lib/config"; import sinon, { SinonStub } from "sinon"; -import AddEndpointCommand from "../../src/commands/add-endpoint"; +import AddEndpointCommand from "../../src/commands/endpoint/add"; +import ListEndpointCommand from "../../src/commands/endpoint/list"; +import RemoveEndpointCommand from "../../src/commands/endpoint/remove"; import { Config } from "@oclif/core"; const rootConfigPath = getRootConfigPath(); @@ -18,7 +20,7 @@ const stubbedRootConfig = ( return config as any; }; -describe("add-endpoint", () => { +describe("endpoint:add", () => { test .add("config", () => stubbedRootConfig({ @@ -45,7 +47,7 @@ describe("add-endpoint", () => { ) .it("adds an endpoint", (ctx) => { expect(ctx.stdout).to.equal( - `Saved endpoint foobar to ${rootConfigPath}\n` + `Warning: could not connect to Fauna\nSaved endpoint foobar to ${rootConfigPath}\n` ); expect(ctx.config.rootConfig).to.deep.equal({ defaultEndpoint: "my-endpoint", @@ -96,7 +98,7 @@ describe("add-endpoint", () => { ) .it("sets default endpoint", (ctx) => { expect(ctx.stdout).to.equal( - `Saved endpoint foobar to ${rootConfigPath}\n` + `Warning: could not connect to Fauna\nSaved endpoint foobar to ${rootConfigPath}\n` ); expect(ctx.config.rootConfig).to.deep.equal({ defaultEndpoint: "foobar", @@ -120,3 +122,109 @@ describe("add-endpoint", () => { expect(ctx.config.saveRootConfig.calledOnce).to.be.true; }); }); + +describe("endpoint:list", () => { + test + .add("config", () => + stubbedRootConfig({ + default: "my-endpoint", + "my-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + "other-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + }) + ) + .stdout() + .do((ctx) => + new ListEndpointCommand([], new Config({} as any)).execute(ctx.config) + ) + .it("lists endpoints", (ctx) => { + expect(ctx.stdout).to.equal( + `Available endpoints:\n* my-endpoint\n other-endpoint\n` + ); + expect(ctx.config.saveRootConfig.calledOnce).to.be.false; + }); +}); + +describe("endpoint:remove", () => { + test + .add("config", () => + stubbedRootConfig({ + default: "my-endpoint", + "my-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + "other-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + }) + ) + .stdout() + .do((ctx) => + new RemoveEndpointCommand( + ["other-endpoint"], + new Config({} as any) + ).execute(ctx.config) + ) + .it("removes an endpoint", (ctx) => { + expect(ctx.stdout).to.equal(`Removed endpoint other-endpoint.\n`); + expect(ctx.config.rootConfig).to.deep.equal({ + defaultEndpoint: "my-endpoint", + endpoints: { + "my-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + // These graphql bits are only saved if they differ from the + // default. + graphqlHost: "graphql.fauna.com", + graphqlPort: 443, + }, + }, + }); + expect(ctx.config.saveRootConfig.calledOnce).to.be.true; + }); + + test + .add("config", () => + stubbedRootConfig({ + default: "my-endpoint", + "my-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + "other-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + }, + }) + ) + .stdout() + .do((ctx) => + new RemoveEndpointCommand(["my-endpoint"], new Config({} as any)).execute( + ctx.config + ) + ) + .it("clears the default if needed", (ctx) => { + expect(ctx.stdout).to.equal(`Removed endpoint my-endpoint.\n`); + expect(ctx.config.rootConfig).to.deep.equal({ + defaultEndpoint: undefined, + endpoints: { + "other-endpoint": { + url: "http://bar.baz", + secret: "fn3333", + // These graphql bits are only saved if they differ from the + // default. + graphqlHost: "graphql.fauna.com", + graphqlPort: 443, + }, + }, + }); + expect(ctx.config.saveRootConfig.calledOnce).to.be.true; + }); +}); diff --git a/test/commands/endpoints.test.js b/test/commands/endpoints.test.js deleted file mode 100644 index 0d31cd9e..00000000 --- a/test/commands/endpoints.test.js +++ /dev/null @@ -1,47 +0,0 @@ -const { expect, test } = require("@oclif/test"); -const fs = require("fs"); -const ini = require("ini"); -const { getConfigFile } = require("../../src/lib/misc"); - -const configMock = { - default: "local", - cloud: { - domain: "db.fauna.com", - scheme: "https", - secret: "secret", - graphqlHost: "graphql.fauna.com", - }, - local: { - domain: "127.0.0.1", - scheme: "http", - secret: "secret", - graphqlHost: "127.0.0.1", - }, -}; - -describe("endpoints", () => { - const originalReadFile = fs.readFile; - - test - .stub(fs, "readFile", (file, enc, cb) => { - if (file !== getConfigFile()) return originalReadFile(file, enc, cb); - cb(null, ini.encode(configMock)); - }) - .stdout() - .command(["list-endpoints"]) - .it("runs list-endpoints", (ctx) => { - expect(ctx.stdout).to.contain("cloud\nlocal *\n"); - }); - - test - .stub(fs, "readFile", (file, enc, cb) => { - if (file !== getConfigFile()) return originalReadFile(file, enc, cb); - cb(null, ini.encode({})); - }) - .stdout() - .command(["list-endpoints"]) - .catch((err) => { - expect(err.message).to.contain("No endpoints defined"); - }) - .it("runs list-endpoints when no endpoints defined"); -});