import ModalTriggerService from './modal-trigger.service';

describe('ModalTriggerService', () => {
  let subject;

  beforeEach(() => {
    subject = new ModalTriggerService();
  });

  it('knows when a registered modal is open', () => {
    subject.open('my-modal');

    expect(subject.isOpen('my-modal')).toBe(true);
    expect(subject.isOpen('some-other-modal')).toBe(false);

    subject.close('my-modal');

    expect(subject.isOpen('my-modal')).toBe(false);
    expect(subject.isOpen('some-other-modal')).toBe(false);
  });

  it('tracks actionInProgress status', () => {
    subject.open('my-modal');
    subject.setIsInProgress('my-modal', true);

    expect(subject.isInProgress('my-modal')).toBe(true);
    expect(subject.isInProgress('some-other-modal')).toBe(false);

    subject.setIsInProgress('my-modal', false);

    expect(subject.isInProgress('my-modal')).toBe(false);
    expect(subject.isInProgress('some-other-modal')).toBe(false);
  });

  it('tracks error status', () => {
    subject.open('my-modal');
    subject.setError('my-modal', new Error('BOOM'));

    expect(subject.getError('my-modal')).toEqual(new Error('BOOM'));
    expect(subject.getError('some-other-modal')).toEqual(null);

    subject.setError('my-modal', null);

    expect(subject.getError('my-modal')).toEqual(null);
    expect(subject.getError('some-other-modal')).toEqual(null);
  });

  it('clears state when the modal is closed and re-opened', () => {
    subject.open('my-modal');
    subject.setIsInProgress('my-modal', true);
    subject.setError('my-modal', new Error('BOOM'));

    subject.close('my-modal');
    subject.open('my-modal');

    expect(subject.isInProgress('my-modal')).toBeFalsy();
    expect(subject.getError('my-modal')).toBeUndefined();
  });
});
