UNPKG

@applicaster/zapplicaster-cli

Version:

CLI Tool for the zapp app and Quick Brick project

151 lines (114 loc) 3.79 kB
const { saveRemoteJsonToFile, saveAssets, saveFontFiles } = require("../index"); const nock = require("nock"); const R = require("ramda"); const logger = require("../../logger"); const getFileName = R.compose(R.last, R.split("/")); const loggerSpy = jest.spyOn(logger, "log").mockImplementation(() => ({})); jest.mock("path", () => ({ ...jest.requireActual("path"), resolve: (...args) => args.join("/"), })); jest.mock("../../file", () => ({ writeJsonToFile: jest.fn(), })); const { writeJsonToFile } = require("../../file"); jest.mock("../../shell", () => ({ curlFile: jest.fn(), curlMultipleFiles: jest.fn(), unzip: jest.fn(), })); const shell = require("../../shell"); function clearAllMocks() { loggerSpy.mockClear(); shell.curlFile.mockClear(); shell.curlMultipleFiles.mockClear(); shell.unzip.mockClear(); } const destinationPath = "path/to/app"; const platform = "ios"; describe("saveRemoteJsonToFile", () => { const fileUrl = "http://host.com/file.json"; const fileName = getFileName(fileUrl); const fileUrlWithError = "http://host.com/error.json"; const filePath = `${destinationPath}/config/${platform}/${fileName}`; const fileContent = { foo: { bar: "baz", }, }; beforeEach(() => { nock("http://host.com").get("/file.json").reply(200, fileContent); nock("http://host.com").get("/error.json").reply(500, { error: "error" }); }); afterEach(() => { clearAllMocks(); }); it("downloads the file and writes it to the path", async () => { await saveRemoteJsonToFile({ destinationPath, platform })([ fileName, fileUrl, ]); expect(loggerSpy).toHaveBeenCalledWith( `saving ${fileName} from ${fileUrl}` ); expect(writeJsonToFile).toHaveBeenCalledWith(filePath, fileContent); }); it("can download files from an object", async () => { await saveRemoteJsonToFile({ destinationPath, platform })([ fileName, fileUrl, ]); expect(loggerSpy).toHaveBeenCalledWith( `saving ${fileName} from ${fileUrl}` ); expect(writeJsonToFile).toHaveBeenCalledWith(filePath, fileContent); }); it("throws an error if the download fails", async () => { expect.assertions(1); try { await saveRemoteJsonToFile({ destinationPath, platform })([ fileName, fileUrlWithError, ]); } catch (e) { expect(e).toMatchSnapshot(); } }); }); describe("saveAssets", () => { const assetsUrl = "http://zapp.applicaster.com/assets.zip"; const fileName = getFileName(assetsUrl); const filePath = `${destinationPath}/assets/${platform}`; afterEach(() => { clearAllMocks(); }); it("downloads the files with cURL, and unzips the assets", () => { saveAssets({ destinationPath, platform }, assetsUrl); expect(loggerSpy).toHaveBeenCalledWith(`saving assets from ${assetsUrl}`); expect(shell.curlFile).toHaveBeenCalledWith(assetsUrl, filePath); expect(shell.unzip).toHaveBeenCalledWith( `${filePath}/${fileName}`, filePath ); }); }); describe("saveFontFiles", () => { const fontUrlPrefix = "https://zapp.applicaster.com/app/fonts/ios"; const fontFiles = ["font1.otf", "font2.ttf", "font3.ttf"]; const filePath = `${destinationPath}/fonts/${platform}`; afterEach(() => { clearAllMocks(); }); it("downloads all font files", () => { saveFontFiles({ destinationPath, platform }, fontUrlPrefix, fontFiles); expect(shell.curlMultipleFiles).toHaveBeenCalledWith( fontUrlPrefix, fontFiles, filePath ); }); it("doesn't do anything if the font files list is empty", () => { saveFontFiles({ destinationPath, platform }, fontUrlPrefix, []); expect(shell.curlMultipleFiles).not.toHaveBeenCalled(); }); });