ghost-keychain/packages/extension-mocks/src/chrome.ts
Tarik Gul b6d42c28b5
Transition from manifest v2 to v3 for chrome (#1367)
* Update manifest.json

* replace `extension.getUrl` to `runtime.getUrl`

* Update localStorage to async chrome.storage.local

* Change action to browserAction

* fix bug

* revert async changes

* revert test change

* Fix localStorage breaking change

* .extension -> .runtime

* fix undefined behaviour

* fixed tests

* lint

* Up some callbacks to async

* Change where extractMetadata is called

* fix  in mock

* Update background field to take in type

* Change background.js to service_worker.js

* Update id and request origin for service worker

* updated chrome mock

* 13 tests passing

* Fix async issue

* switch naming back to original

* linting

* Fix linting in base

* remove timeOut

* added permission to alarms in manifest

* fixed alarms uncaught errors

* Cleanup addr for tests

---------

Co-authored-by: bee344 <alberto.penayo@parity.io>
2024-07-07 22:32:42 -04:00

65 lines
1.5 KiB
TypeScript

// Copyright 2019-2024 @polkadot/extension-mocks authors & contributors
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable @typescript-eslint/no-explicit-any */
import chrome from './chromeWrapper';
class MessagingFake {
private listeners: ((...params: unknown[]) => unknown)[] = [];
get onMessage (): any {
return {
addListener: (cb: (...params: unknown[]) => unknown) => this.listeners.push(cb)
};
}
get onDisconnect (): any {
return {
addListener: (): any => undefined
};
}
postMessage (data: unknown): void {
this.listeners.forEach((cb) => cb.call(this, data));
}
}
const messagingFake = new MessagingFake();
chrome.runtime.connect.returns(messagingFake);
chrome.storage.local.get.returns(
new Promise<Record<string, any>>((resolve, reject) => {
try {
const result = {
authUrls: JSON.stringify({
'localhost:3000': {
authorizedAccounts: ['5FbSap4BsWfjyRhCchoVdZHkDnmDm3NEgLZ25mesq4aw2WvX'],
count: 0,
id: '11',
origin: 'example.com',
url: 'http://localhost:3000'
}
})
};
resolve(result);
} catch (error) {
reject(error);
}
}));
chrome.storage.local.set.returns(
new Promise<void>((resolve, reject) => {
try {
resolve();
} catch (error) {
reject(error);
}
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access
(window as any).chrome = (globalThis as any).chrome = chrome;
export default chrome;