#= require angular/payrollhero_api/ph_restangular.coffee

describe 'angular:payrollhero.api.service(EmployeesService)', ->
  subject = undefined
  httpBackend = undefined

  doNotExpectFailure = (error) ->
    console.error(error)
    throw "promise threw unexpected failure!"

  setupBasicEmployeeResponse = ->
    httpBackend.expectGET('http://api.payrollhero.dev/api/v3/employees?page=1&token=t0ken')
    .respond(getJSONFixture('api/v3/employees_response.json'))

  beforeEach ->
     module 'payrollhero.api'
     angular.module('payrollhero.api').constant('PhToken',{token: 't0ken'});

  beforeEach inject (EmployeesService, $httpBackend) ->
   subject = EmployeesService
   httpBackend = $httpBackend
   return

  describe '#getList', ->
    beforeEach ->
      setupBasicEmployeeResponse()
    it 'queries the server and gives me the list with pagination info', ->
      subject.getList(page: 1).then (employeesList) ->
        expect(employeesList.page).toEqual(1)
        expect(employeesList.hasMorePages()).toBeFalsy()
        expect(employeesList.length).toEqual(21)
      .catch(doNotExpectFailure)
      httpBackend.flush()

    it 'populates the methods on employees', ->
      subject.getList(page: 1).then (employeesList) ->
        employee = employeesList[0]
        expect(employee.fullname()).toEqual('Arc Albero')
      .catch(doNotExpectFailure)
      httpBackend.flush()
