{"size": 1387, "ext": "js", "max_stars_repo_path": "src/models/__tests__/hotSearchKeywords.spec.js", "max_stars_repo_name": "kidkcl/telegram-bot", "max_stars_repo_stars_event_min_datetime": "2017-09-19T07:14:46.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-16T08:12:45.000Z", "max_issues_repo_path": "src/models/__tests__/hotSearchKeywords.spec.js", "max_issues_repo_name": "kidkcl/telegram-bot", "max_issues_repo_issues_event_min_datetime": "2017-07-10T16:23:48.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:49:18.000Z", "max_forks_repo_path": "src/models/__tests__/hotSearchKeywords.spec.js", "max_forks_repo_name": "kidkcl/telegram-bot", "max_forks_repo_forks_event_min_datetime": "2017-07-17T15:45:59.000Z", "max_forks_repo_forks_event_max_datetime": "2018-10-31T08:56:00.000Z"}
{"max_stars_count": 3, "max_issues_count": 65, "max_forks_count": 5, "avg_line_length": 25.2181818182, "max_line_length": 80, "alphanum_fraction": 0.6272530642}
jest.mock('mongodb');
jest.mock('../database');

const { getMongoDatabase } = require('../database');
const insertHotSearchKeywords = require('../hotSearchKeywords').default;

describe('insertHotSearchKeywords', () => {
  let defaultDate;
  beforeEach(() => {
    defaultDate = Date;
    const constantDate = new Date('2017-06-13T04:41:20');
    // eslint-disable-next-line no-global-assign
    Date = class extends Date {
      constructor() {
        super();
        return constantDate;
      }
    };
    getMongoDatabase.mockReturnValue({
      collection: jest.fn().mockReturnThis(),
    });
    getMongoDatabase().collection.mockReturnValue({
      update: jest.fn(),
      insertOne: jest.fn(),
    });
  });

  afterEach(() => {
    // eslint-disable-next-line no-global-assign
    Date = defaultDate;
  });

  it('should be defined', () => {
    expect(insertHotSearchKeywords).toBeDefined();
  });

  it('should call insertOne', async () => {
    const keyword = '123';
    const user = {
      userId: '666',
    };
    const now = new Date();

    await insertHotSearchKeywords(keyword, user);

    expect(getMongoDatabase).toBeCalled();
    expect(getMongoDatabase().collection).toBeCalledWith('hot_search_keywords');
    expect(getMongoDatabase().collection().insertOne).toBeCalledWith({
      keyword,
      created_at: now,
      userId: user.userId,
    });
  });
});
