Skip to content

Commit

Permalink
[feature] Add support for chrome.downloads API (#134)
Browse files Browse the repository at this point in the history
* Add support for chrome.downloads API

* Add Promise Support
  • Loading branch information
jasonandmonte authored Feb 10, 2021
1 parent f773558 commit d38a9e1
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 8 deletions.
164 changes: 164 additions & 0 deletions __tests__/downloads.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
describe('browser.downloads', () => {
test('acceptDanger', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.acceptDanger)).toBe(true);
chrome.downloads.acceptDanger(downloadId, callback);
expect(chrome.downloads.acceptDanger).toHaveBeenCalledTimes(1);
expect(chrome.downloads.acceptDanger).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('acceptDanger Promise', () => {
const downloadId = 1;

expect(chrome.downloads.acceptDanger(downloadId)).resolves.toBeUndefined();
});
test('cancel', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.cancel)).toBe(true);
chrome.downloads.cancel(downloadId, callback);
expect(chrome.downloads.cancel).toHaveBeenCalledTimes(1);
expect(chrome.downloads.cancel).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('cancel Promise', () => {
const downloadId = 1;

expect(chrome.downloads.cancel(downloadId)).resolves.toBeUndefined();
});
test('download', (done) => {
const options = {};
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.download)).toBe(true);
chrome.downloads.download(options, callback);
expect(chrome.downloads.download).toHaveBeenCalledTimes(1);
expect(chrome.downloads.download).toHaveBeenLastCalledWith(options, callback);
expect(callback).toHaveBeenCalled();
});
test('download Promise', () => {
const options = {};

expect(chrome.downloads.download(options)).resolves.toBeUndefined();
});
test('erase', (done) => {
const query = {};
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.erase)).toBe(true);
chrome.downloads.erase(query, callback);
expect(chrome.downloads.erase).toHaveBeenCalledTimes(1);
expect(chrome.downloads.erase).toHaveBeenLastCalledWith(query, callback);
expect(callback).toHaveBeenCalled();
});
test('erase Promise', () => {
const query = {};

expect(chrome.downloads.erase(query)).resolves.toBeUndefined();
});
test('getFileIcon', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.getFileIcon)).toBe(true);
chrome.downloads.getFileIcon(downloadId, callback);
expect(chrome.downloads.getFileIcon).toHaveBeenCalledTimes(1);
expect(chrome.downloads.getFileIcon).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('getFileIcon Promise', () => {
const downloadId = 1;

expect(chrome.downloads.getFileIcon(downloadId)).resolves.toBeUndefined();
});
test('open', () => {
const downloadId = 1;

expect(jest.isMockFunction(chrome.downloads.open)).toBe(true);
chrome.downloads.open(downloadId);
expect(chrome.downloads.open).toHaveBeenCalledTimes(1);
expect(chrome.downloads.open).toHaveBeenLastCalledWith(downloadId);
});
test('pause', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.pause)).toBe(true);
chrome.downloads.pause(downloadId, callback);
expect(chrome.downloads.pause).toHaveBeenCalledTimes(1);
expect(chrome.downloads.pause).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('pause Promise', () => {
const downloadId = 1;

expect(chrome.downloads.pause(downloadId)).resolves.toBeUndefined();
});
test('removeFile', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.removeFile)).toBe(true);
chrome.downloads.removeFile(downloadId, callback);
expect(chrome.downloads.removeFile).toHaveBeenCalledTimes(1);
expect(chrome.downloads.removeFile).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('removeFile Promise', () => {
const downloadId = 1;

expect(chrome.downloads.removeFile(downloadId)).resolves.toBeUndefined();
});
test('resume', (done) => {
const downloadId = 1;
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.resume)).toBe(true);
chrome.downloads.resume(downloadId, callback);
expect(chrome.downloads.resume).toHaveBeenCalledTimes(1);
expect(chrome.downloads.resume).toHaveBeenLastCalledWith(downloadId, callback);
expect(callback).toHaveBeenCalled();
});
test('resume Promise', () => {
const downloadId = 1;

expect(chrome.downloads.resume(downloadId)).resolves.toBeUndefined();
});
test('search', (done) => {
const query = {};
const callback = jest.fn(() => done());

expect(jest.isMockFunction(chrome.downloads.search)).toBe(true);
chrome.downloads.search(query, callback);
expect(chrome.downloads.search).toHaveBeenCalledTimes(1);
expect(chrome.downloads.search).toHaveBeenLastCalledWith(query, callback);
expect(callback).toHaveBeenCalled();
});
test('search Promise', () => {
const query = {};

expect(chrome.downloads.search(query)).resolves.toBeUndefined();
});
test('setShelfEnabled', () => {
expect(jest.isMockFunction(chrome.downloads.setShelfEnabled)).toBe(true);
chrome.downloads.setShelfEnabled(true);
expect(chrome.downloads.setShelfEnabled).toHaveBeenCalledTimes(1);
expect(chrome.downloads.setShelfEnabled).toHaveBeenLastCalledWith(true);
});
test('show', () => {
const downloadId = 1;

expect(jest.isMockFunction(chrome.downloads.show)).toBe(true);
chrome.downloads.show(downloadId);
expect(chrome.downloads.show).toHaveBeenCalledTimes(1);
expect(chrome.downloads.show).toHaveBeenLastCalledWith(downloadId);
});
test('showDefaultFolder', () => {
expect(jest.isMockFunction(chrome.downloads.showDefaultFolder)).toBe(true);
chrome.downloads.showDefaultFolder();
expect(chrome.downloads.showDefaultFolder).toHaveBeenCalledTimes(1);
});
});
33 changes: 25 additions & 8 deletions dist/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var runtime = {
// https://developer.chrome.com/extensions/tabs
var tabs = {
get: jest.fn(function () {
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () { };
return cb({});
}),
getCurrent: jest.fn(function (cb) {
Expand Down Expand Up @@ -119,31 +119,31 @@ var tabs = {
}),
duplicate: jest.fn(function () {
var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () { };
return cb(Object.assign({}, {
id: id
}));
}),
query: jest.fn(function () {
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () { };
return cb([{}]);
}),
highlight: jest.fn(function () {
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () { };
return cb();
}),
update: jest.fn(function () {
var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () { };
return cb(Object.assign({}, props, {
id: id
}));
}),
move: jest.fn(function () {
var ids = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
var cb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () { };
return cb(ids.map(function (id) {
return Object.assign({}, props, {
id: id
Expand Down Expand Up @@ -481,6 +481,22 @@ var extension = {
getURL: jest.fn()
};

var downloads = {
acceptDanger: jest.fn((downloadId, cb) => cbOrPromise(cb)),
cancel: jest.fn((downloadId, cb) => cbOrPromise(cb)),
download: jest.fn((options, cb) => cbOrPromise(cb)),
erase: jest.fn((query, cb) => cbOrPromise(cb)),
getFileIcon: jest.fn((downloadId, cb) => cbOrPromise(cb)),
open: jest.fn(),
pause: jest.fn((downloadId, cb) => cbOrPromise(cb)),
removeFile: jest.fn((downloadId, cb) => cbOrPromise(cb)),
resume: jest.fn((downloadId, cb) => cbOrPromise(cb)),
search: jest.fn((query, cb) => cbOrPromise(cb)),
setShelfEnabled: jest.fn(),
show: jest.fn(),
showDefaultFolder: jest.fn(),
};

var geckoProfiler = {
stop: jest.fn(function () {
return Promise.resolve();
Expand Down Expand Up @@ -519,9 +535,10 @@ var chrome = {
notifications: notifications,
i18n: i18n,
webNavigation: webNavigation,
extension: extension
extension: extension,
downloads: downloads,
};
// Firefox uses 'browser' but aliases it to chrome
// Firefox uses 'browser' but aliases it to chrome

/**
* This is a setup file we specify as our 'main' entry point
Expand Down
23 changes: 23 additions & 0 deletions src/downloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const cbOrPromise = (cb, value) => {
if (cb !== undefined) {
return cb(value);
}

return Promise.resolve(value);
};

export const downloads = {
acceptDanger: jest.fn((downloadId, cb) => cbOrPromise(cb)),
cancel: jest.fn((downloadId, cb) => cbOrPromise(cb)),
download: jest.fn((options, cb) => cbOrPromise(cb)),
erase: jest.fn((query, cb) => cbOrPromise(cb)),
getFileIcon: jest.fn((downloadId, cb) => cbOrPromise(cb)),
open: jest.fn(),
pause: jest.fn((downloadId, cb) => cbOrPromise(cb)),
removeFile: jest.fn((downloadId, cb) => cbOrPromise(cb)),
resume: jest.fn((downloadId, cb) => cbOrPromise(cb)),
search: jest.fn((query, cb) => cbOrPromise(cb)),
setShelfEnabled: jest.fn(),
show: jest.fn(),
showDefaultFolder: jest.fn(),
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { notifications } from './notifications';
import { i18n } from './i18n';
import { webNavigation } from './webNavigation';
import { extension } from './extension';
import { downloads } from './downloads';

// Firefox specific API
import { geckoProfiler } from './geckoProfiler';
Expand All @@ -24,6 +25,7 @@ const chrome = {
i18n,
webNavigation,
extension,
downloads,
};

export { chrome };
Expand Down

0 comments on commit d38a9e1

Please sign in to comment.