import { createNAP, NAPEventType, NAPMessage } from '../lib';


const TEST_MESSAGE: NAPMessage = {
  event: { type: NAPEventType.WebAppSelected },
  date: new Date().toISOString(),
  id: 'b285d6d9-370d-4c75-8951-8a9268f32830',
};

describe('Sending messages', () => {
  beforeEach(() => {
    window['NAP'] = {
      send: (): undefined => undefined,
      setCallback: (): undefined => undefined,
    };
    spyOn(window['NAP'], 'send');
  });

  afterEach(() => {
    window['NAP'] = undefined;
  });

  it('through the ingoing stream message should result in the hostbridge receiving a message', () => {
    const { toHost$ } = createNAP();

    toHost$.next(TEST_MESSAGE);

    expect(window['NAP'].send).toHaveBeenCalledTimes(1);
    expect(window['NAP'].send).toHaveBeenCalledWith(
      JSON.stringify(TEST_MESSAGE)
    );
  });

  it('through the send handle should result in the hostbridge receiving a message', () => {
    const { send } = createNAP();

    send(TEST_MESSAGE);

    expect(window['NAP'].send).toHaveBeenCalledTimes(1);
    expect(window['NAP'].send).toHaveBeenCalledWith(
      JSON.stringify(TEST_MESSAGE)
    );
  });
});
