From c1aa5105da0373f42359de26a4d872adcfa72eb1 Mon Sep 17 00:00:00 2001 From: Jason Vinson Date: Fri, 7 Oct 2016 12:17:01 -0400 Subject: [PATCH] refactoring for better test coverage --- gulpfile.js | 29 ++++--- src/services.js | 5 ++ test/backblasts_tests.js | 182 ++++++++++++++++++++++++--------------- 3 files changed, 133 insertions(+), 83 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index a3a5037..7017d89 100755 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,4 +1,3 @@ - var shell = require('gulp-shell'); var mocha = require('gulp-mocha'); var watch = require('gulp-watch'); @@ -11,21 +10,23 @@ var cfgRoot = './cfg/'; var tstRoot = './test/'; var buildDir = './build/'; -gulp.task('lint', function(){ - return gulp.src([ - srcRoot + '*.js', - cfgRoot + '*.js']) - .pipe(jshint()) - .pipe(jshint.reporter('jshint-stylish')) - //.pipe(jshint.reporter('fail')); //there is an issue with this reporter: skipping +gulp.task('lint', function() { + return gulp.src([ + srcRoot + '*.js', + cfgRoot + '*.js' + ]) + .pipe(jshint()) + .pipe(jshint.reporter('jshint-stylish')); + //.pipe(jshint.reporter('fail')); //there is an issue with this reporter: skipping }); -gulp.task('build', function(){ - return gulp.src([ - srcRoot + '*.js', - cfgRoot + '*.js']) - - .pipe(gulp.dest(buildDir)); +gulp.task('build', function() { + return gulp.src([ + srcRoot + '*.js', + cfgRoot + '*.js' + ]) + + .pipe(gulp.dest(buildDir)); }); gulp.task('test', function() { diff --git a/src/services.js b/src/services.js index a22c31e..624f033 100644 --- a/src/services.js +++ b/src/services.js @@ -13,3 +13,8 @@ services.fetch = function(url, oauthservice, opts) { services.parse_xml = function(xml) { return XmlService.parse(xml); }; + +// this block is for when running in node outside of GAS +if (typeof exports !== 'undefined') { + exports.services = services; +} diff --git a/test/backblasts_tests.js b/test/backblasts_tests.js index f722402..7abe029 100644 --- a/test/backblasts_tests.js +++ b/test/backblasts_tests.js @@ -1,80 +1,124 @@ var assert = require("chai").assert; -var bb = require("../src/backblasts.js").BackblastChecker; +var sinon = require("sinon"); +var bb = require("../src/backblasts.js").backblasts; +// Global Scope = :( +services = require("../src/services.js").services; -describe('Backblasts Additional Data', function() { - describe('getAdditionalData', function() { + +function mockDocument() { + return { + getRootElement: function() { + return { + getChild: function() { + return { + getChildren: function() { + return {}; + } + }; + } + }; + } + }; +} + +describe('Backblasts Data', function() { + var cfg = {}; + describe('checkForUpdates', function() { it('should parse out workout dates', function() { - var content = "
  • When:01/01/2016
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.date === "01/01/2016", "Should parse out date"); - }); + var fetch = sinon.stub(services, 'fetch'); + fetch.returns("
  • When:01/01/2016
  • "); - it('should parse out todays date if workout date not found', function() { - var content = "
  • "; - var result = bb.getAdditionalData(content, []); - var date = new Date(result.date); - var today = new Date(); + var parse_xml = sinon.stub(services, 'parse_xml'); + var doc = { + getRootElement: function() { + return { + getChild: function() { + console.log("called getChild"); + return { + getChildren: sinon.stub() + }; + } + }; + } + }; + doc.getRootElement().getChild().getChildren.returns([{}]); + parse_xml.returns(doc); - assert(date.toLocaleDateString() == today.toLocaleDateString(), "Should be equal to today"); - }); - it('should parse out pax count and list', function() { - var content = "
  • The PAX:Wingman
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 1, "Should parse out pax list"); - assert(result.paxList.indexOf("wingman") != -1, "Should parse out pax list"); + var result = bb.checkForUpdates( + cfg, + function() { + return new Date().toString(); + }, + function(newDate) { + // check date here + }, + function(rowValues) { + // check Row Values here + }); + assert(result.date === "01/01/2016", "Should parse out date"); }); + /* + it('should parse out todays date if workout date not found', function() { + var content = "
  • "; + var result = bb.getAdditionalData(content, []); + var date = new Date(result.date); + var today = new Date(); - it('should parse out pax count', function() { - var content = "The PAX: The Once-ler, Waterfoot, Vida, Chin Music, Crayola, Bullwinkle (FNG), Hannibal, Knight Rider, MAD, Pele, Adobe, Smash, Balk, Fireman Ed, Marge, Lambeau, Torpedo, Goonie (QIC) "; - var result = bb.getAdditionalData(content, []); - assert(result.paxList.length === 18, "Should find the number of Pax"); - }); - it('should parse out QIC', function() { - var content = "
  • QIC:Gears
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 1, "Should parse out pax list"); - assert(result.paxList.indexOf("gears") != -1, "Should parse out pax list"); - }); - it('should handle QIC in pax list and QIC field', function() { - var content = "
  • QIC:Gears
  • " + - "
  • The PAX:Wingman, Gears
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 2, "Should parse out pax list"); - assert(result.paxList.indexOf("gears") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); - }); - it('should handle multiple in QIC field with and', function() { - var content = "
  • QIC:Gears and Old Bay
  • " + - "
  • The PAX:Wingman, Gears, Old Bay
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 3, "Should parse out pax list"); - assert(result.paxList.indexOf("gears") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("oldbay") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); - }); - it('should handle multiple in QIC field with commas', function() { - var content = "
  • QIC:Zima, Bolt, Bing
  • " + - "
  • The PAX:Wingman, Gears, Old Bay
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 6, "Should parse out pax list"); - assert(result.paxList.indexOf("zima") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("bolt") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("bing") != -1, "Should capture QIC as a PAX"); - assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); - assert(result.paxList.indexOf("gears") != -1, "Should capture non-QIC as a PAX"); - assert(result.paxList.indexOf("oldbay") != -1, "Should capture non-QIC as a PAX"); - }); - it('should handle handle common acronyms', function() { - var content = "
  • QIC:Gears(YHC)
  • " + - "
  • The PAX:Wingman (Respect), Bolt(QIC), Sharkbait (2.0)
  • "; - var result = bb.getAdditionalData(content, []); - assert(result.paxCount == 4, "Should parse out pax list"); - assert(result.paxList.indexOf("bolt") != -1, "Should contain bolt"); - assert(result.paxList.indexOf("wingman") != -1, "Should contain wingman"); - assert(result.paxList.indexOf("gears") != -1, "Should contain gears"); - assert(result.paxList.indexOf("sharkbait") != -1, "Should contain sharkbait"); - }); + assert(date.toLocaleDateString() == today.toLocaleDateString(), "Should be equal to today"); + }); it('should parse out pax count and list', function() { + var content = "
  • The PAX:Wingman
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 1, "Should parse out pax list"); + assert(result.paxList.indexOf("wingman") != -1, "Should parse out pax list"); + }); + + it('should parse out pax count', function() { + var content = "The PAX:
    The Once-ler, Waterfoot, Vida, Chin Music, Crayola, Bullwinkle (FNG), Hannibal, Knight Rider, MAD, Pele, Adobe, Smash, Balk, Fireman Ed, Marge, Lambeau, Torpedo, Goonie (QIC) "; + var result = bb.getAdditionalData(content, []); + assert(result.paxList.length === 18, "Should find the number of Pax"); + }); it('should parse out QIC', function() { + var content = "
  • QIC:Gears
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 1, "Should parse out pax list"); + assert(result.paxList.indexOf("gears") != -1, "Should parse out pax list"); + }); it('should handle QIC in pax list and QIC field', function() { + var content = "
  • QIC:Gears
  • " + + "
  • The PAX:Wingman, Gears
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 2, "Should parse out pax list"); + assert(result.paxList.indexOf("gears") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); + }); it('should handle multiple in QIC field with and', function() { + var content = "
  • QIC:Gears and Old Bay
  • " + + "
  • The PAX:Wingman, Gears, Old Bay
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 3, "Should parse out pax list"); + assert(result.paxList.indexOf("gears") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("oldbay") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); + }); it('should handle multiple in QIC field with commas', function() { + var content = "
  • QIC:Zima, Bolt, Bing
  • " + + "
  • The PAX:Wingman, Gears, Old Bay
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 6, "Should parse out pax list"); + assert(result.paxList.indexOf("zima") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("bolt") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("bing") != -1, "Should capture QIC as a PAX"); + assert(result.paxList.indexOf("wingman") != -1, "Should capture non-QIC as a PAX"); + assert(result.paxList.indexOf("gears") != -1, "Should capture non-QIC as a PAX"); + assert(result.paxList.indexOf("oldbay") != -1, "Should capture non-QIC as a PAX"); + }); it('should handle handle common acronyms', function() { + var content = "
  • QIC:Gears(YHC)
  • " + + "
  • The PAX:Wingman (Respect), Bolt(QIC), Sharkbait (2.0)
  • "; + var result = bb.getAdditionalData(content, []); + assert(result.paxCount == 4, "Should parse out pax list"); + assert(result.paxList.indexOf("bolt") != -1, "Should contain bolt"); + assert(result.paxList.indexOf("wingman") != -1, "Should contain wingman"); + assert(result.paxList.indexOf("gears") != -1, "Should contain gears"); + assert(result.paxList.indexOf("sharkbait") != -1, "Should contain sharkbait"); + }); + */ }); });