codecombat/app/views/TestView.coffee

124 lines
3.4 KiB
CoffeeScript
Raw Normal View History

RootView = require 'views/core/RootView'
2014-12-12 14:36:41 -05:00
template = require 'templates/test-view'
requireUtils = require 'lib/requireUtils'
2016-01-26 14:02:11 -05:00
storage = require 'core/storage'
require 'vendor/jasmine-bundle'
require 'tests'
TEST_REQUIRE_PREFIX = 'test/app/'
TEST_URL_PREFIX = '/test/'
module.exports = TestView = class TestView extends RootView
2014-06-30 22:16:26 -04:00
id: 'test-view'
template: template
reloadOnClose: true
2016-06-03 19:26:03 -04:00
className: 'style-flat'
2016-01-26 14:02:11 -05:00
events:
'click #show-demos-btn': 'onClickShowDemosButton'
'click #hide-demos-btn': 'onClickHideDemosButton'
2014-06-30 22:16:26 -04:00
# INITIALIZE
2016-01-26 14:02:11 -05:00
initialize: (options, @subPath='') ->
@subPath = @subPath[1..] if @subPath[0] is '/'
2016-01-26 14:02:11 -05:00
@demosOn = storage.load('demos-on')
2016-06-03 19:26:03 -04:00
@failureReports = []
@loadedFileIDs = []
2016-01-26 14:02:11 -05:00
afterInsert: ->
@initSpecFiles()
@render()
2016-06-03 19:26:03 -04:00
TestView.runTests(@specFiles, @demosOn, @)
window.runJasmine()
2016-01-26 14:02:11 -05:00
# EVENTS
onClickShowDemosButton: ->
storage.save('demos-on', true)
document.location.reload()
onClickHideDemosButton: ->
storage.remove('demos-on')
document.location.reload()
2014-06-30 22:16:26 -04:00
# RENDER DATA
2014-06-30 22:16:26 -04:00
getRenderData: ->
c = super(arguments...)
c.parentFolders = requireUtils.getParentFolders(@subPath, TEST_URL_PREFIX)
c.children = requireUtils.parseImmediateChildren(@specFiles, @subPath, TEST_REQUIRE_PREFIX, TEST_URL_PREFIX)
parts = @subPath.split('/')
c.currentFolder = parts[parts.length-1] or parts[parts.length-2] or 'All'
c
2014-06-30 22:16:26 -04:00
# RUNNING TESTS
2014-06-30 22:16:26 -04:00
initSpecFiles: ->
2014-06-07 21:15:57 -04:00
@specFiles = TestView.getAllSpecFiles()
if @subPath
prefix = TEST_REQUIRE_PREFIX + @subPath
@specFiles = (f for f in @specFiles when _.string.startsWith f, prefix)
2016-06-03 19:26:03 -04:00
@runTests: (specFiles, demosOn=false, view) ->
jasmine.getEnv().addReporter({
suiteStack: []
specDone: (result) ->
if result.status is 'failed'
console.log 'result', result
report = {
suiteDescriptions: _.clone(@suiteStack)
failMessages: (fe.message for fe in result.failedExpectations)
testDescription: result.description
}
view.failureReports.push(report)
view.renderSelectors('#failure-reports')
suiteStarted: (result) ->
@suiteStack.push(result.description)
suiteDone: (result) ->
@suiteStack.pop()
})
application.testing = true
2014-06-07 21:15:57 -04:00
specFiles ?= @getAllSpecFiles()
2016-01-26 14:02:11 -05:00
if demosOn
2016-05-26 20:39:56 -04:00
jasmine.demoEl = _.once ($el) ->
2016-01-26 14:02:11 -05:00
$('#demo-area').append($el)
jasmine.demoModal = _.once (modal) ->
currentView.openModalView(modal)
else
jasmine.demoEl = _.noop
jasmine.demoModal = _.noop
2016-06-03 19:26:03 -04:00
jasmine.Ajax.install()
beforeEach ->
jasmine.Ajax.requests.reset()
Backbone.Mediator.init()
Backbone.Mediator.setValidationEnabled false
spyOn(application.tracker, 'trackEvent')
# TODO Stubbify more things
# * document.location
# * firebase
# * all the services that load in main.html
afterEach ->
# TODO Clean up more things
# * Events
require f for f in specFiles # runs the tests
2014-06-07 21:15:57 -04:00
@getAllSpecFiles = ->
allFiles = window.require.list()
(f for f in allFiles when f.indexOf('.spec') > -1)
destroy: ->
# hack to get jasmine tests to properly run again on clicking links, and make sure if you
# leave this page (say, back to the main site) that test stuff doesn't follow you.
2014-06-30 22:16:26 -04:00
document.location.reload()