{"size": 2928, "ext": "js", "max_stars_repo_path": "src/index.test.js", "max_stars_repo_name": "glorious-codes/glorious-rasket", "max_stars_repo_stars_event_min_datetime": "2020-06-14T21:13:26.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-12T14:36:07.000Z", "max_issues_repo_path": "src/index.test.js", "max_issues_repo_name": "glorious-codes/glorious-rasket", "max_issues_repo_issues_event_min_datetime": "2020-07-21T06:27:13.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-13T20:35:33.000Z", "max_forks_repo_path": "src/index.test.js", "max_forks_repo_name": "glorious-codes/glorious-rasket", "max_forks_repo_forks_event_min_datetime": "2021-05-17T17:04:47.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-17T17:04:47.000Z"}
{"max_stars_count": 2, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 39.5675675676, "max_line_length": 112, "alphanum_fraction": 0.7049180328}
import rasket from './';

describe('Rasket', () => {
  it('should not subscribe if event name has not been given', () => {
    console.error = jest.fn();
    const eventNameRequiredErrorMessage = '[Rasket] You must pass an event name (String) as first argument.';
    const callback = jest.fn();
    rasket.subscribe(callback);
    expect(console.error).toHaveBeenCalledWith(eventNameRequiredErrorMessage);
  });

  it('should not subscribe if callback has not been given', () => {
    console.error = jest.fn();
    const callbackRequiredErrorMessage = '[Rasket] You must pass a callback (Function) as second argument.';
    rasket.subscribe('testTopic');
    expect(console.error).toHaveBeenCalledWith(callbackRequiredErrorMessage);
  });

  it('should subscribe', () => {
    const eventName = 'testTopic';
    const data = {some: 'data'};
    const callback = jest.fn();
    const id = rasket.subscribe(eventName, callback);
    rasket.publish(eventName, data);
    expect(callback).toHaveBeenCalledWith(data);
    rasket.unsubscribe(id);
  });

  it('should only execute callbacks for the published event name', () => {
    const firstCallback = jest.fn();
    const secondCallback = jest.fn();
    const firstSubscriberId = rasket.subscribe('firstTopic', firstCallback);
    const secondSubscriberId = rasket.subscribe('secondTopic', secondCallback);
    rasket.publish('firstTopic');
    expect(firstCallback).toHaveBeenCalled();
    expect(secondCallback).not.toHaveBeenCalled();
    rasket.unsubscribe(firstSubscriberId);
    rasket.unsubscribe(secondSubscriberId);
  });

  it('should unsubscribe', () => {
    const eventName = 'testTopic';
    const callback = jest.fn();
    const id = rasket.subscribe(eventName, callback);
    rasket.unsubscribe(id);
    rasket.publish(eventName);
    expect(callback).not.toHaveBeenCalled();
  });

  it('should only unsubscribe the subscriber for the given subscriber id', () => {
    const firstCallback = jest.fn();
    const secondCallback = jest.fn();
    const firstSubscriberId = rasket.subscribe('firstTopic', firstCallback);
    const secondSubscriberId = rasket.subscribe('secondTopic', secondCallback);
    rasket.publish('firstTopic');
    expect(firstCallback).toHaveBeenCalled();
    rasket.publish('secondTopic');
    expect(secondCallback).toHaveBeenCalled();
    rasket.unsubscribe(secondSubscriberId);
    rasket.publish('secondTopic');
    expect(secondCallback).toHaveBeenCalledTimes(1);
    rasket.unsubscribe(firstSubscriberId);
  });

  it('should not unsubscribe if subscriber id has not been given', () => {
    console.error = jest.fn();
    const listenerIdRequiredErrorMessage = '[Rasket] You must pass a subscriber id (String) as first argument.';
    const id = rasket.subscribe('testTopic', jest.fn());
    rasket.unsubscribe();
    expect(console.error).toHaveBeenCalledWith(listenerIdRequiredErrorMessage);
    rasket.unsubscribe(id);
  });
});
