modify gist handler for testing

pull/1/head
Grandschtroumpf 6 years ago committed by yann300
parent eaba9b7165
commit f8c7308cb6
  1. 108
      test/gist-handler-test.js

@ -1,70 +1,52 @@
'use strict' 'use strict'
var modalDialogCustom
var test = require('tape') if (typeof window !== 'undefined') {
modalDialogCustom = require('../app/ui/modal-dialog-custom')
var GistHandler = require('../src/lib/gist-handler')
test('gistHandler.handleLoad with no gist param', function (t) {
t.plan(1)
var gistHandler = new GistHandler({})
var params = {}
var result = gistHandler.handleLoad(params, null)
t.equal(result, false)
})
test('gistHandler.handleLoad with blank gist param, and invalid user input', function (t) {
t.plan(3)
var fakeWindow = {prompt: function (title, message, input, cb) {
t.ok(message)
t.ok(message.match(/gist/i))
cb('invalid')
}}
var gistHandler = new GistHandler(fakeWindow)
var params = {'gist': ''}
var result = gistHandler.handleLoad(params, null)
t.equal(result, true)
})
test('gistHandler.handleLoad with blank gist param, and valid user input', function (t) {
t.plan(4)
var fakeWindow = {prompt: function (title, message, input, cb) {
t.ok(message)
t.ok(message.match(/gist/i))
cb('Beef1234')
}}
var cb = function (gistId) {
t.equal(gistId, 'Beef1234')
} }
// ^ this class can be load in a non browser context when running node unit testing.
// should not load UI in that case
var gistHandler = new GistHandler(fakeWindow) // Allowing window to be overriden for testing
function GistHandler (_window) {
var params = {'gist': ''} if (_window !== undefined) {
var result = gistHandler.handleLoad(params, cb) modalDialogCustom = _window
}
t.equal(result, true)
})
test('gistHandler.handleLoad with gist param', function (t) {
t.plan(2)
var gistHandler = new GistHandler({})
var params = {'gist': 'abc'}
var cb = function (gistId) { this.handleLoad = function (params, cb) {
t.equal(gistId, 'abc') if (!cb) cb = () => {}
var loadingFromGist = false
var gistId
if (params['gist'] === '') {
loadingFromGist = true
modalDialogCustom.prompt(
null,
'Enter the URL or ID of the Gist you would like to load.',
null,
target => {
if (target !== '') {
gistId = getGistId(target)
if (gistId) {
cb(gistId)
}
}
}
)
return loadingFromGist
} else {
gistId = params['gist']
loadingFromGist = !!gistId
}
if (loadingFromGist) {
cb(gistId)
}
return loadingFromGist
} }
var result = gistHandler.handleLoad(params, cb) function getGistId (str) {
var idr = /[0-9A-Fa-f]{8,}/
var match = idr.exec(str)
return match ? match[0] : null
}
}
t.equal(result, true) module.exports = GistHandler
})

Loading…
Cancel
Save