Skip to content

Instantly share code, notes, and snippets.

@dimified
Last active April 18, 2016 22:39
Show Gist options
  • Save dimified/8b7a6ed348e9e265bbb67793c2b62d05 to your computer and use it in GitHub Desktop.
Save dimified/8b7a6ed348e9e265bbb67793c2b62d05 to your computer and use it in GitHub Desktop.
Unit Tests
### global Foundation:false ###
'use strict'
define [
'jquery'
'app'
'angular'
'angular.mocks'
'services'
], ($, App, Angular) ->
Angular.module 'ecoCalcLight', [
'App.services'
]
beforeEach(module('ecoCalcLight'))
describe 'App', () ->
# Jasmine Matchers
#
# toBe
# toEqual
# toMatch
# toBeDefined
# toBeUndefined
# toBeNull
# toBeTruthy
# toBeFalsy
# toContain
# toBeLessThan
# toBeGreaterThan
# toBeCloseTo
# toThrow
# Testing: PhantomJS is incapable to properly resolving the namespace of
# Foundation. We need to set the global namespace to an empty string to
# prevent failing tests in Karma
# http://foundation.zurb.com/forum/posts/16493-unrecognized-expression-data-times-new-roman-abide-in-karma-tests
Foundation.global.namespace = ''
it 'was initialized', () ->
app = App.init()
expect(typeof app).toEqual('object')
return
return # app
describe 'Kongcat', () ->
kongcat = {}
beforeEach (
inject ($injector) ->
kongcat = $injector.get('kongcat')
return
)
beforeEach () ->
localStorage.clear()
return
describe 'should write and update', () ->
it 'String in localStorage', () ->
kongcat.write('string', 'string')
expect(localStorage.string).toEqual('"string"')
kongcat.update('string', 'String')
expect(localStorage.string).toEqual('"String"')
return
it 'Number in localStorage', () ->
kongcat.write('number', 100)
expect(localStorage.number).toEqual('100')
kongcat.update('number', 101)
expect(localStorage.number).toEqual('101')
return
it 'Array with numbers and strings in localStorage', () ->
kongcat.write('array', [0, 10, 20])
expect(localStorage.array).toEqual('[0,10,20]')
kongcat.update('array', 30, 0)
expect(localStorage.array).toEqual('[30,10,20]')
kongcat.update('array', 'string', 1)
expect(localStorage.array).toEqual('[30,"string",20]')
return
it 'Object in localStorage', () ->
kongcat.write('object', {"pos": 100})
expect(localStorage.object).toEqual('{"pos":100}')
kongcat.update('object', 200, 'pos')
expect(localStorage.object).toEqual('{"pos":200}')
return
return
describe 'should read', () ->
it 'String of localStorage', () ->
kongcat.write('string', 'string')
expect(kongcat.read('string')).toEqual('string')
return
it 'Number of localStorage', () ->
kongcat.write('number', 100)
expect(kongcat.read('number')).toEqual(100)
return
it 'Array in localStorage', () ->
kongcat.write('array', [0, 10, 20])
expect(kongcat.read('array')).toEqual([0, 10, 20])
return
it 'Object in localStorage', () ->
kongcat.write('object', {"pos": "100"})
expect(kongcat.read('object')).toEqual({"pos": "100"})
return
return
it 'erase value in localStorage', () ->
kongcat.write('string', 'string')
expect(kongcat.erase('string')).toEqual('')
return
it 'remove value in localStorage', () ->
kongcat.write('string', 'string')
kongcat.write('number', 100)
kongcat.clear('number')
expect(localStorage.string).toBeDefined
expect(localStorage.number).toBeUndefined
return
it 'clears all values in localStorage', () ->
kongcat.write('array', [0, 10, 20])
kongcat.write('object', {"pos": "100"})
kongcat.clear()
expect(localStorage.array).toBeUndefined
expect(localStorage.object).toBeUndefined
return
it 'pushes value to item in localStorage', () ->
expect(localStorage.array).toBeUndefined
kongcat.push('array', 10)
kongcat.push('array', 20)
expect(localStorage.array).toEqual('[10,20]')
return
return # kongcat
describe 'Store service', () ->
storeSrvc = {}
beforeEach (
inject ($injector) ->
storeSrvc = $injector.get('storeSrvc')
return
)
xit 'which should start with initial value', () ->
mockStoreProvider = {
seed: () ->
returnValue = [
{ isActive: false, isSelectable: true }
{ isActive: false, isSelectable: false }
{ isActive: false, isSelectable: false }
]
return {
success: (fn) ->
fn(returnValue)
return
}
}
spyOn(mockStoreProvider, 'seed').andCallThrough()
storeSrvc.seed()
expect(mockStoreProvider.seed).toHaveBeenCalled()
storeSrvc.seed()
console.log
expect(currentSolution.get()).toEqual(0)
return
it 'which should be incremented', () ->
expect(currentSolution.inc()).toEqual(1)
return
it 'which should be decremented', () ->
expect(currentSolution.dec()).toEqual(0)
return
return # services
describe 'Burning hours service', () ->
burningHours = {}
beforeEach (
inject ($injector) ->
burningHours = $injector.get('burningHours')
return
)
it 'should convert time string into minutes', () ->
expect(burningHours.calculateMinutes('00:00')).toEqual(0)
expect(burningHours.calculateMinutes('12:50')).toEqual(770)
expect(burningHours.calculateMinutes('24:00')).toEqual(1440)
return
it 'should convert minutes to time string', () ->
expect(burningHours.getTimeString(0)).toEqual('00:00')
expect(burningHours.getTimeString(770)).toEqual('12:50')
expect(burningHours.getTimeString(1440)).toEqual('24:00')
return
it 'should convert minutes to hours', () ->
expect(burningHours.convertHours(0)).toEqual(0)
expect(burningHours.convertHours(525600)).toEqual(8760)
return
it 'should convert hours to minutes', () ->
expect(burningHours.convertMinutes(0)).toEqual(0)
expect(burningHours.convertMinutes(8760)).toEqual(525600)
return
# Use Case 00: Default use case (default values in ecoCALC light projects)
# Start time: 07:00
# End time: 20:00
# Working days: 7
it 'should simulate the default use case: UC00', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 7
burningHours: 4745
result = burningHours.calculateBurningHours(obj)
expect(burningHours.calculateBurningHours(obj).startTime).toEqual('07:00')
expect(burningHours.calculateBurningHours(obj).endTime).toEqual('20:00')
expect(burningHours.calculateBurningHours(obj).workingDays).toEqual(7)
expect(burningHours.calculateBurningHours(obj).burningHours).toEqual(4745)
return
# Use Case 01: Change of burning hours value #1
it 'should simulate the change of burning hours: UC01', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 7
burningHours: 8760
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(7)
expect(result.burningHours).toEqual(8760)
return
# Use Case 02: Change of burning hours value #2
it 'should simulate the change of burning hours: UC02', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 7
burningHours: 8757
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(7)
expect(result.burningHours).toEqual(8760)
return
# Use Case 03: Change of burning hours value #3
it 'should simulate the change of burning hours: UC03', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 5
burningHours: 8757
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(7)
expect(result.burningHours).toEqual(8760)
return
# Use Case 04: Change of burning hours value #4
it 'should simulate the change of burning hours: UC04', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 5
burningHours: 9000
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(7)
expect(result.burningHours).toEqual(8760)
return
# Use Case 05: Change of burning hours value #5
it 'should simulate the change of burning hours: UC05', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 5
burningHours: 3000
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('07:00')
expect(result.endTime).toEqual('18:30')
expect(result.workingDays).toEqual(5)
expect(result.burningHours).toEqual(2998.214285714286)
return
# Use Case 06: Change of burning hours value #6
it 'should simulate the change of burning hours: UC06', () ->
obj =
startTime: '07:00'
endTime: '18:30'
workingDays: 5
burningHours: 8000
result = burningHours.calculateOperatingTime(obj)
expect(result.startTime).toEqual('07:00')
expect(result.endTime).toEqual('04:55')
expect(result.workingDays).toEqual(7)
expect(result.burningHours).toEqual(7999.583333333333)
return
# Use Case 07: Change of working days
# Start time: 07:00
# End time: 20:00
# Working days: 6
it 'should simulate the change of working days: UC07', () ->
obj =
startTime: '07:00'
endTime: '20:00'
workingDays: 6
result = burningHours.calculateBurningHours(obj)
expect(result.startTime).toEqual('07:00')
expect(result.endTime).toEqual('20:00')
expect(result.workingDays).toEqual(6)
expect(result.burningHours).toEqual(4067.1428571428573)
return
# Use Case 08: Change of operating times #1
# Start time: 08:00
# End time: 20:00
# Working days: 6
it 'should simulate the change of operating times: UC08', () ->
obj =
startTime: '08:00'
endTime: '20:00'
workingDays: 6
result = burningHours.calculateBurningHours(obj)
expect(result.startTime).toEqual('08:00')
expect(result.endTime).toEqual('20:00')
expect(result.workingDays).toEqual(6)
expect(result.burningHours).toEqual(3754.2857142857147)
return
# Use Case 09: Change of operating times #2
# Start time: 08:00
# End time: 07:59
# Working days: 6
it 'should simulate the change of operating times: UC09', () ->
obj =
startTime: '08:00'
endTime: '07:59'
workingDays: 6
result = burningHours.calculateBurningHours(obj)
expect(result.startTime).toEqual('08:00')
expect(result.endTime).toEqual('07:59')
expect(result.workingDays).toEqual(6)
expect(result.burningHours).toEqual(7503.357142857144)
return
# Use Case 10: Change of operating times #3
# Start time: 08:00
# End time: 08:00
# Working days: 6
it 'should simulate the change of operating times: UC10', () ->
obj =
startTime: '08:00'
endTime: '08:00'
workingDays: 6
result = burningHours.calculateBurningHours(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(6)
expect(result.burningHours).toEqual(7508.571428571429)
return
# Use Case 11: Change of operating times #4
# Start time: 20:00
# End time: 20:00
# Working days: 6
it 'should simulate the change of operating times: UC11', () ->
obj =
startTime: '20:00'
endTime: '20:00'
workingDays: 6
result = burningHours.calculateBurningHours(obj)
expect(result.startTime).toEqual('00:00')
expect(result.endTime).toEqual('24:00')
expect(result.workingDays).toEqual(6)
expect(result.burningHours).toEqual(7508.571428571429)
return
return # Burning hours service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment