
import { createNAP } from '../lib/index';

describe('The initialization of the SDK', () => {
  it('should report errors through the errors stream if no hostbridge is available', () => {
    const nap = createNAP();

    expect(Object.keys(nap)).toContain('errors$');
    nap.errors$.subscribe(
      data => {
        expect([100, 105, 106].indexOf(data.id) !== -1).toBeTruthy();
      },
      () => {
        fail('Error stream should not go to error state!');
      },
      () => {
        fail('Error stream should not complete ever!');
      }
    );
  });
  it('should report errors through the errors stream if available hostbridge is malformed', () => {
    window['NAP'] = {};
    const nap = createNAP();
    expect(Object.keys(nap)).toContain('errors$');

    nap.errors$.subscribe(
      data => {
        expect([105, 106].indexOf(data.id) !== -1).toBeTruthy();
      },
      () => {
        fail('Error stream should not go to error state!');
      },
      () => {
        fail('Error stream should not complete ever!');
      }
    );
  });
  it('should return a host bridge', () => {
    window['NAP'] = {
      send: (): undefined => undefined,
      setCallback: (): undefined => undefined
    };
    const nap = createNAP();
    expect(Object.keys(nap)).toContain('toHost$');
    expect(Object.keys(nap)).toContain('fromHost$');
    expect(Object.keys(nap)).toContain('errors$');
    expect(Object.keys(nap)).toContain('send');
    expect(Object.keys(nap)).toContain('subscribe');
    expect(Object.keys(nap)).toContain('unsubscribe');
  });
});
