|
| 1 | +import { restore, fake } from "sinon"; |
| 2 | +import { join } from "path"; |
| 3 | +import proxyquire from "proxyquire"; |
| 4 | +import { expect } from "chai"; |
| 5 | +import { getCDNBaseUrl } from "../../src/utils/ui5"; |
| 6 | + |
| 7 | +describe("ui5", () => { |
| 8 | + afterEach(() => { |
| 9 | + restore(); |
| 10 | + }); |
| 11 | + context("getCDNBaseUrl", () => { |
| 12 | + it("get CDN without local url [with version]", async () => { |
| 13 | + const result = await getCDNBaseUrl("SAPUI5", "1.111.0"); |
| 14 | + expect(result).to.be.equal("https://ui5.sap.com/1.111.0/"); |
| 15 | + }); |
| 16 | + it("get CDN without local url [without version]", async () => { |
| 17 | + const result = await getCDNBaseUrl("SAPUI5", undefined); |
| 18 | + expect(result).to.be.equal("https://ui5.sap.com/"); |
| 19 | + }); |
| 20 | + it("get CDN with local url", async () => { |
| 21 | + const filePath = join(__dirname, "..", "..", "src", "utils", "ui5"); |
| 22 | + const fakeGetLocalUrl = fake.returns("http://localhost:3000/1.111.0/"); |
| 23 | + const fakeTryFetch = fake.resolves({ ok: true }); |
| 24 | + const ui5Module = proxyquire |
| 25 | + .noPreserveCache() |
| 26 | + .noCallThru() |
| 27 | + .load(filePath, { |
| 28 | + "@ui5-language-assistant/logic-utils": { |
| 29 | + getLocalUrl: fakeGetLocalUrl, |
| 30 | + tryFetch: fakeTryFetch, |
| 31 | + }, |
| 32 | + }); |
| 33 | + const result = await ui5Module.getCDNBaseUrl("SAPUI5", "1.111.0"); |
| 34 | + expect(fakeGetLocalUrl).to.have.been.called; |
| 35 | + expect(fakeTryFetch).to.have.been.called; |
| 36 | + expect(result).to.be.equal("http://localhost:3000/1.111.0/"); |
| 37 | + }); |
| 38 | + it("get CDN with local url [fetch not responding => fall back to public]", async () => { |
| 39 | + const filePath = join(__dirname, "..", "..", "src", "utils", "ui5"); |
| 40 | + const fakeGetLocalUrl = fake.returns("http://localhost:3000/1.111.0/"); |
| 41 | + const fakeTryFetch = fake.resolves(undefined); |
| 42 | + const ui5Module = proxyquire |
| 43 | + .noPreserveCache() |
| 44 | + .noCallThru() |
| 45 | + .load(filePath, { |
| 46 | + "@ui5-language-assistant/logic-utils": { |
| 47 | + getLocalUrl: fakeGetLocalUrl, |
| 48 | + tryFetch: fakeTryFetch, |
| 49 | + }, |
| 50 | + }); |
| 51 | + const result = await ui5Module.getCDNBaseUrl("SAPUI5", "1.111.0"); |
| 52 | + expect(fakeGetLocalUrl).to.have.been.called; |
| 53 | + expect(fakeTryFetch).to.have.been.called; |
| 54 | + expect(result).to.be.equal("https://ui5.sap.com/1.111.0/"); |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments