|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { RC } from "../rc"; |
| 4 | +import { setNewActive } from "./use"; |
| 5 | +import * as projects from "../management/projects"; |
| 6 | +import * as utils from "../utils"; |
| 7 | +import { FirebaseError } from "../error"; |
| 8 | +import { logger } from "../logger"; |
| 9 | + |
| 10 | +describe("commands/use", () => { |
| 11 | + let rc: RC; |
| 12 | + let getProjectStub: sinon.SinonStub; |
| 13 | + let makeActiveProjectStub: sinon.SinonStub; |
| 14 | + let loggerInfoStub: sinon.SinonStub; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + rc = new RC(); |
| 18 | + getProjectStub = sinon.stub(projects, "getProject"); |
| 19 | + makeActiveProjectStub = sinon.stub(utils, "makeActiveProject"); |
| 20 | + loggerInfoStub = sinon.stub(logger, "info"); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sinon.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe("setNewActive", () => { |
| 28 | + it("should set active project by alias", async () => { |
| 29 | + rc.addProjectAlias("prod", "project-id"); |
| 30 | + getProjectStub.resolves({ projectId: "project-id" }); |
| 31 | + |
| 32 | + await setNewActive("prod", undefined, rc, "root"); |
| 33 | + |
| 34 | + expect(makeActiveProjectStub.calledWith("root", "prod")).to.be.true; |
| 35 | + expect(loggerInfoStub.calledWithMatch(/Now using alias/)).to.be.true; |
| 36 | + }); |
| 37 | + |
| 38 | + it("should set active project by ID", async () => { |
| 39 | + getProjectStub.resolves({ projectId: "project-id" }); |
| 40 | + |
| 41 | + await setNewActive("project-id", undefined, rc, "root"); |
| 42 | + |
| 43 | + expect(makeActiveProjectStub.calledWith("root", "project-id")).to.be.true; |
| 44 | + expect(loggerInfoStub.calledWithMatch(/Now using project/)).to.be.true; |
| 45 | + }); |
| 46 | + |
| 47 | + it("should create alias when --alias is provided", async () => { |
| 48 | + getProjectStub.resolves({ projectId: "project-id" }); |
| 49 | + |
| 50 | + await setNewActive("project-id", "prod", rc, "root"); |
| 51 | + |
| 52 | + expect(rc.resolveAlias("prod")).to.equal("project-id"); |
| 53 | + expect(makeActiveProjectStub.calledWith("root", "project-id")).to.be.true; |
| 54 | + }); |
| 55 | + |
| 56 | + it("should throw error if project does not exist", async () => { |
| 57 | + getProjectStub.rejects(new Error("Not found")); |
| 58 | + |
| 59 | + await expect(setNewActive("non-existent", undefined, rc, "root")).to.be.rejectedWith( |
| 60 | + FirebaseError, |
| 61 | + /Invalid project selection/, |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments