|
| 1 | +import { describe, vi } from "vitest"; |
| 2 | +import { |
| 3 | + checkIsCurrentHostIsLocalhost, |
| 4 | + ensureThatURLStartsWithHTTP, |
| 5 | +} from "../../js/utils/url"; |
| 6 | + |
| 7 | +// Mocking of the window location |
| 8 | +const setWindowHost = (host: string) => { |
| 9 | + Object.defineProperty(window, "location", { |
| 10 | + value: { host }, |
| 11 | + writable: true, |
| 12 | + }); |
| 13 | +}; |
| 14 | + |
| 15 | +describe("Tests for the ensureThatURLStartsWithHTTP function", () => { |
| 16 | + afterEach(() => { |
| 17 | + vi.restoreAllMocks(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("Works correctly with localhost", () => { |
| 21 | + beforeEach(() => { |
| 22 | + setWindowHost("localhost:8085"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("Should add http if it is missing", () => { |
| 26 | + const result = ensureThatURLStartsWithHTTP("//some_domain.com"); |
| 27 | + expect(result).toBe("http://some_domain.com"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("Should not change if url already has http", () => { |
| 31 | + const result = ensureThatURLStartsWithHTTP("http://some_domain.com"); |
| 32 | + expect(result).toBe("http://some_domain.com"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("Should not change if url already has https", () => { |
| 36 | + const result = ensureThatURLStartsWithHTTP("https://some_domain.com"); |
| 37 | + expect(result).toBe("https://some_domain.com"); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("Works correctly with not localhost", () => { |
| 42 | + beforeEach(() => { |
| 43 | + setWindowHost("some_domain.com"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("Should add https if missing", () => { |
| 47 | + const result = ensureThatURLStartsWithHTTP("//some_domain.com"); |
| 48 | + expect(result).toBe("https://some_domain.com"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("Should not change if url already has http", () => { |
| 52 | + const result = ensureThatURLStartsWithHTTP("http://some_domain.com"); |
| 53 | + expect(result).toBe("http://some_domain.com"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("Should not change if url already has https", () => { |
| 57 | + const result = ensureThatURLStartsWithHTTP("https://some_domain.com"); |
| 58 | + expect(result).toBe("https://some_domain.com"); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("Tests for the checkIsCurrentHostIsLocalhost function", () => { |
| 64 | + afterEach(() => { |
| 65 | + vi.restoreAllMocks(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("Return boolean true if we using localhost", () => { |
| 69 | + setWindowHost("localhost:8085"); |
| 70 | + expect(checkIsCurrentHostIsLocalhost()).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it("Return boolean false if we don't using localhost", () => { |
| 74 | + setWindowHost("some_domain.com"); |
| 75 | + expect(checkIsCurrentHostIsLocalhost()).toBe(false); |
| 76 | + }); |
| 77 | +}); |
0 commit comments