|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { command } from "./firestore-operations-describe"; |
| 4 | +import * as fsi from "../firestore/api"; |
| 5 | +import { logger } from "../logger"; |
| 6 | +import { PrettyPrint } from "../firestore/pretty-print"; |
| 7 | +import { FirebaseError } from "../error"; |
| 8 | + |
| 9 | +describe("firestore:operations:describe", () => { |
| 10 | + const sandbox = sinon.createSandbox(); |
| 11 | + let firestoreApiStub: sinon.SinonStubbedInstance<fsi.FirestoreApi>; |
| 12 | + let loggerInfoStub: sinon.SinonStub; |
| 13 | + let prettyPrintStub: sinon.SinonStub; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + firestoreApiStub = sandbox.createStubInstance(fsi.FirestoreApi); |
| 17 | + sandbox.stub(fsi, "FirestoreApi").returns(firestoreApiStub); |
| 18 | + loggerInfoStub = sandbox.stub(logger, "info"); |
| 19 | + prettyPrintStub = sandbox.stub(PrettyPrint.prototype, "prettyPrintOperation"); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + sandbox.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should call the Firestore API with the correct parameters", async () => { |
| 27 | + const options = { project: "test-project", database: "test-db" }; |
| 28 | + const operationName = "test-operation"; |
| 29 | + firestoreApiStub.describeOperation.resolves({ name: "op1", done: false, metadata: {} }); |
| 30 | + |
| 31 | + await command.runner()(operationName, options); |
| 32 | + |
| 33 | + expect(firestoreApiStub.describeOperation).to.be.calledOnceWith( |
| 34 | + "test-project", |
| 35 | + "test-db", |
| 36 | + operationName, |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should use default values for database if not provided", async () => { |
| 41 | + const options = { project: "test-project" }; |
| 42 | + const operationName = "test-operation"; |
| 43 | + firestoreApiStub.describeOperation.resolves({ name: "op1", done: false, metadata: {} }); |
| 44 | + |
| 45 | + await command.runner()(operationName, options); |
| 46 | + |
| 47 | + expect(firestoreApiStub.describeOperation).to.be.calledOnceWith( |
| 48 | + "test-project", |
| 49 | + "(default)", |
| 50 | + operationName, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should print the operation in JSON format when --json is specified", async () => { |
| 55 | + const options = { project: "test-project", json: true }; |
| 56 | + const operationName = "test-operation"; |
| 57 | + const operation = { name: "op1", done: false, metadata: {} }; |
| 58 | + firestoreApiStub.describeOperation.resolves(operation); |
| 59 | + |
| 60 | + await command.runner()(operationName, options); |
| 61 | + |
| 62 | + expect(loggerInfoStub).to.be.calledOnceWith(JSON.stringify(operation, undefined, 2)); |
| 63 | + expect(prettyPrintStub).to.not.be.called; |
| 64 | + }); |
| 65 | + |
| 66 | + it("should pretty-print the operation when --json is not specified", async () => { |
| 67 | + const options = { project: "test-project" }; |
| 68 | + const operationName = "test-operation"; |
| 69 | + const operation = { name: "op1", done: false, metadata: {} }; |
| 70 | + firestoreApiStub.describeOperation.resolves(operation); |
| 71 | + |
| 72 | + await command.runner()(operationName, options); |
| 73 | + |
| 74 | + expect(prettyPrintStub).to.be.calledOnceWith(operation); |
| 75 | + expect(loggerInfoStub).to.not.be.called; |
| 76 | + }); |
| 77 | + |
| 78 | + it("should throw a FirebaseError if project is not defined", async () => { |
| 79 | + const options = {}; |
| 80 | + const operationName = "test-operation"; |
| 81 | + await expect(command.runner()(operationName, options)).to.be.rejectedWith( |
| 82 | + FirebaseError, |
| 83 | + "Project is not defined. Either use `--project` or use `firebase use` to set your active project.", |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should throw a FirebaseError if operation name is invalid", async () => { |
| 88 | + const options = { project: "test-project" }; |
| 89 | + await expect(command.runner()("", options)).to.be.rejectedWith( |
| 90 | + FirebaseError, |
| 91 | + '"" is not a valid operation name.', |
| 92 | + ); |
| 93 | + await expect(command.runner()("projects/p/databases/d", options)).to.be.rejectedWith( |
| 94 | + FirebaseError, |
| 95 | + '"projects/p/databases/d" is not a valid operation name.', |
| 96 | + ); |
| 97 | + await expect( |
| 98 | + command.runner()("projects/p/databases/d/operations/", options), |
| 99 | + ).to.be.rejectedWith( |
| 100 | + FirebaseError, |
| 101 | + '"projects/p/databases/d/operations/" is not a valid operation name.', |
| 102 | + ); |
| 103 | + }); |
| 104 | +}); |
0 commit comments