trurl-master / jsdom-testing-mocks

A set of tools for emulating browser behavior in jsdom environment
MIT License
116 stars 7 forks source link

Allow adding more properties to the IntersectionDescription #31

Closed ChromeQ closed 2 years ago

ChromeQ commented 2 years ago

I am using mockIntersectionObserver.enterNode and the type allows passing a desc which is typed as IntersectionDescription This type is too basic, some callbacks need to know more about the "entry" such as the intersectionRatio or boundingClientRect

I realise this is probably difficult to do as jsdom does not render actual layouts so a lot of these cannot be calculated easily (or at all)

However you do spread the desc in the enter/leave helper functions,

setNodeState(index, { ...desc, isIntersecting: true });

So I propose that the IntersectionDescription should look a lot more like the TS lib definition with all optionals so they are spread into your callback, then it is up to the developer to set the correct values as the test may require

ChromeQ commented 2 years ago

I noticed you commented out some of these properties https://github.com/trurl-master/jsdom-testing-mocks/blob/52a5b4ce70ab978f39adf07c061ba8c4061908c1/src/mocks/intersection-observer.ts#L63

Is this something you considered or tried but did not work?

I can get around it for now but would like to avoid the ts-ignore & eslint-disable if possible:

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-next-line Until `jsdom-testing-mocks` allows more props
intersectionObserver.enterNode(screen.getByText('foo'), { intersectionRatio: 1 });
trurl-master commented 2 years ago

Hey @ChromeQ

Thanks for the issue. You are correct. It would be better to have correct definitions. Probably I just forgot to implement them as it wasn't relevant to me at the moment. I will take a look at it in the next few days, or if you have time, you can make a quick PR

ChromeQ commented 2 years ago

I am happy to open a PR very soon, just checking this wasn't something you had spent time on and found it to be impossible. Thanks

trurl-master commented 2 years ago

@ChromeQ published under 1.2.2. Please check that it works for you

Thank you very much for your contribution! ❤️

ChromeQ commented 2 years ago

Updated and can confirm it is working! Thanks

2022-03-03_11-00