Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Apr 1, 2024
1 parent e613863 commit 646a36e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
14 changes: 8 additions & 6 deletions source/demo/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ oneEvent(chrome.tabs.onCreated).then(() => {
console.log('Tab created');
});

oneEvent(chrome.tabs.onMoved, (tabId, moveInfo) => {
console.log('Should handle event?', {tabId, moveInfo});
return Boolean(tabId % 5);
}).then(() => {
oneEvent(chrome.tabs.onMoved, {
filter(tabId, moveInfo) {
console.log('Should handle event?', {tabId, moveInfo});
return Boolean(tabId % 5);
}}).then(() => {
console.log('Tab moved');
});

const httpsOnlyFilter = (tab: chrome.tabs.Tab) => Boolean(tab.pendingUrl?.startsWith('https'));
oneEvent(
chrome.tabs.onCreated,
httpsOnlyFilter,
chrome.tabs.onCreated, {
filter: httpsOnlyFilter,
},
).then(() => {
console.log('HTTPS Tab created');
});
41 changes: 24 additions & 17 deletions source/one-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ describe('oneEvent', () => {
it('it should resolve when a specific event is received', async () => {
expect(chrome.runtime.onMessage.hasListeners()).toBe(false);
const eventPromise = oneEvent(
chrome.runtime.onMessage,
{filter: ({greeting}) => greeting === 'sup'},
chrome.runtime.onMessage, {
filter: ({greeting}) => greeting === 'sup',
},
);
expect(chrome.runtime.onMessage.hasListeners()).toBe(true);

Expand All @@ -47,22 +48,28 @@ describe('oneEvent', () => {
});

it('it should resolve original event\'s parameters', () => {
void oneEvent(chrome.tabs.onMoved, {filter(tabId, moveInfo) {
expectTypeOf(tabId).toEqualTypeOf<number>();
expectTypeOf(moveInfo).toEqualTypeOf<chrome.tabs.TabMoveInfo>();
return true;
}});
void oneEvent(chrome.tabs.onMoved, {
filter(tabId, moveInfo) {
expectTypeOf(tabId).toEqualTypeOf<number>();
expectTypeOf(moveInfo).toEqualTypeOf<chrome.tabs.TabMoveInfo>();
return true;
},
});

void oneEvent(chrome.runtime.onMessage, {filter(message, sender, sendResponse) {
expectTypeOf(message).toEqualTypeOf<any>();
expectTypeOf(sender).toEqualTypeOf<Runtime.MessageSender>();
expectTypeOf(sendResponse).toEqualTypeOf<(response?: any) => void>();
return true;
}});
void oneEvent(chrome.runtime.onMessage, {
filter(message, sender, sendResponse) {
expectTypeOf(message).toEqualTypeOf<any>();
expectTypeOf(sender).toEqualTypeOf<Runtime.MessageSender>();
expectTypeOf(sendResponse).toEqualTypeOf<(response?: any) => void>();
return true;
},
});

void oneEvent(chrome.cookies.onChanged, {filter(changeInfo) {
expectTypeOf(changeInfo).toEqualTypeOf<Cookies.CookieChangeInfo>();
return true;
}});
void oneEvent(chrome.cookies.onChanged, {
filter(changeInfo) {
expectTypeOf(changeInfo).toEqualTypeOf<Cookies.CookieChangeInfo>();
return true;
},
});
});
});

0 comments on commit 646a36e

Please sign in to comment.