{"size": 2022, "ext": "js", "max_stars_repo_path": "src/query.test.js", "max_stars_repo_name": "flybondi/flynamo", "max_stars_repo_stars_event_min_datetime": "2018-07-11T18:33:42.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-21T17:20:28.000Z", "max_issues_repo_path": "src/query.test.js", "max_issues_repo_name": "flybondi/flynamo", "max_issues_repo_issues_event_min_datetime": "2018-07-24T21:03:29.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-21T15:43:04.000Z", "max_forks_repo_path": "src/query.test.js", "max_forks_repo_name": "flybondi/flynamo", "max_forks_repo_forks_event_min_datetime": "2018-07-24T20:58:53.000Z", "max_forks_repo_forks_event_max_datetime": "2018-07-24T20:58:53.000Z"}
{"max_stars_count": 15, "max_issues_count": 38, "max_forks_count": 1, "avg_line_length": 34.8620689655, "max_line_length": 96, "alphanum_fraction": 0.6107814045}
'use strict';
const createQuerier = require('./query');

describe('the query function', () => {
  test('should call `query` internally', async () => {
    const mockQuery = jest.fn().mockResolvedValue({});
    const { query } = createQuerier({ query: mockQuery });
    await query();
    expect(mockQuery).toHaveBeenCalled();
  });

  test('should return `undefined` if no items are returned', async () => {
    const mockQuery = jest.fn().mockResolvedValue({});
    const { query } = createQuerier({ query: mockQuery });
    expect(await query()).toBeUndefined();
  });

  test('should return unwrapped items', async () => {
    const mockQuery = jest.fn().mockResolvedValue({ Items: [{ foo: { S: 'bar' } }] });
    const { query } = createQuerier({ query: mockQuery });
    expect(await query()).toEqual([{ foo: 'bar' }]);
  });

  test('should return a full response object with the items unwrapped', async () => {
    const mockQuery = jest.fn().mockResolvedValue({ Items: [{ foo: { S: 'bar' } }], Count: 1 });
    const { query } = createQuerier({ query: mockQuery });
    expect(await query({}, { raw: true })).toEqual({ Items: [{ foo: 'bar' }], Count: 1 });
  });
});

describe('the queryFor function', () => {
  test('should add `TableName` automatically to any request', async () => {
    const mockQuery = jest.fn().mockResolvedValue({});
    const { queryFor } = createQuerier({ query: mockQuery });
    await queryFor('some_table')({});
    expect(mockQuery).toHaveBeenCalledWith(
      expect.objectContaining({
        TableName: 'some_table'
      }),
      undefined
    );
  });

  test('should create a query function with two arguments', async () => {
    const mockQuery = jest.fn().mockResolvedValue({});
    const { queryFor } = createQuerier({ query: mockQuery });
    const query = queryFor('some_table');
    await query({}, { raw: true });

    expect(mockQuery).toHaveBeenCalledWith(
      expect.objectContaining({
        TableName: 'some_table'
      }),
      { raw: true }
    );
  });
});
