Skip to content

Commit

Permalink
Add permissions API (#177)
Browse files Browse the repository at this point in the history
* Add permissions API

* Add build
  • Loading branch information
hranum authored Oct 4, 2023
1 parent 02a1e2a commit c0b1bbf
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
39 changes: 39 additions & 0 deletions __tests__/permissions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
test('chrome.permissions.contains', () => {
expect(jest.isMockFunction(chrome.permissions.contains)).toBe(true);
chrome.permissions.contains({ permissions: ['tabs'] }, () => {});
expect(chrome.permissions.contains).toBeCalled();
});

test('chrome.permissions.getAll', () => {
expect(jest.isMockFunction(chrome.permissions.getAll)).toBe(true);
chrome.permissions.getAll(() => {});
expect(chrome.permissions.getAll).toBeCalled();
});

test('chrome.permissions.remove', () => {
expect(jest.isMockFunction(chrome.permissions.remove)).toBe(true);
chrome.permissions.remove({ permissions: ['tabs'] }, () => {});
expect(chrome.permissions.remove).toBeCalled();
});

test('chrome.permissions.request', () => {
expect(jest.isMockFunction(chrome.permissions.request)).toBe(true);
chrome.permissions.request({ permissions: ['tabs'] }, () => {});
expect(chrome.permissions.request).toBeCalled();
});

test('chrome.permissions.onAdded.addListener', () => {
expect(jest.isMockFunction(chrome.permissions.onAdded.addListener)).toBe(
true
);
chrome.permissions.onAdded.addListener(() => {});
expect(chrome.permissions.onAdded.addListener).toBeCalled();
});

test('chrome.permissions.onRemoved.addListener', () => {
expect(jest.isMockFunction(chrome.permissions.onRemoved.addListener)).toBe(
true
);
chrome.permissions.onRemoved.addListener(() => {});
expect(chrome.permissions.onRemoved.addListener).toBeCalled();
});
17 changes: 16 additions & 1 deletion dist/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ var downloads = {
showDefaultFolder: jest.fn()
};

// https://developer.chrome.com/extensions/permissions
var permissions = {
contains: jest.fn(),
getAll: jest.fn(),
remove: jest.fn(),
request: jest.fn(),
onAdded: {
addListener: jest.fn()
},
onRemoved: {
addListener: jest.fn()
}
};

var geckoProfiler = {
stop: jest.fn(function () {
return Promise.resolve();
Expand Down Expand Up @@ -620,7 +634,8 @@ var chrome = {
i18n: i18n,
webNavigation: webNavigation,
extension: extension,
downloads: downloads
downloads: downloads,
permissions: permissions
};
// Firefox uses 'browser' but aliases it to chrome

Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import { i18n } from './i18n';
import { webNavigation } from './webNavigation';
import { extension } from './extension';
import { downloads } from './downloads';
import { permissions } from './permissions';

// Firefox specific API
import { geckoProfiler } from './geckoProfiler';

globalThis[Symbol.for('jest-webextension-mock')] = {
extensionPath: 'moz-extension://8b413e68-1e0d-4cad-b98e-1eb000799783/',
...globalThis[Symbol.for('jest-webextension-mock')]
extensionPath: 'moz-extension://8b413e68-1e0d-4cad-b98e-1eb000799783/',
...globalThis[Symbol.for('jest-webextension-mock')],
};

const chrome = {
Expand All @@ -31,6 +32,7 @@ const chrome = {
webNavigation,
extension,
downloads,
permissions,
};

export { chrome };
Expand Down
17 changes: 17 additions & 0 deletions src/permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// https://developer.chrome.com/extensions/permissions

export let onMessageListeners = [];
export let onMessageExternalListeners = [];

export const permissions = {
contains: jest.fn(),
getAll: jest.fn(),
remove: jest.fn(),
request: jest.fn(),
onAdded: {
addListener: jest.fn(),
},
onRemoved: {
addListener: jest.fn(),
},
};

0 comments on commit c0b1bbf

Please sign in to comment.