Permalink
Browse files

Refactored ui classes and their tests so that now they can be tested …

…wo the use of sandboxed-module.
  • Loading branch information...
1 parent bb968d3 commit fa112b96abd3aa78ab383d15f02cf659469ebeb8 @airportyh airportyh committed Mar 16, 2013
View
@@ -156,10 +156,12 @@ App.prototype = {
this.emit('exit')
this.cleanUpLaunchers(function(){
self.runExitHook(function(){
- self.view.cleanup(function(){
+ if (self.view) self.view.cleanup(die)
+ else die()
+ function die(){
if (err) console.error(err.stack)
process.exit(code)
- })
+ }
})
})
}
View
@@ -13,7 +13,7 @@ var constants = require('./constants')
var RunnerTab = tabs.RunnerTab
var RunnerTabs = tabs.RunnerTabs
var getTermSize = require('../gettermsize')
-var screen = require('./screen')
+var Screen = require('./screen')
var pad = require('../strutils').pad
var log = require('winston')
var ErrorMessagesPanel = require('./error_messages_panel')
@@ -31,9 +31,18 @@ var AppView = module.exports = View.extend({
var app = attrs.app
this.name = 'Testem'
this.app = app
+ if (!this.get('screen')){
+ this.set('screen', Screen())
+ }
+
this.initCharm()
+
+ var screen = this.get('screen')
- var runnerTabs = this.runnerTabs = new RunnerTabs([], {appview: this})
+ var runnerTabs = this.runnerTabs = new RunnerTabs([], {
+ appview: this
+ , screen: screen
+ })
this.set({
runnerTabs: runnerTabs
})
@@ -45,6 +54,7 @@ var AppView = module.exports = View.extend({
runner: runner
, index: idx
, appview: self
+ , screen: screen
})
runnerTabs.push(tab)
})
@@ -68,14 +78,15 @@ var AppView = module.exports = View.extend({
this.errorMessagesPanel = new ErrorMessagesPanel({
appview: this
, text: ''
+ , screen: screen
})
this.errorMessagesPanel.on('change:text', function(m, text){
self.set('isPopupVisible', !!text)
})
this.startMonitorTermSize()
}
, initCharm: function(){
-
+ var screen = this.get('screen')
screen.reset()
screen.erase('screen')
screen.cursor(false)
@@ -88,9 +99,11 @@ var AppView = module.exports = View.extend({
}
, startMonitorTermSize: function(){
var self = this
+ var screen = self.get('screen')
;(function checkTermSize(){
getTermSize(function(cols, lines){
+
if (cols !== self.get('cols') || lines !== self.get('lines')){
screen.enableScroll(constants.LogPanelUnusedLines, lines - 1)
self.set({
@@ -121,6 +134,7 @@ var AppView = module.exports = View.extend({
, renderTop: function(){
if (this.isPopupVisible()) return
+ var screen = this.get('screen')
var url = 'http://localhost:' + this.app.config.get('port')
var cols = this.get('cols')
screen
@@ -140,6 +154,7 @@ var AppView = module.exports = View.extend({
if (this.isPopupVisible()) return
if (this.runners.length > 0) return
+ var screen = this.get('screen')
var lines = this.get('lines')
var cols = this.get('cols')
var textLineIdx = Math.floor(lines / 2 + 2)
@@ -153,6 +168,7 @@ var AppView = module.exports = View.extend({
, renderBottom: function(){
if (this.isPopupVisible()) return
+ var screen = this.get('screen')
var cols = this.get('cols')
var msg = (
!this.get('atLeastOneRunner') ?
@@ -233,6 +249,7 @@ var AppView = module.exports = View.extend({
}
, cleanup: function(cb){
+ var screen = this.get('screen')
screen.display('reset')
screen.erase('screen')
screen.position(0, 0)
@@ -1,7 +1,6 @@
var ScrollableTextPanel = require('./scrollable_text_panel')
var log = require('winston')
var Chars = require('../chars')
-var screen = require('./screen')
var ErrorMessagesPanel = module.exports = ScrollableTextPanel.extend({
initialize: function(attrs){
@@ -27,6 +26,7 @@ var ErrorMessagesPanel = module.exports = ScrollableTextPanel.extend({
var width = this.get('width') + 1
var height = this.get('height') + 1
+ var screen = this.get('screen')
screen.foreground('red')
screen.position(col, line)
screen.write(Chars.topLeft + Array(width).join(Chars.horizontal) + Chars.topRight)
View
@@ -15,7 +15,7 @@ var pad = require('../strutils').pad
var log = require('winston')
var Chars = require('../chars')
var assert = require('assert')
-var screen = require('./screen')
+var Screen = require('./screen')
var growl = require('growl')
var constants = require('./constants')
var TabWidth = constants.TabWidth
@@ -33,18 +33,22 @@ var RunnerTab = exports.RunnerTab = View.extend({
, width: TabWidth
, initialize: function(){
var runner = this.get('runner')
- , results = runner.get('results')
- , index = this.get('index')
- , appview = this.get('appview')
- , app = appview.app
- , config = app.config
- , self = this
-
+ var results = runner.get('results')
+ var index = this.get('index')
+ var appview = this.get('appview')
+ var app = appview.app
+ var config = app.config
+ var self = this
var visible = appview.get('currentTab') === index
+ if (!this.get('screen')){
+ this.set('screen', Screen())
+ }
+
this.splitPanel = new SplitLogPanel({
runner: runner
, appview: appview
, visible: visible
+ , screen: this.get('screen')
})
this.spinnerIdx = 0
@@ -158,6 +162,7 @@ var RunnerTab = exports.RunnerTab = View.extend({
, renderRunnerName: function(){
if (this.isPopupVisible()) return
+ var screen = this.get('screen')
var index = this.get('index')
var line = this.line
var width = this.width
@@ -180,6 +185,7 @@ var RunnerTab = exports.RunnerTab = View.extend({
, renderResults: function(){
if (this.isPopupVisible()) return
+ var screen = this.get('screen')
var index = this.get('index')
var line = this.line
var width = this.width
@@ -228,6 +234,8 @@ var RunnerTab = exports.RunnerTab = View.extend({
}
, renderUnselected: function(){
if (this.isPopupVisible()) return
+
+ var screen = this.get('screen')
var index = this.get('index')
var width = this.width
var height = this.height
@@ -252,6 +260,7 @@ var RunnerTab = exports.RunnerTab = View.extend({
}
, renderSelected: function(){
if (this.isPopupVisible()) return
+ var screen = this.get('screen')
var index = this.get('index')
var width = this.width
var height = this.height
@@ -291,6 +300,7 @@ var RunnerTabs = exports.RunnerTabs = Backbone.Collection.extend({
, initialize: function(arr, attrs){
this.appview = attrs.appview
var self = this
+ this.screen = attrs.screen || Screen()
this.appview.runners().on('remove', function(removed, runners, options){
var idx = options.index
var tab = self.at(idx)
@@ -323,7 +333,7 @@ var RunnerTabs = exports.RunnerTabs = Backbone.Collection.extend({
}
, blankOutBackground: function(){
if (this.isPopupVisible()) return
-
+ var screen = this.screen
var cols = this.appview.get('cols')
for (var i = 0; i < TabHeight; i++){
screen
@@ -339,6 +349,7 @@ var RunnerTabs = exports.RunnerTabs = Backbone.Collection.extend({
}
, renderLine: function(){
if (this.isPopupVisible()) return
+ var screen = this.screen
var startCol = this.length * TabWidth
var lineLength = this.appview.get('cols') - startCol + 1
if (lineLength > 0){
@@ -349,6 +360,7 @@ var RunnerTabs = exports.RunnerTabs = Backbone.Collection.extend({
}
, eraseLast: function(){
if (this.isPopupVisible()) return
+ var screen = this.screen
var index = this.length
var width = TabWidth
var height = TabHeight
View
@@ -1,9 +1,13 @@
var getTermSize = require('../gettermsize')
+var charm
+
function initCharm(){
+
+ if (charm) return charm
// A wrapper around charm (gives the same API) that automatically parks the cursor
// to the bottom right corner when not in use
- var charm = function(charm){
+ charm = function(charm){
var timeoutID
function parkCursor(){
getTermSize(function(cols, lines){
@@ -40,4 +44,4 @@ function initCharm(){
}
require('../patchcharm.js')
-module.exports = initCharm()
+module.exports = initCharm
@@ -1,7 +1,7 @@
var View = require('./view')
var log = require('winston')
var splitLines = require('../strutils').splitLines
-var screen = require('./screen')
+var Screen = require('./screen')
// This is a generic scrollable text viewer widget. Should be refactored
// out to another file or npm module at some point.
@@ -19,6 +19,9 @@ var ScrollableTextPanel = module.exports = View.extend({
// * height and width
, initialize: function(){
var self = this
+ if (!this.get('screen')){
+ this.set('screen', Screen())
+ }
this.updateTextLines()
this.observe(this, 'change:text change:width', function(model, text){
self.updateTextLines()
@@ -99,7 +102,8 @@ var ScrollableTextPanel = module.exports = View.extend({
}
, render: function(firstOrLast){
if (!this.get('visible')) return
-
+
+ var screen = this.get('screen')
var startLine = this.get('line')
var col = this.get('col')
var width = this.get('width')
@@ -5,7 +5,7 @@ var tabs = require('./constants')
var StyledString = require('styled_string')
var Chars = require('../chars')
var indent = require('../strutils').indent
-
+var Screen = require('./screen')
function failureDisplay(item){
var extra = ''
@@ -51,20 +51,26 @@ var SplitLogPanel = module.exports = View.extend({
, focus: 'top'
}
, initialize: function(attrs){
+ if (!this.get('screen')){
+ this.set('screen', Screen())
+ }
var runner = this.get('runner')
var results = runner.get('results')
var messages = runner.get('messages')
var appview = this.get('appview')
var visible = this.get('visible')
var self = this
+ var screen = this.get('screen')
var topPanel = this.topPanel = new ScrollableTextPanel({
line: tabs.TabStartLine + tabs.TabHeight - 1
, col: 0
, visible: visible
+ , screen: screen
})
var bottomPanel = this.bottomPanel = new ScrollableTextPanel({
col: 0
, visible: visible
+ , screen: screen
})
this.observe(appview, 'change:cols change:lines', function(){
self.syncDimensions()
View
@@ -65,8 +65,7 @@
"sinon-chai": "*",
"bufferstream": "*",
"request": "~2.12.0",
- "jsdom": "~0.5.2",
- "sandboxed-module": "~0.2.0"
+ "jsdom": "~0.5.2"
},
"bin": {
"testem": "./testem.js"
View
@@ -12,6 +12,9 @@ launchers:
command: mocha server_tests.js -R tap
protocol: tap
cwd: tests
+ UI:
+ command: mocha tests/ui/error_messages_panel_tests.js -R tap
+ protocol: tap
launch_in_dev:
- All
launch_in_ci:
View
@@ -0,0 +1,20 @@
+var AppView = require('../../lib/ui/appview')
+var Backbone = require('backbone')
+var screen = require('./fake_screen')
+
+describe('AppView', function(){
+
+ it('initializes', function(){
+ var app = new Backbone.Model
+ app.config = {get: function(){ return 1234 }}
+ app.runners = new Backbone.Collection
+ var appview = new AppView({
+ app: app
+ , screen: screen
+ })
+ appview.renderTop()
+ appview.renderMiddle()
+ appview.renderBottom()
+ })
+
+})
@@ -1,18 +1,6 @@
-var libDir = '../../lib/'
-var sandbox = require('sandboxed-module')
var expect = require('chai').expect
var screen = require('./fake_screen')
-var ScrollableTextPanel = sandbox.require(libDir + 'ui/scrollable_text_panel', {
- requires: {
- './screen': screen
- }
-})
-var ErrorMessagesPanel = sandbox.require(libDir + 'ui/error_messages_panel', {
- requires: {
- './screen': screen
- , './scrollable_text_panel': ScrollableTextPanel
- }
-})
+var ErrorMessagesPanel = require('../../lib/ui/error_messages_panel')
describe('ErrorMessagesPanel', function(){
var panel
@@ -24,6 +12,7 @@ describe('ErrorMessagesPanel', function(){
, width: 10
, height: 10
, text: 'blah'
+ , screen: screen
})
})
it('initializes', function(){})
Oops, something went wrong.

0 comments on commit fa112b9

Please sign in to comment.