Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement browser.alarms API #17

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"presets": [
"@babel/preset-env"
],
"plugins": [
"@babel/plugin-transform-runtime"
]
}
213 changes: 213 additions & 0 deletions __tests__/alarms.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
function expectIntegerCloseTo(actual, expected, allowedDifference) {
expect(actual).toBeGreaterThanOrEqual(expected - allowedDifference);
expect(actual).toBeLessThanOrEqual(expected + allowedDifference);
}

describe('browser.alarms', () => {
afterEach(() => {
browser.alarms.clearAll();
jest.clearAllMocks();
});

test('create empty', async () => {
browser.alarms.create();

const alarm = await browser.alarms.get();

expect(alarm.name).toBe('');
expect(alarm.periodInMinutes).toBeUndefined();
});

test('create two without name', async () => {
browser.alarms.create({ when: 123 });
browser.alarms.create({ when: 456 });

const alarm = await browser.alarms.get();

expect(alarm.scheduledTime).toBe(456);
expect(await browser.alarms.getAll()).toHaveLength(1);
});

test('create full', async () => {
browser.alarms.create('alarm', {
when: 123,
delayInMinutes: 4,
periodInMinutes: 5,
});

expect(await browser.alarms.get('alarm')).toStrictEqual({
name: 'alarm',
scheduledTime: 123,
periodInMinutes: 5,
});
});

test('create with delay only', async () => {
browser.alarms.create({ delayInMinutes: 4 });

const alarm = await browser.alarms.get();

expectIntegerCloseTo(alarm.scheduledTime, Date.now() + (4 * 60 * 1000), 2000);
expect(alarm.periodInMinutes).toBeUndefined();
});

test('create with period only', async () => {
browser.alarms.create({ periodInMinutes: 4 });

const alarm = await browser.alarms.get();

expectIntegerCloseTo(alarm.scheduledTime, Date.now() + (4 * 60 * 1000), 2000);
expect(alarm.periodInMinutes).toBe(4);
});

test('get', () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });
const callback = jest.fn();

browser.alarms.get('alarm1', callback);

expect(callback).toHaveBeenCalledWith({
name: 'alarm1',
scheduledTime: 123,
});
});

test('get promise', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });

const alarm = await browser.alarms.get('alarm1');

expect(alarm).toStrictEqual({
name: 'alarm1',
scheduledTime: 123,
});
});

test('getAll', () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });
const callback = jest.fn();

browser.alarms.getAll(callback);

expect(callback).toHaveBeenCalledTimes(1);
const alarms = callback.mock.calls[0][0];
expect(alarms).toHaveLength(2);
expect(alarms).toContainEqual({
name: 'alarm1',
scheduledTime: 123,
});
expect(alarms).toContainEqual({
name: 'alarm2',
scheduledTime: 456,
});
});

test('getAll promise', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });

const alarms = await browser.alarms.getAll();

expect(alarms).toHaveLength(2);
expect(alarms).toContainEqual({
name: 'alarm1',
scheduledTime: 123,
});
expect(alarms).toContainEqual({
name: 'alarm2',
scheduledTime: 456,
});
});

test('clear', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });
const callback = jest.fn();

browser.alarms.clear('alarm1', callback);

expect(callback).toHaveBeenCalledWith(true);
expect(await browser.alarms.get('alarm1')).toBeUndefined();
expect(await browser.alarms.get('alarm2')).not.toBeUndefined();
});

test('clear promise', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });

const result = await browser.alarms.clear('alarm1');

expect(result).toBe(true);
expect(await browser.alarms.get('alarm1')).toBeUndefined();
expect(await browser.alarms.get('alarm2')).not.toBeUndefined();
});

test('clear default', async () => {
browser.alarms.create({ when: 123 });
const callback = jest.fn();

browser.alarms.clear(callback);

expect(callback).toHaveBeenCalledWith(true);
expect(await browser.alarms.get()).toBeUndefined();
expect(await browser.alarms.getAll()).toHaveLength(0);
});

test('clear promise default', async () => {
browser.alarms.create({ when: 123 });
const callback = jest.fn();

const result = await browser.alarms.clear();

expect(result).toBe(true);
expect(await browser.alarms.get()).toBeUndefined();
expect(await browser.alarms.getAll()).toHaveLength(0);
});

test('clear non-existing', async () => {
browser.alarms.create('alarm1', { when: 123 });
const callback = jest.fn();

browser.alarms.clear('alarm2', callback);

expect(callback).toHaveBeenCalledWith(false);
expect(await browser.alarms.get('alarm1')).not.toBeUndefined();
});

test('clear promise non-existing', async () => {
browser.alarms.create('alarm1', { when: 123 });

const result = await browser.alarms.clear('alarm2');

expect(result).toBe(false);
expect(await browser.alarms.get('alarm1')).not.toBeUndefined();
});

test('clearAll', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });
const callback = jest.fn();

browser.alarms.clearAll(callback);

expect(callback).toHaveBeenCalledWith(true);
expect(await browser.alarms.get('alarm1')).toBeUndefined();
expect(await browser.alarms.get('alarm2')).toBeUndefined();
expect(await browser.alarms.getAll()).toHaveLength(0);
});

test('clearAll promise', async () => {
browser.alarms.create('alarm1', { when: 123 });
browser.alarms.create('alarm2', { when: 456 });

const result = await browser.alarms.clearAll();

expect(result).toBe(true);
expect(await browser.alarms.get('alarm1')).toBeUndefined();
expect(await browser.alarms.get('alarm2')).toBeUndefined();
expect(await browser.alarms.getAll()).toHaveLength(0);
});
});
116 changes: 96 additions & 20 deletions dist/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,101 @@ function _defineProperty(obj, key, value) {
return obj;
}

var createEventListeners = function createEventListeners() {
var listeners = [];
return {
addListener: jest.fn(function (listener) {
listeners.push(listener);
}),
removeListener: jest.fn(function (listener) {
listeners = listeners.filter(function (l) {
return l !== listener;
});
}),
hasListener: jest.fn(function (listener) {
return listeners.includes(listener);
}),
hasListeners: jest.fn(function () {
return listeners.length > 0;
})
};
};

var createdAlarms = {};
function clearAlarms(alarmNames) {
return (alarmNames !== null && alarmNames !== void 0 ? alarmNames : Object.keys(createdAlarms)).map(function (alarmName) {
var alarmExisted = alarmName in createdAlarms;
delete createdAlarms[alarmName];
return alarmExisted;
}).some(function (alarmExisted) {
return alarmExisted;
});
}
var alarms = {
clear: jest.fn(function (arg1, arg2) {
var _find;
var name = (_find = [arg1, arg2].find(function (it) {
return typeof it === 'string';
})) !== null && _find !== void 0 ? _find : '';
var callback = [arg1, arg2].find(function (it) {
return typeof it === 'function';
});
var result = clearAlarms([name]);
if (typeof callback === 'function') {
callback(result);
} else {
return Promise.resolve(result);
}
}),
clearAll: jest.fn(function (callback) {
var result = clearAlarms();
if (typeof callback === 'function') {
callback(result);
} else {
return Promise.resolve(result);
}
}),
create: jest.fn(function (arg1, arg2) {
var _find2, _find3, _alarmInfo$when, _ref, _alarmInfo$delayInMin;
var name = (_find2 = [arg1, arg2].find(function (it) {
return typeof it === 'string';
})) !== null && _find2 !== void 0 ? _find2 : '';
var alarmInfo = (_find3 = [arg1, arg2].find(function (it) {
return _typeof(it) === 'object';
})) !== null && _find3 !== void 0 ? _find3 : {};
createdAlarms[name] = _objectSpread2({
name: name,
scheduledTime: (_alarmInfo$when = alarmInfo.when) !== null && _alarmInfo$when !== void 0 ? _alarmInfo$when : Date.now() + ((_ref = (_alarmInfo$delayInMin = alarmInfo.delayInMinutes) !== null && _alarmInfo$delayInMin !== void 0 ? _alarmInfo$delayInMin : alarmInfo.periodInMinutes) !== null && _ref !== void 0 ? _ref : 0) * 60 * 1000
}, typeof alarmInfo.periodInMinutes === 'number' ? {
periodInMinutes: alarmInfo.periodInMinutes
} : {});
}),
get: jest.fn(function (arg1, arg2) {
var _find4;
var name = (_find4 = [arg1, arg2].find(function (it) {
return typeof it === 'string';
})) !== null && _find4 !== void 0 ? _find4 : '';
var callback = [arg1, arg2].find(function (it) {
return typeof it === 'function';
});
var alarm = createdAlarms[name];
if (typeof callback === 'function') {
callback(alarm);
} else {
return Promise.resolve(alarm);
}
}),
getAll: jest.fn(function (callback) {
var alarms = Object.values(createdAlarms);
if (typeof callback === 'function') {
callback(alarms);
} else {
return Promise.resolve(alarms);
}
}),
onAlarm: createEventListeners()
};

// https://developer.chrome.com/extensions/omnibox

var omnibox = {
Expand Down Expand Up @@ -239,26 +334,6 @@ var tabs = {
})
};

var createEventListeners = function createEventListeners() {
var listeners = [];
return {
addListener: jest.fn(function (listener) {
listeners.push(listener);
}),
removeListener: jest.fn(function (listener) {
listeners = listeners.filter(function (l) {
return l !== listener;
});
}),
hasListener: jest.fn(function (listener) {
return listeners.includes(listener);
}),
hasListeners: jest.fn(function () {
return listeners.length > 0;
})
};
};

var syncStore = {};
var localStore = {};
var managedStore = {};
Expand Down Expand Up @@ -644,6 +719,7 @@ globalThis[Symbol["for"]('jest-webextension-mock')] = _objectSpread2({
extensionPath: 'moz-extension://8b413e68-1e0d-4cad-b98e-1eb000799783/'
}, globalThis[Symbol["for"]('jest-webextension-mock')]);
var chrome = {
alarms: alarms,
omnibox: omnibox,
tabs: tabs,
runtime: runtime,
Expand Down
Loading