ldx-widgets
Version:
widgets
226 lines (139 loc) • 6.83 kB
text/coffeescript
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 3 3 4 w/ spaces', ->
Utils.formatNHS('1515151522').should.equal('151 515 1522')
it 'should handle various string lengths', ->
Utils.formatNHS('1').should.equal('1')
Utils.formatNHS('12').should.equal('12')
Utils.formatNHS('123').should.equal('123')
Utils.formatNHS('1234').should.equal('123 4')
Utils.formatNHS('12345').should.equal('123 45')
Utils.formatNHS('123456').should.equal('123 456')
Utils.formatNHS('1234567').should.equal('123 456 7')
Utils.formatNHS('12345678').should.equal('123 456 78')
Utils.formatNHS('123456789').should.equal('123 456 789')
Utils.formatNHS('1234567890').should.equal('123 456 7890')
it 'should handle whitespace and strip non digits', ->
Utils.formatNHS('1 234 567 8 9 0').should.equal('123 456 7890')
Utils.formatNHS('123-456-7890').should.equal('123 456 7890')
Utils.formatNHS('A123-456:7890A').should.equal('123 456 7890')
describe 'Phone Number Format', ->
it 'should create a phone number with (xxx) xxx-xxxx for 10 digit numbers', ->
Utils.formatPhoneNumber('(555) 333-4444').should.equal('(555) 333-4444')
Utils.formatPhoneNumber('555-333-4444').should.equal('(555) 333-4444')
Utils.formatPhoneNumber('555.333.4444').should.equal('(555) 333-4444')
Utils.formatPhoneNumber('5553334444').should.equal('(555) 333-4444')
it 'should create a phone number with xxx-xxxx for 7 digit numbers', ->
Utils.formatPhoneNumber('333-4444').should.equal('333-4444')
Utils.formatPhoneNumber('333.4444').should.equal('333-4444')
Utils.formatPhoneNumber('3334444').should.equal('333-4444')
it 'should NOT format number that are not 7 or 10 digits', ->
Utils.formatPhoneNumber('13334444').should.equal('13334444')
Utils.formatPhoneNumber('113334444').should.equal('113334444')
Utils.formatPhoneNumber('334444').should.equal('334444')
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)