krakenjs / post-robot

Cross domain post-messaging on the client side using a simple listener/client pattern.
Apache License 2.0
741 stars 92 forks source link

Unit Testing `on` method #120

Open esponges opened 1 month ago

esponges commented 1 month ago

Hi,

We're using this tool in an internal SDK using React and I was wondering if there's a way to unit test the on event? I was checking the src code and you use the getWindows method helper but I'm not very sure if using a similar approach would work.

// App.tsx
  useEffect(() => {
    const onEvent = on('test', (event) => {
      console.log('do something', event);
      return Promise.resolve(event);
    });
    return () => onEvent.cancel();
  }, []);

// App.spec.tsx
describe('App', () => {
  it('test post-robot', async() => {
    const {container} = render(<App />);

    // this doesn't work — probably pass window from `getWindows` helper? 
    send(window, 'test', {
      name: 'test',
    });

    // probably assert using spying post-robot calls
  })
});