Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Illies committed Mar 17, 2017
1 parent 439c821 commit 7d9d8c5
Show file tree
Hide file tree
Showing 4 changed files with 522 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = (grunt) ->
'test/fcsaNumber.spec.js': 'test/fcsaNumber.spec.coffee'
'test/fcsaNumberConfig.spec.js': 'test/fcsaNumberConfig.spec.coffee'
'test/fcsaNumberConfigExt.spec.js': 'test/fcsaNumberConfigExt.spec.coffee'
'test/fcsaNumberExt.spec.js': 'test/fcsaNumberExt.spec.coffee'
'e2e/fcsaNumber.e2e.js': 'e2e/fcsaNumber.e2e.coffee'
pkg: grunt.file.readJSON('package.json')
uglify:
Expand Down Expand Up @@ -47,6 +48,7 @@ module.exports = (grunt) ->
'test/fcsaNumber.spec.coffee'
'test/fcsaNumberConfig.spec.coffee'
'test/fcsaNumberConfigExt.spec.coffee'
'test/fcsaNumberExt.spec.coffee'
'e2e/fcsaNumber.e2e.coffee'
]
tasks: 'default'
Expand All @@ -56,6 +58,7 @@ module.exports = (grunt) ->
'test/fcsaNumber.spec.js'
'test/fcsaNumberConfig.spec.js'
'test/fcsaNumberConfigExt.spec.js'
'test/fcsaNumberExt.spec.js'
]
tasks: ['karma:unit:run']
karma:
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = function(config) {
'src/fcsaNumber.js',
'test/fcsaNumber.spec.js',
'test/fcsaNumberConfig.spec.js',
'test/fcsaNumberExt.spec.js',
'test/fcsaNumberConfigExt.spec.js'
],

Expand Down
225 changes: 225 additions & 0 deletions test/fcsaNumberExt.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
describe 'fcsaNumber', ->
form = undefined
$scope = undefined
$compile = undefined

beforeEach ->
testModule = angular.module 'testModule', []
testModule.config (fcsaNumberConfigProvider) ->
fcsaNumberConfigProvider.setDefaultOptions {
minDecimals: 2
decimalSeparator: '#'
thousandSeparator: '_'
}

module 'fcsa-number', 'testModule'

inject(($rootScope, _$compile_) ->
$scope = $rootScope
$compile = _$compile_
$scope.model = { number: 0 }
)

compileForm = (options = '{}') ->
$compile("<form name='form'><input type='text' name='number' ng-model='model.number' fcsa-number='#{options}' /></form>")($scope)
$scope.$digest()
$scope.form

isValid = (args) ->
args.options ||= '{}'
$compile("<form name='form'><input type='text' name='number' ng-model='model.number' fcsa-number='#{args.options}' /></form>")($scope)
$scope.$digest()
$scope.form.number.$setViewValue args.val
$scope.form.number.$valid

describe 'on focus', ->
it 'removes the commas', ->
$scope.model.number = 1000
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
expect(el.value).toBe '1000#00'

describe 'on blur', ->
it 'adds commas', ->
$scope.model.number = 1000
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
angular.element(el).triggerHandler 'blur'
expect(el.value).toBe '1_000#00'

describe 'with negative decimal number', ->
it 'correctly formats it', ->
$scope.model.number = -1000.2
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
angular.element(el).triggerHandler 'blur'
expect(el.value).toBe '-1_000#20'

describe 'when more than 3 decimals', ->
it 'does not add commas to the decimals', ->
$scope.model.number = 1234.5678
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
angular.element(el).triggerHandler 'blur'
expect(el.value).toBe '1_234#5678'

describe 'no options', ->
it 'validates positive number', ->
valid = isValid
val: '1'
expect(valid).toBe true

it 'validates positive number with commas', ->
valid = isValid
val: '1_23'
expect(valid).toBe true

it 'validates negative number', ->
valid = isValid
val: '-1'
expect(valid).toBe true

it 'invalidates hyphen only', ->
valid = isValid
val: '-'
expect(valid).toBe false

it 'validates number with decimals', ->
valid = isValid
val: '1#1'
expect(valid).toBe true

it 'validates number with decimals and commas', ->
valid = isValid
val: '1_123_142#1'
expect(valid).toBe true

it 'validates number while ignoring extra commas', ->
valid = isValid
val: '1_1_23_1_4_2#1'
expect(valid).toBe true

it 'invalidates number with multiple decimals', ->
valid = isValid
val: '1#1#2'
expect(valid).toBe false

it 'invalidates non number', ->
valid = isValid
val: '1a'
expect(valid).toBe false

describe 'options', ->
describe 'max', ->
it 'validates numbers below or equal to max', ->
valid = isValid
options: '{ max: 100 }'
val: '100'
expect(valid).toBe true

it 'invalidates numbers above max', ->
valid = isValid
options: '{ max: 100 }'
val: '100#1'
expect(valid).toBe false

describe 'min', ->
it 'validates numbers not below the min', ->
valid = isValid
options: '{ min: 0 }'
val: '0'
expect(valid).toBe true

it 'invalidates numbers below the min', ->
valid = isValid
options: '{ min: 0 }'
val: '-0#1'
expect(valid).toBe false

describe 'maxDigits', ->
it 'validates positive numbers not above number of digits', ->
valid = isValid
options: '{ maxDigits: 2 }'
val: '99'
expect(valid).toBe true

it 'validates positive numbers not above number of digits with decimals', ->
valid = isValid
options: '{ maxDigits: 2 }'
val: '99#99'
expect(valid).toBe true

it 'invalidates positive numbers above number of digits', ->
valid = isValid
options: '{ maxDigits: 2 }'
val: '999'
expect(valid).toBe false

it 'validates negative numbers not above number of digits', ->
valid = isValid
options: '{ maxDigits: 2 }'
val: '-99'
expect(valid).toBe true

it 'validates negative numbers not above number of digits and decimals', ->
valid = isValid
options: '{ maxDigits: 2 }'
val: '-99#99'
expect(valid).toBe true

describe 'maxDecimals', ->
it 'validates numbers without more decimals', ->
valid = isValid
options: '{ maxDecimals: 2 }'
val: '1#23'
expect(valid).toBe true

it 'invalidates numbers with more decimals', ->
valid = isValid
options: '{ maxDecimals: 2 }'
val: '1#234'
expect(valid).toBe false

describe 'prepend', ->
it 'prepends the value', ->
$scope.model.number = 1000
form = compileForm "{ prepend: \"$\" }"
expect(form.number.$viewValue).toBe '$1_000#00'
expect($scope.model.number).toBe 1000

it 'removes the prepend value on focus', ->
$scope.model.number = 1000
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number='{ prepend: \"$\" }' />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
expect(el.value).toBe '1000#00'

describe 'append', ->
it 'appends the value', ->
$scope.model.number = 100
form = compileForm "{ append: \"%\" }"
expect(form.number.$viewValue).toBe '100#00%'
expect($scope.model.number).toBe 100

it 'removes the append value on focus', ->
$scope.model.number = 100
el = $compile("<input type='text' name='number' ng-model='model.number' fcsa-number='{ append: \"%\" }' />")($scope)
el = el[0]
$scope.$digest()
angular.element(document.body).append el
angular.element(el).triggerHandler 'focus'
expect(el.value).toBe '100#00'
Loading

0 comments on commit 7d9d8c5

Please sign in to comment.