-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from huwshimi/handle-no-versions
Be defensive about returned version data
- Loading branch information
Showing
3 changed files
with
79 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,85 @@ | ||
import { cachedAPIResponse, jujuUpdateAvailable } from "../versions"; | ||
import { | ||
cachedAPIResponse, | ||
jujuUpdateAvailable, | ||
dashboardUpdateAvailable, | ||
} from "../versions"; | ||
|
||
describe("versions", () => { | ||
// mock fetch: https://juju.is/latest.json | ||
const mockFetch = jest.fn(); | ||
mockFetch.mockReturnValue({ | ||
json: () => ({ | ||
dashboard: "2.8.1", | ||
juju: ["2.8.1", "2.8.0", "2.7.9"], | ||
}), | ||
}); | ||
global.fetch = mockFetch; | ||
let fetchSpy: jest.SpyInstance; | ||
|
||
beforeAll(() => { | ||
fetchSpy = jest.spyOn(global, "fetch"); | ||
}); | ||
|
||
beforeEach(() => { | ||
// invalidate the cache | ||
cachedAPIResponse?.fetchedAt.setTime(0); | ||
// mock fetch: https://juju.is/latest.json | ||
fetchSpy.mockImplementation( | ||
jest.fn(() => | ||
Promise.resolve({ | ||
json: () => ({ | ||
dashboard: "2.8.1", | ||
juju: ["2.8.1", "2.8.0", "2.7.9"], | ||
}), | ||
}) | ||
) as jest.Mock | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("should return true if the Juju controller version is old", async () => { | ||
expect(await jujuUpdateAvailable("2.7.8")).toBe(true); | ||
}); | ||
|
||
it("should return false if the Juju controller version is new", async () => { | ||
expect(await jujuUpdateAvailable("2.8.1")).toBe(false); | ||
}); | ||
|
||
it("should return true if the Juju dashboard version is old", async () => { | ||
expect(await jujuUpdateAvailable("2.2.0")).toBe(true); | ||
expect(await dashboardUpdateAvailable("2.2.0")).toBe(true); | ||
}); | ||
|
||
it("should return false if the Juju dashboard version is new", async () => { | ||
expect(await jujuUpdateAvailable("2.8.1")).toBe(false); | ||
expect(await dashboardUpdateAvailable("2.8.1")).toBe(false); | ||
}); | ||
|
||
it("handles no juju version response", async () => { | ||
fetchSpy.mockImplementation( | ||
jest.fn(() => | ||
Promise.resolve({ | ||
json: () => ({}), | ||
}) | ||
) as jest.Mock | ||
); | ||
expect(await jujuUpdateAvailable("2.8.1")).toBeNull(); | ||
}); | ||
|
||
it("handles no dashboard version response", async () => { | ||
fetchSpy.mockImplementation( | ||
jest.fn(() => | ||
Promise.resolve({ | ||
json: () => ({}), | ||
}) | ||
) as jest.Mock | ||
); | ||
expect(await dashboardUpdateAvailable("2.2.0")).toBeNull(); | ||
}); | ||
|
||
it("should use TTL to cache the response", async () => { | ||
await jujuUpdateAvailable("2.7.8"); | ||
await jujuUpdateAvailable("2.7.8"); | ||
expect(mockFetch).toHaveBeenCalledTimes(1); | ||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
// invalidate the cache | ||
cachedAPIResponse?.fetchedAt.setTime(0); | ||
await jujuUpdateAvailable("2.7.8"); | ||
expect(mockFetch).toHaveBeenCalledTimes(2); | ||
expect(fetchSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters