From d38a9e1a978907dc63b2c9bb1624ee096f5ece79 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 10 Feb 2021 07:01:00 -0800 Subject: [PATCH] [feature] Add support for chrome.downloads API (#134) * Add support for chrome.downloads API * Add Promise Support --- __tests__/downloads.test.js | 164 ++++++++++++++++++++++++++++++++++++ dist/setup.js | 33 ++++++-- src/downloads.js | 23 +++++ src/index.js | 2 + 4 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 __tests__/downloads.test.js create mode 100644 src/downloads.js diff --git a/__tests__/downloads.test.js b/__tests__/downloads.test.js new file mode 100644 index 0000000..49d37a9 --- /dev/null +++ b/__tests__/downloads.test.js @@ -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); + }); +}); diff --git a/dist/setup.js b/dist/setup.js index 3fff785..cd0c2eb 100644 --- a/dist/setup.js +++ b/dist/setup.js @@ -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) { @@ -119,23 +119,23 @@ 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 })); @@ -143,7 +143,7 @@ var tabs = { 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 @@ -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(); @@ -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 diff --git a/src/downloads.js b/src/downloads.js new file mode 100644 index 0000000..eeae061 --- /dev/null +++ b/src/downloads.js @@ -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(), +}; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 703644a..61b1f62 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -24,6 +25,7 @@ const chrome = { i18n, webNavigation, extension, + downloads, }; export { chrome };