Created
June 7, 2024 10:49
-
-
Save EduardoAC/a56f190b99302fde8b6f7e1650b499ab to your computer and use it in GitHub Desktop.
Chrome extension - testing get rate from cache using jest-chrome for Chrome API mocking
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
/* eslint-disable @typescript-eslint/no-explicit-any */ | |
import { JestChrome } from "jest-chrome/types/jest-chrome" | |
import { getRateFromCache } from "./getRateFromCache" | |
describe("getRateFromCache", () => { | |
const sendResponseMock = jest.fn() | |
const jestChrome = chrome as any as JestChrome | |
beforeEach(() => { | |
jest.clearAllMocks() | |
}) | |
it("should retrieve the rating from cache and return status 200 with data", async () => { | |
const data = { url: "http://example.com" } | |
jestChrome.storage.local.get.mockImplementation(() => | |
Promise.resolve({ "http://example.com": 5 }), | |
) | |
await getRateFromCache(sendResponseMock, data) | |
expect(chrome.storage.local.get).toHaveBeenCalledWith([ | |
"http://example.com", | |
]) | |
expect(sendResponseMock).toHaveBeenCalledWith({ | |
statusCode: 200, | |
data: 5, | |
}) | |
}) | |
it("should handle error during retrieval and return status 500", async () => { | |
// ... Full code on https://github.com/EduardoAC/site-review-extension/blob/master/src/serviceWorker/events/onMessageHandlers/getRateFromCache.test.ts | |
}) | |
it("should return status 500 if URL is missing", async () => { | |
// ...Full code on https://github.com/EduardoAC/site-review-extension/blob/master/src/serviceWorker/events/onMessageHandlers/getRateFromCache.test.ts | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code for getRateFromCache can be found at https://github.com/EduardoAC/site-review-extension/blob/master/src/serviceWorker/events/onMessageHandlers/getRateFromCache.ts
The original test file can be found at https://github.com/EduardoAC/site-review-extension/blob/master/src/serviceWorker/events/onMessageHandlers/getRateFromCache.test.ts