{"size": 2661, "ext": "js", "max_stars_repo_path": "test/client-test.js", "max_stars_repo_name": "hecto932/tv-maze", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/client-test.js", "max_issues_repo_name": "hecto932/tv-maze", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/client-test.js", "max_forks_repo_name": "hecto932/tv-maze", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1530612245, "max_line_length": 75, "alphanum_fraction": 0.6087936866}
var test = require('tape')
var tvmaze = require('../')
var Client = require('../lib/client')
var nock = require('nock')

var endpoint = 'http://api.tvmaze.test'

test('should create a client', function (t) {
  t.ok(tvmaze.createClient, 'should exist')
  t.equals(typeof tvmaze.createClient, 'function', 'should be a function')
  var client = tvmaze.createClient()
  t.ok(client instanceof Client, 'should be instance of client')
  t.end()
})

test('should fail with unknown endpoint', function (t) {
  var client = tvmaze.createClient({ endpoint: endpoint })

  nock(endpoint)
    .get('/foo')
    .reply(404)

  client._request('/foo', 'GET', null, function (err, body) {
    t.ok(err, 'should failed')
    t.end()
  })
})

test('should fetch a show by id', function (t) {
  var client = tvmaze.createClient({ endpoint: endpoint })
  t.equals(typeof client.show, 'function', 'shouldbe a function')

  nock(endpoint)
    .get('/shows/2473')
    .reply(200, { id: 2473, name: 'Limitless' })

  client.show(2473, function (err, show) {
    t.error(err, 'should not be an error')
    t.ok(show, 'should exist')
    t.equals(show.id, 2473)
    t.equals(show.name, 'Limitless')
    t.end()
  })
})

test('should fail if not query is passed', function (t) {
  var client = tvmaze.createClient({ endpoint: endpoint })

  nock(endpoint)
    .get('/search/shows')
    .reply(400, {
      code: 0,
      message: 'Missing required parameters: q',
      name: 'Bad request',
      status: 400
    })

  client._request('/search/shows', 'GET', null, function (err, res) {
    t.ok(err, 'bad request error')
    t.notOk(res, 'reply should be null')
    t.end()
  })
})

test('should list shows', function (t) {
  var client = tvmaze.createClient({ endpoint: endpoint })

  t.equals(typeof client.shows, 'function', 'should be a function')

  nock(endpoint)
    .get('/shows')
    .reply(200, [])

  client.shows(function (err, shows) {
    t.error(err, 'should be not an error')
    t.ok(Array.isArray(shows), 'should be an array')
    t.end()
  })
})

test('should search shows', function (t) {
  var client = tvmaze.createClient({ endpoint: endpoint })

  t.equals(typeof client.search, 'function', 'should be a function')

  nock(endpoint)
    .get('/search/shows')
    .query({ q: 'limitless' })
    .reply(200, [{ name: 'Limitless' }])

  client.search('limitless', function (err, shows) {
    t.error(err, 'should be an error')
    t.ok(Array.isArray(shows), 'should be an array')
    t.equals(shows[0].name, 'Limitless', 'show retrieve a show name')
    t.end()
  })
})
