{"size": 2510, "ext": "js", "max_stars_repo_path": "__tests__/routes/listAuthors.test.js", "max_stars_repo_name": "motyar/quotable", "max_stars_repo_stars_event_min_datetime": "2019-06-23T23:48:23.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T10:04:30.000Z", "max_issues_repo_path": "__tests__/routes/listAuthors.test.js", "max_issues_repo_name": "motyar/quotable", "max_issues_repo_issues_event_min_datetime": "2019-09-05T11:47:34.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-17T00:59:57.000Z", "max_forks_repo_path": "__tests__/routes/listAuthors.test.js", "max_forks_repo_name": "motyar/quotable", "max_forks_repo_forks_event_min_datetime": "2019-08-01T02:25:59.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T11:13:05.000Z"}
{"max_stars_count": 730, "max_issues_count": 59, "max_forks_count": 122, "avg_line_length": 36.9117647059, "max_line_length": 76, "alphanum_fraction": 0.6852589641}
import request from 'supertest'
import { stringify } from 'query-string'
import sortBy from 'lodash/sortBy'
import app from '../../src/app'
import * as db from '../../scripts/db'
import Authors from '../../src/models/Authors'

let authorCount
let sortedAuthors

const getId = obj => obj._id
// Setup
beforeAll(async () => {
  await db.connect()
  sortedAuthors = await Authors.find({}).sort({ name: 1 })
  authorCount = await Authors.countDocuments({})
})
// Teardown
afterAll(async () => db.close())

describe('GET /authors', () => {
  it('Response matches schema', async () => {
    const { status, type, body } = await request(app).get('/authors')
    expect(status).toBe(200)
    expect(type).toBe('application/json')
    // Response matches schema
    expect(body.results).toEqual(expect.any(Array))
    expect(body.count).toEqual(body.results.length)
    expect(body.totalCount).toEqual(expect.any(Number))
    expect(body.totalPages).toEqual(expect.any(Number))
    expect(body.page).toEqual(expect.any(Number))

    // Each result should be an `Author` object with the following fields
    const author = body.results[0]
    expect(author._id).toEqual(expect.any(String))
    expect(author.slug).toEqual(expect.any(String))
    expect(author.name).toEqual(expect.any(String))
    expect(author.bio).toEqual(expect.any(String))
    expect(author.description).toEqual(expect.any(String))
    expect(author.link).toEqual(expect.any(String))
    expect(author.quoteCount).toEqual(expect.any(Number))
  })

  test(`Returns the first page of results`, async () => {
    const { body } = await request(app).get(`/authors`)
    expect(body.page).toEqual(1)
    const expectedAuthorIds = sortedAuthors.slice(0, 20).map(getId)
    expect(body.results.map(getId)).toEqual(expectedAuthorIds)
  })

  test(`Returns second page of results`, async () => {
    const query = stringify({ page: 2 })
    const { body } = await request(app).get(`/authors?${query}`)
    expect(body.page).toEqual(2)
    const expectedAuthorIds = sortedAuthors.slice(20, 40).map(getId)
    expect(body.results.map(getId)).toEqual(expectedAuthorIds)
  })

  test(`When limit is set to 10, returns 10 results per page`, async () => {
    const query = stringify({ page: 1, limit: 10 })
    const { body } = await request(app).get(`/authors?${query}`)
    expect(body.page).toEqual(1)
    expect(body.totalCount).toEqual(authorCount)
    expect(body.count).toEqual(10)
    expect(body.totalPages).toEqual(Math.ceil(authorCount / 10))
  })
})
