Skip to content

Commit

Permalink
fix(fake-browser): Implement runtime.getURL
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinker1 committed Mar 2, 2023
1 parent c8dba1c commit 270bd8f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
16 changes: 15 additions & 1 deletion packages/fake-browser/src/apis/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Runtime } from 'webextension-polyfill';
import { fakeBrowser } from '..';

describe('Fake Storage API', () => {
describe('Fake Runtime API', () => {
beforeEach(fakeBrowser.reset);

describe('messaging', () => {
Expand Down Expand Up @@ -103,4 +103,18 @@ describe('Fake Storage API', () => {
expect(listener).toBeCalledTimes(1);
expect(listener).toBeCalledWith(input);
});

describe('getURL', () => {
it('should return an extension URL', () => {
expect(fakeBrowser.runtime.getURL('options.html')).toBe(
`chrome-extension://test-extension-id/options.html`,
);
});

it('should return an extension URL, ignoring leading slashes', () => {
expect(fakeBrowser.runtime.getURL('/options.html')).toBe(
`chrome-extension://test-extension-id/options.html`,
);
});
});
});
5 changes: 4 additions & 1 deletion packages/fake-browser/src/apis/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserOverrides, FakeBrowser } from '../types';
import { BrowserOverrides } from '../types';
import { defineEventWithTrigger } from '../utils/defineEventWithTrigger';
import { Runtime } from 'webextension-polyfill';

Expand All @@ -24,6 +24,9 @@ export const runtime: BrowserOverrides['runtime'] = {
runtime.id = TEST_ID;
},
id: TEST_ID,
getURL(path: string) {
return `chrome-extension://${runtime.id}/${path.replace(/^\//, '')}`;
},
onInstalled,
onMessage,
onStartup,
Expand Down
3 changes: 1 addition & 2 deletions packages/fake-browser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ export interface BrowserOverrides {
onButtonClicked: EventForTesting<[notificationId: string, buttonIndex: number]>;
onShown: EventForTesting<[notificationId: string]>;
};
runtime: {
runtime: Pick<Runtime.Static, 'id' | 'getURL'> & {
resetState(): void;
id: string;
onSuspend: EventForTesting<[]>;
onSuspendCanceled: EventForTesting<[]>;
onStartup: EventForTesting<[]>;
Expand Down

0 comments on commit 270bd8f

Please sign in to comment.