clarkbw / jest-webextension-mock

A module to mock WebExtensions in Jest
BSD 3-Clause "New" or "Revised" License
93 stars 37 forks source link

Add support for browser.windows.* APIs #89

Open pureooze opened 6 years ago

pureooze commented 6 years ago

First of all thanks for making this library, it seems really promising!

I was trying to use this library in my extension and followed the instructions in the README but I keep getting this error when I run certain tests:

failing-test

I think the issue is just that there are no mocks for this part of the API yet (correct me if I am wrong). Let me know if this is actually an issue or I am just misusing the library.

If it is just a case of missing support, I think we can add it. If you want I can make a PR to add these mocks, I looked over the existing source code and I think I understand how this library works.

Thanks!

clarkbw commented 6 years ago

If it is just a case of missing support, I think we can add it. If you want I can make a PR to add these mocks, I looked over the existing source code and I think I understand how this library works.

Yes, please do! I'd love a PR to add support. 🔨

pureooze commented 6 years ago

I made the mocks for the windows api (see here) but I just had a question about how jest-webextension-mock works 🤔 .

In the documentation for the chrome.tab.get API the callback is passed a Tab object. But when I look in the mocks I see: export const tabs = { get: jest.fn((id = '', cb = () => {}) => cb({})), ... }

So I have two questions:

  1. If the code being tested actually passes in a callback it will expect a Tab object with certain properties but from what I can tell it will always get an empty object?
  2. How does this deal with the difference between the browser. and chrome. APIs? From what I understand the WebExtension API expects promises while the chrome API expects callbacks. So if I understand this correctly if we call browser.tabs.get in our code we wont have anything returned?
clarkbw commented 6 years ago

If the code being tested actually passes in a callback it will expect a Tab object with certain properties but from what I can tell it will always get an empty object?

I'm guessing you'll need to fix that and it was just stubbed out.

How does this deal with the difference between the browser. and chrome. APIs? From what I understand the WebExtension API expects promises while the chrome API expects callbacks. So if I understand this correctly if we call browser.tabs.get in our code we wont have anything returned?

In a number of places where this is a concern we use a pattern like this:

https://github.com/clarkbw/jest-webextension-mock/blob/3a37dd93ca52a0d991ea29180be394cd923a4c05/src/browserAction.js#L1-L6

ronilitman commented 2 years ago

Any progress on this? :)