Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Created June 7, 2024 10:49
Show Gist options
  • Save EduardoAC/a56f190b99302fde8b6f7e1650b499ab to your computer and use it in GitHub Desktop.
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
/* 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