
Utils = require('../src/utils')
moment = require 'moment'

describe 'Age Format', ->
  it 'should return age from datestring when months are not included', ->

    # need these variables since formatAge uses today's year to format age
    displayDate = '1910-12-31'
    displayYear = 2016
    displayAgeForYear2016 = 105
    
    now  = moment().utc()
    
    if now.diff(displayYear) then displayAgeForYear2016 + now.diff(displayYear)

    Utils.formatAge(displayDate).should.equal(displayAgeForYear2016 + ' YO')

describe 'NHS ID number', ->

  it 'should be formatted with hyphens', ->
    Utils.formatNHS('1515151522').should.equal('151-515-1522')


describe 'Phone Number Format', ->
  
  it 'should create a phone number with (xxx) xxx-xxxx', ->

    Utils.formatPhoneNumber('5553334444').should.equal('(555) 333-4444')

describe 'Source Format', ->

  it 'should create a string: Org - Facility - System', ->
    input = 
      orgName: 'Org' 
      facilityName: 'Facility'
      systemName: 'System'

    Utils.formatSource(input).should.equal('Org - Facility - System')


  it 'should omit hyphens and spaces for a facility that is an empty string', ->
    input = 
      orgName: 'Org' 
      facilityName: ''
      systemName: 'System'

    Utils.formatSource(input).should.equal('Org - System')

  it 'should omit hyphens and spaces for a system that is an empty string', ->
    input = 
      orgName: 'Org' 
      facilityName: 'Facility'
      systemName: ''

    Utils.formatSource(input).should.equal('Org - Facility')

  it 'should omit hyphens and spaces for a facility that is null/undefined', ->
    input = 
      orgName: 'Org' 
      systemName: 'System'

    Utils.formatSource(input).should.equal('Org - System')

  it 'should omit hyphens and spaces for a system that is null/undefined', ->
    input = 
      orgName: 'Org' 
      facilityName: 'Facility'


    Utils.formatSource(input).should.equal('Org - Facility')

  it 'should omit leading hyphen and spaces for an org that is null/undefined', ->
    input = 
      facilityName: 'Facility'
      systemName: 'System'

    Utils.formatSource(input).should.equal('Facility - System')

describe 'Reduced File Name Format', ->

  it 'should return the whole filename if filename length is less than target length', ->

    targetLength = 3

    Utils.formatFileName('to', targetLength).should.equal('to')

  it 'should return reduced filename with if filename length is equal to target length', ->

    targetLength = 10

    Utils.formatFileName('1234567890', targetLength).should.equal('1234567890')

  it 'should return reduced filename with 4 characters from the end of the filename', ->
    # target length is 10 and file name length is 14
    targetLength = 10

    Utils.formatFileName('12345678901234', targetLength).should.equal('123…1234')

  it 'should return reduced filename with 6 characters from the end of the filename', ->
    # target length is 19 and file name length is 20
    targetLength = 19

    Utils.formatFileName('12345678901234567890', targetLength).should.equal('1234567890…567890')




describe 'File Ext Parse', ->

  it 'should return the extension from a file name when extension exists', ->

    Utils.parseFileExtension('filename.ext').should.equal('ext')

  it 'should return the empty string from a file name when no extension exists', ->

    Utils.parseFileExtension('filename').should.equal('')

describe 'Match object Ids', ->

  obj1 = {id:1}
  obj2 = {id:2}
  obj3 = {guid: 3}
  obj4 = {guid: 4}
  obj5 = {id:1, guid:3}
  obj6 = {id:2, guid:3}

  it 'should return the false if objects have different ids', ->

    matchTest = Utils.idsMatch(obj1, obj2)

    expect(matchTest).to.equal(false)

  it 'should return the true if objects have same ids', ->

    matchTest = Utils.idsMatch(obj2, obj2)

    expect(matchTest).to.equal(true)

  it 'should return the false if objects have different guids', ->

    matchTest = Utils.idsMatch(obj3, obj4)

    expect(matchTest).to.equal(false)

  it 'should return the true if objects have same guids', ->

    matchTest = Utils.idsMatch(obj4, obj4)

    expect(matchTest).to.equal(true)

  it 'should return the false if objects have different ids and same guids', ->

    matchTest = Utils.idsMatch(obj5, obj6)

    expect(matchTest).to.equal(false)

describe 'Is Number', ->

  it 'should return the false if not number', ->

    numberTest = Utils.isNumber('string')

    expect(numberTest).to.equal(false)

  it 'should return the true if number', ->

    numberTest = Utils.isNumber(44)

    expect(numberTest).to.equal(true)

  it 'should return the true if number is wrapped in quotes', ->

    numberTest = Utils.isNumber('44.22222222')

    expect(numberTest).to.equal(true)

describe 'Measure DOM Property', ->
  
  it 'should return the request DOM property of the passed element', ->
    measurer = document.createElement 'div'
    measurer.id = 'measurer'
    document.body.appendChild measurer

    el = document.createElement 'span'
    el.innerText = 'some text to make this el have width'

    width = Utils.measureDOMProp el, 'offsetWidth'

    document.body.removeChild measurer

    expect(width).to.not.equal(0)




