1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-08-27 23:31:41 +02:00

[PM-6426] Adding jest tests to the BrowserApi implementation

This commit is contained in:
Cesar Gonzalez 2024-04-02 16:13:49 -05:00
parent dbbc07b08b
commit f9f60e7c59
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF

View File

@ -552,6 +552,21 @@ describe("BrowserApi", () => {
});
describe("clearAlarm", () => {
it("uses the browser.alarms API if it is available", async () => {
const alarmName = "alarm-name";
globalThis.browser = {
// eslint-disable-next-line
// @ts-ignore
alarms: {
clear: jest.fn(),
},
};
await BrowserApi.clearAlarm(alarmName);
expect(browser.alarms.clear).toHaveBeenCalledWith(alarmName);
});
it("clears the alarm with the provided name", async () => {
const alarmName = "alarm-name";
@ -563,6 +578,20 @@ describe("BrowserApi", () => {
});
describe("clearAllAlarms", () => {
it("uses the browser.alarms API if it is available", async () => {
globalThis.browser = {
// eslint-disable-next-line
// @ts-ignore
alarms: {
clearAll: jest.fn(),
},
};
await BrowserApi.clearAllAlarms();
expect(browser.alarms.clearAll).toHaveBeenCalled();
});
it("clears all alarms", async () => {
const wasCleared = await BrowserApi.clearAllAlarms();
@ -572,6 +601,22 @@ describe("BrowserApi", () => {
});
describe("createAlarm", () => {
it("uses the browser.alarms API if it is available", async () => {
const alarmName = "alarm-name";
const alarmInfo = { when: 1000 };
globalThis.browser = {
// eslint-disable-next-line
// @ts-ignore
alarms: {
create: jest.fn(),
},
};
await BrowserApi.createAlarm(alarmName, alarmInfo);
expect(browser.alarms.create).toHaveBeenCalledWith(alarmName, alarmInfo);
});
it("creates an alarm", async () => {
const alarmName = "alarm-name";
const alarmInfo = { when: 1000 };
@ -583,6 +628,21 @@ describe("BrowserApi", () => {
});
describe("getAlarm", () => {
it("uses the browser.alarms API if it is available", async () => {
const alarmName = "alarm-name";
globalThis.browser = {
// eslint-disable-next-line
// @ts-ignore
alarms: {
get: jest.fn(),
},
};
await BrowserApi.getAlarm(alarmName);
expect(browser.alarms.get).toHaveBeenCalledWith(alarmName);
});
it("gets the alarm by name", async () => {
const alarmName = "alarm-name";
const alarmMock = mock<chrome.alarms.Alarm>();
@ -596,6 +656,20 @@ describe("BrowserApi", () => {
});
describe("getAllAlarms", () => {
it("uses the browser.alarms API if it is available", async () => {
globalThis.browser = {
// eslint-disable-next-line
// @ts-ignore
alarms: {
getAll: jest.fn(),
},
};
await BrowserApi.getAllAlarms();
expect(browser.alarms.getAll).toHaveBeenCalled();
});
it("gets all registered alarms", async () => {
const alarms = [mock<chrome.alarms.Alarm>(), mock<chrome.alarms.Alarm>()];
chrome.alarms.getAll = jest.fn().mockImplementation((callback) => callback(alarms));