Skip to content

Instantly share code, notes, and snippets.

@csahlman
Created March 17, 2014 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csahlman/a8f2507d063f8844d4b4 to your computer and use it in GitHub Desktop.
Save csahlman/a8f2507d063f8844d4b4 to your computer and use it in GitHub Desktop.
jasmine angularjs testing
#= require spec_helper
describe "Lineup Controller", ->
beforeEach ->
directiveTemplate = null
@templateCache.put('/assets/directives/grid/editable_cell.tpl.html', directiveTemplate)
@element = angular.element('<smart-table class="table table-striped" table-title="Smart Table example"
columns="columnCollection"
rows="filteredProjections" config="globalConfig"></smart-table>')
projections = { 'Baseball Hitting': [{
bb: 0.443
double: 0.229
hbp: 0.008
hr: 0.082
isSelected: true
name: "James Loney"
o: 2.867
positions: {"DS":"1B"}
price: {"DS":2000}
r: 0.503
rbi: 0.456
single: 0.711
so: 0.621
triple: 0.022
edited: true
}]}
today = new Date()
savedEdits = [{ name: "James Loney", field: 'o', originalValue: 2.867, updatedValue: 3.00, timePeriod: '2014-03-14'}]
@http.whenGET('calc.json').respond(200, projections)
@http.whenGET('/dashboard/edits', { 'timePeriod': today }).respond(200, savedEdits)
LineUpsCtrl = @controller('LineUpsCtrl', { $scope: @scope, $http: @httpInjector })
@http.flush()
describe "load", ->
it "loads up the projections", ->
expect(@scope.projections.length).toEqual(1)
# it "loads up the previous edits", ->
# expect(@scope.savedEdits.length).toEqual(1)
describe "$scope.undoEdit", ->
beforeEach ->
@scope.projections[0].hr = 1.023
@scope.edits = [{name: "James Loney", field: "hr", originalValue: 0.082, updatedValue: 1.023}]
it "removes the edit from the array", ->
@scope.undoEdit(@scope.edits[0])
expect(@scope.edits.length).toEqual(0)
it "sets edited to false if there is only one edit", ->
expect(@scope.projections[0].edited).toBe(true)
expect(@scope.undoEdit(@scope.edits[0]))
expect(@scope.projections[0].edited).not.toBe(true)
it "leaves edited equaling true if there is more than one edit", ->
@scope.projections[0].o = 3.00
@scope.edits.push { name: "James Loney", field: 'o', originalValue: 2.867, updatedValue: 3.00}
expect(@scope.projections[0].edited).toBe(true)
@scope.undoEdit(@scope.edits[1])
expect(@scope.projections[0].edited).toBe(true)
$scope.setPositionsAndLoadData = (sport) ->
$scope.options.sport = sport
promise = $scope.loadProjections(sport)
$scope.positions = $scope.playerPositions[$scope.options.sport]
$scope.columnCollection = columnDefsReference[sport]
promise
$scope.loadProjections = (playerType) ->
$http
url: 'calc.json'
method: 'GET'
.success (data) ->
$scope.projections = data[playerType]
$scope.filterPlayers($scope.options.selectedPosition)
$scope.loadEdits = ->
$http
url: '/dashboard/edits'
method: 'GET'
params:
'timePeriod': $scope.dt
.success (data) ->
$scope.edits = data
markRowsAsEdited()
# console.log data
.error (data) ->
# console.log 'foo'
$scope.setPositionsAndLoadData($scope.options.sport).then($scope.loadEdits())
#= require lib/jquery
#= require lib/angular
#= require lib/angular-route
#= require dashboard
#= require lib/angular-mocks
#= require sinon
beforeEach(module('fantasy_sports_dashboard'))
beforeEach inject (_$httpBackend_, _$compile_, $rootScope, $controller, $location, $injector, $timeout, $templateCache, $http) ->
@scope = $rootScope.$new()
@http = _$httpBackend_
@compile = _$compile_
@location = $location
@controller = $controller
@injector = $injector
@timeout = $timeout
@templateCache = $templateCache
@httpInjector = $http
@model = (name) =>
@injector.get(name)
@eventLoop =
flush: =>
@scope.$digest()
@sandbox = sinon.sandbox.create()
afterEach ->
@http.resetExpectations()
@http.verifyNoOutstandingExpectation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment