UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

80 lines (61 loc) 2.15 kB
const { mockZappRequests, cleanMocks, } = require("../../../test_helpers/mockZappRequests"); const build_params = require("../../../test_helpers/appBuildParams.json"); const VERSION_ID = "e7a17c6f-4113-41ac-9450-6281d22153a1"; const ZAPP_TOKEN_MOCK = "fake_zapp_token"; const FAILING_VERSION_ID = "09876POI"; // silences the console on warnings from tests const consoleSpy = jest.spyOn(console, "warn").mockImplementation(() => {}); describe("when ZAPP_TOKEN is not defined", () => { const ENV = process.env; beforeEach(() => { jest.resetModules(); process.env = { ...ENV }; delete process.env.ZAPP_TOKEN; }); afterEach(() => { process.env = ENV; }); it("throws an error", () => { const { getAppVersionParams } = require("../"); const ERROR_MESSAGE = "no $ZAPP_TOKEN defined - please add it to your environment variables"; expect(getAppVersionParams(VERSION_ID)).rejects.toThrow(ERROR_MESSAGE); }); }); describe("getAppVersionsParams", () => { const { getAppVersionParams } = require("../index"); const ENV = process.env; beforeEach(() => { jest.resetModules(); consoleSpy.mockClear(); process.env = { ...ENV }; process.env.ZAPP_TOKEN = ZAPP_TOKEN_MOCK; mockZappRequests(); mockZappRequests(FAILING_VERSION_ID, 404); }); it("returns the app's build params", async () => { const res = await getAppVersionParams(VERSION_ID); expect(res).toEqual(build_params.build_params); }); it("trims the app version id string before sending the request", async () => { // eslint-disable-next-line no-irregular-whitespace const res = await getAppVersionParams(` ${VERSION_ID} `); expect(res).toEqual(build_params.build_params); }); it("throws if response is not 2xx / 3xx", async () => { const { getAppVersionParams } = require("../index"); try { await getAppVersionParams(FAILING_VERSION_ID); } catch (e) { expect(e).toEqual(new Error("Request failed with status code 404")); expect(consoleSpy).toHaveBeenCalledTimes(3); } }); afterEach(() => { process.env = ENV; cleanMocks(); }); });