diff --git a/gulpfile.js b/gulpfile.js index e1dd1517f91..141cac6d585 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -6,13 +6,19 @@ var jscs = require('gulp-jscs'); var header = require('gulp-header'); var del = require('del'); var ecstatic = require('ecstatic'); -var browserify = require('gulp-browserify'); +var gulpBrowserify = require('gulp-browserify'); var gutil = require("gulp-util"); var gulpJsdoc2md = require("gulp-jsdoc-to-markdown"); var concat = require("gulp-concat"); var zip = require('gulp-zip'); var mocha = require('gulp-mocha'); var preprocessify = require('preprocessify'); +/** for automated unit testing */ +var browserSync = require("browser-sync"), + browserify = require("browserify"), + source = require("vinyl-source-stream"), + mochaPhantomJS = require("gulp-mocha-phantomjs"); + var releaseDir = './dist/'; var csaSrcLocation = './src/prebid.js'; @@ -93,18 +99,12 @@ gulp.task('clean-dist', function(cb) { }); -//longer tests go here, to be run only when specfcifcally testing -gulp.task('runAdvancedTests', function() { - -}); - gulp.task('codeQuality', ['jshint', 'jscs'], function() {}); -gulp.task('testAll', ['runBasicTests', 'runAdvancedTests'], function() {}); gulp.task('default', ['build'], function() {}); -gulp.task('serve', ['build-dev', 'watch'], function () { +gulp.task('serve', ['build-dev', 'watch', 'browser-sync'], function () { var port = 9999; require('http').createServer(ecstatic({ root: __dirname @@ -112,22 +112,22 @@ gulp.task('serve', ['build-dev', 'watch'], function () { console.log('Server started at http://localhost:' + port + '/'); }); -gulp.task('build-dev', ['jscs', 'clean-dist'], function () { - gulp.src(['src/prebid.js']) - .pipe(browserify({ - debug: false - })) +gulp.task('build-dev', ['jscs', 'clean-dist', 'browserify', 'unit-tests'], function () { + gulp.src(['src/prebid.js']) + .pipe(gulpBrowserify({ + debug: false + })) .pipe(header(banner, { pkg: pkg })) - .pipe(gulp.dest(releaseDir)); + .pipe(gulp.dest(releaseDir)); }); gulp.task('build-minify', ['clean-dist', 'quality'], function(cb){ gulp.src(['src/prebid.js']) - .pipe(browserify({ + .pipe(gulpBrowserify({ transform : preprocessify({ NODE_ENV: 'production'}) } @@ -155,6 +155,7 @@ gulp.task('quality', ['jscs'], function(cb){ gulp.task('watch', function () { gulp.watch(['src/**/*.js'], ['build-dev']); + //gulp.watch(["test/tests.js", "src/*"], ["browserify", "unit-tests"]); }); @@ -179,3 +180,34 @@ gulp.task('zip', ['quality', 'clean-dist', 'build-minify'], function () { .pipe(gulp.dest('./')); }); +gulp.task("browser-sync", function () { + "use strict"; + browserSync({ + server: { + //serve tests and the root as base dirs + baseDir: ["./test/", "./"], + //make tests.html the index file + index: "automatedRunnner.html" + } + }); +}); + +//see http://fettblog.eu/gulp-browserify-multiple-bundles/ +gulp.task("browserify", function() { + "use strict"; + return browserify("./test/test.js") + .bundle() + .on("error", function (err) { + console.log(err.toString()); + this.emit("end"); + }) + .pipe(source("tests-browserify.js")) + .pipe(gulp.dest("test/")) + .pipe(browserSync.reload({stream:true})); +}); + +gulp.task("unit-tests", function () { + "use strict"; + return gulp.src("./test/automatedRunnner.html") + .pipe(mochaPhantomJS()); +}); diff --git a/package.json b/package.json index 877b23765d0..ac139a0ba43 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "author": [], "license": "NONE", "devDependencies": { + "browser-sync": "^2.11.0", + "browserify": "^12.0.1", "del": "^1.1.1", "ecstatic": "^0.8.0", "gulp": "^3.9.0", @@ -19,6 +21,7 @@ "gulp-jsdoc-to-markdown": "^1.1.1", "gulp-jshint": "^1.9.2", "gulp-mocha": "^2.1.3", + "gulp-mocha-phantomjs": "^0.10.1", "gulp-notify": "^2.2.0", "gulp-rename": "^1.2.2", "gulp-sourcemaps": "^1.5.0", @@ -28,7 +31,10 @@ "gulp-zip": "^3.0.2", "jsdoc-to-markdown": "^1.1.1", "jshint-stylish": "^2.0.1", - "preprocessify": "0.0.6" + "mocha": "^2.3.4", + "mocha-phantomjs": "^4.0.2", + "preprocessify": "0.0.6", + "vinyl-source-stream": "^1.1.0" }, "dependencies": {} } diff --git a/src/bidmanager.js b/src/bidmanager.js index 27e12b9c4ab..f52062d08e4 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -168,7 +168,7 @@ exports.addBidResponse = function(adUnitCode, bid) { //if there is any key value pairs to map do here var keyValues = {}; if (bid.bidderCode && bid.cpm !== 0) { - keyValues = getKeyValueTargetingPairs(bid.bidderCode, bid); + keyValues = this.getKeyValueTargetingPairs(bid.bidderCode, bid); bid.adserverTargeting = keyValues; } @@ -213,7 +213,7 @@ exports.createEmptyBidResponseObj = function() { }; }; -function getKeyValueTargetingPairs(bidderCode, custBidObj) { +exports.getKeyValueTargetingPairs = function(bidderCode, custBidObj) { //retrive key value settings var keyValues = {}; var bidder_settings = pbjs.bidderSettings || {}; @@ -260,7 +260,7 @@ function getKeyValueTargetingPairs(bidderCode, custBidObj) { } return keyValues; -} +}; function setKeys(keyValues, bidderSettings, custBidObj) { var targeting = bidderSettings[CONSTANTS.JSON_MAPPING.ADSERVER_TARGETING]; diff --git a/test/automatedRunnner.html b/test/automatedRunnner.html new file mode 100644 index 00000000000..2f82a9a36da --- /dev/null +++ b/test/automatedRunnner.html @@ -0,0 +1,29 @@ + + + + + + Unit Tests for Prebid.js + + + +
+ + + + + + + + + + + \ No newline at end of file diff --git a/test/test.js b/test/test.js new file mode 100644 index 00000000000..2806a7dc6e2 --- /dev/null +++ b/test/test.js @@ -0,0 +1,181 @@ +var assert = require("assert"); + +/* use this method to test individual files instead of the whole prebid.js project */ + +//TODO refactor to use the spec files +var utils = require('../src/utils'); +var bidmanager = require('../src/bidmanager'); + + describe('replaceTokenInString', function(){ + + it('should replace all given tokens in a String', function() { + var tokensToReplace = { + 'foo': 'bar', + 'zap': 'quux' + }; + + var output = utils.replaceTokenInString("hello %FOO%, I am %ZAP%", tokensToReplace, "%"); + assert.equal(output, "hello bar, I am quux"); + }); + + it('should ignore tokens it does not see', function() { + var output = utils.replaceTokenInString("hello %FOO%", {}, "%"); + + assert.equal(output, "hello %FOO%"); + }); + }); + + + describe('bidmanager.js', function(){ + + + + describe('getKeyValueTargetingPairs', function(){ + var bid = {}; + var bidPriceCpm = 5.578; + var bidPbLg = 5.50; + var bidPbMg = 5.50; + var bidPbHg = 5.57; + var adUnitCode = '12345'; + var bidderCode = 'appnexus'; + var size = '300x250'; + var adId = '1adId'; + + before(function() { + console.log(pbjs); + bid.cpm = bidPriceCpm; + bid.pbLg = bidPbLg; + bid.pbMg = bidPbMg; + bid.pbHg = bidPbHg; + bid.height = 300; + bid.width = 250; + bid.adUnitCode = adUnitCode; + bid.getSize = function(){ + return this.height + 'x' + this.width; + }; + bid.bidderCode = bidderCode; + bid.adId = adId; + + }); + + + it('No bidder level configuration defined - default', function() { + var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbMg,"hb_size": size}; + var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); + assert.deepEqual(response, expected); + + }); + + it('Custom configuration for all bidders', function() { + pbjs.bidderSettings = + { + standard: { + adserverTargeting: [{ + key: "hb_bidder", + val: function(bidResponse) { + return bidResponse.bidderCode; + } + }, { + key: "hb_adid", + val: function(bidResponse) { + return bidResponse.adId; + } + }, { + key: "hb_pb", + val: function(bidResponse) { + //change default here + return bidResponse.pbHg; + } + }, { + key: "hb_size", + val: function(bidResponse) { + return bidResponse.size; + + } + }] + + } + }; + + var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbHg,"hb_size": size}; + var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); + assert.deepEqual(response, expected); + + }); + + it('Custom configuration for one bidder', function() { + pbjs.bidderSettings = + { + appnexus: { + adserverTargeting: [{ + key: "hb_bidder", + val: function(bidResponse) { + return bidResponse.bidderCode; + } + }, { + key: "hb_adid", + val: function(bidResponse) { + return bidResponse.adId; + } + }, { + key: "hb_pb", + val: function(bidResponse) { + //change default here + return bidResponse.pbHg; + } + }, { + key: "hb_size", + val: function(bidResponse) { + return bidResponse.size; + + } + }] + + } + }; + + var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbHg,"hb_size": size}; + var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); + assert.deepEqual(response, expected); + + }); + + it('Custom configuration for one bidder - not matched', function() { + pbjs.bidderSettings = + { + nonExistentBidder: { + adserverTargeting: [{ + key: "hb_bidder", + val: function(bidResponse) { + return bidResponse.bidderCode; + } + }, { + key: "hb_adid", + val: function(bidResponse) { + return bidResponse.adId; + } + }, { + key: "hb_pb", + val: function(bidResponse) { + //change default here + return bidResponse.pbHg; + } + }, { + key: "hb_size", + val: function(bidResponse) { + return bidResponse.size; + + } + }] + + } + }; + + var expected = {"hb_bidder": bidderCode, "hb_adid": adId,"hb_pb": bidPbMg,"hb_size": size}; + var response = bidmanager.getKeyValueTargetingPairs(bidderCode, bid); + assert.deepEqual(response, expected); + + }); + + }); + }); \ No newline at end of file