|
1 | 1 | import { getLatestVersion } from "./helper"; |
2 | | -import * as helper from "./helper"; |
3 | 2 | import { ReleaseChannel } from "./constants"; |
4 | 3 |
|
5 | 4 | describe("getLatestVersion", () => { |
6 | | - afterEach(() => { |
| 5 | + beforeEach(() => { |
7 | 6 | jest.restoreAllMocks(); |
8 | 7 | }); |
9 | 8 |
|
10 | | - it("returns latest stable version", async () => { |
11 | | - jest.spyOn(helper, "loadHtml").mockResolvedValue(` |
12 | | - <html><body> |
13 | | - <article><h3> 2.31.1 </h3><span>build number and release date</span></article> |
14 | | - <article><h3> 2.32.1 </h3><span>build number and release date</span></article> |
15 | | - <article><h3> 2.33.2 </h3><span>build number and release date</span></article> |
16 | | - <article><h3>2.33.1 </h3><span>build number and release date</span></article> |
17 | | - </body></html> |
18 | | - `); |
| 9 | + it("should return latest stable version", async () => { |
| 10 | + const mockResponse = { |
| 11 | + CLI2: { |
| 12 | + release: { version: "2.31.0" }, |
| 13 | + beta: { version: "2.32.0-beta.01" }, |
| 14 | + }, |
| 15 | + }; |
| 16 | + |
| 17 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 18 | + json: async () => mockResponse, |
| 19 | + } as Response); |
| 20 | + |
19 | 21 | const version = await getLatestVersion(ReleaseChannel.Stable); |
20 | | - expect(version).toBe("2.33.2"); |
| 22 | + expect(version).toBe("2.31.0"); |
21 | 23 | }); |
22 | 24 |
|
23 | | - it("returns latest beta version", async () => { |
24 | | - jest.spyOn(helper, "loadHtml").mockResolvedValue(` |
25 | | - <html><body> |
26 | | - <article class="beta"><h3> 1.32.0-beta.01 <span>build number and release date</span></h3></article> |
27 | | - <article class="beta"><h3>2.32.0-beta.01 <span>build number and release date</span></h3></article> |
28 | | - <article class="beta"><h3>3.32.0-beta.02<span>build number and release date</span></h3></article> |
29 | | - <article class="beta"><h3>3.32.0-beta.01 <span>build number and release date</span></h3></article> |
30 | | - </body></html> |
31 | | - `); |
| 25 | + it("should return latest beta version", async () => { |
| 26 | + const mockResponse = { |
| 27 | + CLI2: { |
| 28 | + release: { version: "2.31.0" }, |
| 29 | + beta: { version: "2.32.0-beta.01" }, |
| 30 | + }, |
| 31 | + }; |
| 32 | + |
| 33 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 34 | + json: async () => mockResponse, |
| 35 | + } as Response); |
| 36 | + |
32 | 37 | const version = await getLatestVersion(ReleaseChannel.Beta); |
33 | | - expect(version).toBe("3.32.0-beta.02"); |
| 38 | + expect(version).toBe("2.32.0-beta.01"); |
34 | 39 | }); |
35 | 40 |
|
36 | | - it("throws error when no versions found", async () => { |
37 | | - jest.spyOn(helper, "loadHtml").mockResolvedValue("<html></html>"); |
38 | | - await expect(getLatestVersion(ReleaseChannel.Stable)).rejects.toThrow( |
39 | | - `No ${ReleaseChannel.Stable} versions found`, |
40 | | - ); |
41 | | - }); |
| 41 | + it("should throw if no CLI2 field", async () => { |
| 42 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 43 | + json: async () => ({}), |
| 44 | + } as Response); |
42 | 45 |
|
43 | | - it("throws error when HTML is invalid", async () => { |
44 | | - jest |
45 | | - .spyOn(helper, "loadHtml") |
46 | | - .mockResolvedValue("<html><article></article></html>"); |
47 | | - await expect(getLatestVersion(ReleaseChannel.Stable)).rejects.toThrow( |
48 | | - `No ${ReleaseChannel.Stable} versions found`, |
49 | | - ); |
| 46 | + await expect( |
| 47 | + getLatestVersion(ReleaseChannel.Stable), |
| 48 | + ).rejects.toThrow(`No ${ReleaseChannel.Stable} versions found`); |
50 | 49 | }); |
51 | 50 |
|
52 | | - it("calls loadHtml once", async () => { |
53 | | - const spy = jest.spyOn(helper, "loadHtml").mockResolvedValue(` |
54 | | - <article><h3>2.31.1</h3></article> |
55 | | - `); |
56 | | - await getLatestVersion(ReleaseChannel.Stable); |
57 | | - expect(spy).toHaveBeenCalledTimes(1); |
| 51 | + it("should throw if no stable version found", async () => { |
| 52 | + const mockResponse = { |
| 53 | + CLI2: { |
| 54 | + beta: { version: "2.32.0-beta.01" }, |
| 55 | + }, |
| 56 | + }; |
| 57 | + |
| 58 | + jest.spyOn(global, "fetch").mockResolvedValueOnce({ |
| 59 | + json: async () => mockResponse, |
| 60 | + } as Response); |
| 61 | + |
| 62 | + await expect( |
| 63 | + getLatestVersion(ReleaseChannel.Stable), |
| 64 | + ).rejects.toThrow(`No ${ReleaseChannel.Stable} versions found`); |
58 | 65 | }); |
59 | 66 | }); |
0 commit comments