1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-06 09:20:43 +01:00

[PM-6426] Adding jest tests for added BrowserApi methods

This commit is contained in:
Cesar Gonzalez 2024-04-01 15:57:25 -05:00
parent c903e0a0b0
commit 338042b0e4
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 86 additions and 0 deletions

View File

@ -550,4 +550,66 @@ describe("BrowserApi", () => {
expect(callbackMock).toHaveBeenCalled(); expect(callbackMock).toHaveBeenCalled();
}); });
}); });
describe("clearAlarm", () => {
it("clears the alarm with the provided name", async () => {
const alarmName = "alarm-name";
chrome.alarms.clear = jest.fn().mockImplementation((name, callback) => callback(true));
const wasCleared = await BrowserApi.clearAlarm(alarmName);
expect(chrome.alarms.clear).toHaveBeenCalledWith(alarmName, expect.any(Function));
expect(wasCleared).toBe(true);
});
});
describe("clearAllAlarms", () => {
it("clears all alarms", async () => {
chrome.alarms.clearAll = jest.fn().mockImplementation((callback) => callback(true));
const wasCleared = await BrowserApi.clearAllAlarms();
expect(chrome.alarms.clearAll).toHaveBeenCalledWith(expect.any(Function));
expect(wasCleared).toBe(true);
});
});
describe("createAlarm", () => {
it("creates an alarm", async () => {
const alarmName = "alarm-name";
const alarmInfo = { when: 1000 };
chrome.alarms.create = jest
.fn()
.mockImplementation((_name, _createInfo, callback) => callback());
await BrowserApi.createAlarm(alarmName, alarmInfo);
expect(chrome.alarms.create).toHaveBeenCalledWith(alarmName, alarmInfo, expect.any(Function));
});
});
describe("getAlarm", () => {
it("gets the alarm by name", async () => {
const alarmName = "alarm-name";
const alarmMock = mock<chrome.alarms.Alarm>();
chrome.alarms.get = jest.fn().mockImplementation((_name, callback) => callback(alarmMock));
const receivedAlarm = await BrowserApi.getAlarm(alarmName);
expect(chrome.alarms.get).toHaveBeenCalledWith(alarmName, expect.any(Function));
expect(receivedAlarm).toBe(alarmMock);
});
});
describe("getAllAlarms", () => {
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));
const receivedAlarms = await BrowserApi.getAllAlarms();
expect(chrome.alarms.getAll).toHaveBeenCalledWith(expect.any(Function));
expect(receivedAlarms).toBe(alarms);
});
});
}); });

View File

@ -592,22 +592,46 @@ export class BrowserApi {
}); });
} }
/**
* Clears a new alarm with the given name and create info. Returns a promise
* that indicates when the alarm has been cleared successfully.
*
* @param alarmName - The name of the alarm to create.
*/
static clearAlarm(alarmName: string): Promise<boolean> { static clearAlarm(alarmName: string): Promise<boolean> {
return new Promise((resolve) => chrome.alarms.clear(alarmName, resolve)); return new Promise((resolve) => chrome.alarms.clear(alarmName, resolve));
} }
/**
* Clears all alarms that have been set by the extension. Returns a promise
* that indicates when all alarms have been cleared successfully.
*/
static clearAllAlarms(): Promise<boolean> { static clearAllAlarms(): Promise<boolean> {
return new Promise((resolve) => chrome.alarms.clearAll(resolve)); return new Promise((resolve) => chrome.alarms.clearAll(resolve));
} }
/**
* Creates a new alarm with the given name and create info.
*
* @param name - The name of the alarm to create.
* @param createInfo - The creation info for the alarm.
*/
static createAlarm(name: string, createInfo: chrome.alarms.AlarmCreateInfo): Promise<void> { static createAlarm(name: string, createInfo: chrome.alarms.AlarmCreateInfo): Promise<void> {
return new Promise((resolve) => chrome.alarms.create(name, createInfo, resolve)); return new Promise((resolve) => chrome.alarms.create(name, createInfo, resolve));
} }
/**
* Gets the alarm with the given name.
*
* @param alarmName - The name of the alarm to get.
*/
static getAlarm(alarmName: string): Promise<chrome.alarms.Alarm> { static getAlarm(alarmName: string): Promise<chrome.alarms.Alarm> {
return new Promise((resolve) => chrome.alarms.get(alarmName, resolve)); return new Promise((resolve) => chrome.alarms.get(alarmName, resolve));
} }
/**
* Gets all alarms that have been set by the extension.
*/
static getAllAlarms(): Promise<chrome.alarms.Alarm[]> { static getAllAlarms(): Promise<chrome.alarms.Alarm[]> {
return new Promise((resolve) => chrome.alarms.getAll(resolve)); return new Promise((resolve) => chrome.alarms.getAll(resolve));
} }