From 3465808eaaae5a6b4a62d1fd6c9d822fc3d5fb6d Mon Sep 17 00:00:00 2001 From: Joaquim Neto Date: Mon, 27 May 2019 16:39:18 -0300 Subject: [PATCH] =?UTF-8?q?perf(*):=20implementa=C3=A7ao=20est=C3=A1vel=20?= =?UTF-8?q?do=20travis,=20semantic-release=20e=20testes=20unit=C3=A1rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/gtm/main.js | 566 +++++++++++++++++++++++++++++ gulpfile.js | 65 ++-- package.json | 213 ++++++----- test/core/modules/closet.test.js | 14 + test/core/modules/cookie.test.js | 20 + test/core/modules/has.test.js | 20 +- test/core/modules/sanitize.test.js | 50 +-- test/core/modules/text.test.js | 17 + test/index.test.js | 4 +- test/jsdom-site-mock.js | 8 + test/src/site/index.html | 182 ++++++++++ 11 files changed, 986 insertions(+), 173 deletions(-) create mode 100644 test/core/modules/closet.test.js create mode 100644 test/core/modules/cookie.test.js create mode 100644 test/core/modules/text.test.js create mode 100644 test/jsdom-site-mock.js create mode 100644 test/src/site/index.html diff --git a/build/gtm/main.js b/build/gtm/main.js index e69de29..2da69eb 100755 --- a/build/gtm/main.js +++ b/build/gtm/main.js @@ -0,0 +1,566 @@ +; +(function () { + 'use strict'; + var hasOwnProperty = Object.prototype.hasOwnProperty; + var jQuery = window.jQuery; + var fn = {}; + + var options = { + helperName: 'analyticsHelper', + dataLayerName: 'dataLayer', + debug: ({{Debug Mode}} || false), + waitQueue: true, + containerId: ({{Container ID}} || ''), + exceptionEvent: 'gtm_dataQuality_event', + exceptionCategory: 'GTM Exception', + customNamePageview: 'ga_pageview', + customNameEvent: 'ga_event', + customNameTiming: 'ga_timing', + errorSampleRate: 1, + gtmCleanup: function (gtmId) { + helper.setDataLayer('ecommerce', undefined); + helper.setDataLayer('noInteraction', undefined); + } + }; + + var internal = { + sentPageview: false + }; + + var helper = { + internal: internal, + init: init, + pageview: pageview, + event: event, + timing: timing, + sanitize: sanitize, + getDataLayer: getDataLayer, + setDataLayer: setDataLayer, + cookie: cookie, + getKey: getKey, + safeFn: safeFn, + fn: fn, + options: options + }; + function closest(elm, seletor) { + if ('closest' in elm) return elm.closest(seletor); + if (typeof jQuery === 'function') return jQuery(elm).closest(seletor)[0]; + + var parent = elm.parentNode; + + while (parent != document) { + if (matches(parent, seletor)) { + return parent; + } + parent = parent.parentNode; + } + return undefined; + } + + function getCookie(key) { + key = ('; ' + key + '='); + var cookie = ('; ' + document.cookie); + var index = cookie.indexOf(key); + var end; + if (index === -1) { + return ''; + } + cookie = cookie.substring(index + key.length); + end = cookie.indexOf(';'); + return window.unescape(end === -1 ? cookie : cookie.substring(0, end)); + } + + function setCookie(name, value, opts) { + var exdate, cookie; + opts = opts || {}; + + cookie = name + "=" + window.escape(value); + if (opts.exdays) { + exdate = new Date(); + exdate.setDate(exdate.getDate() + opts.exdays); + cookie += "; expires=" + exdate.toUTCString(); + } + if (opts.domain) { + cookie += "; domain=" + opts.domain; + } + cookie += "; path=" + (opts.path || '/'); + return (document.cookie = cookie); + } + + function cookie(name, value, opts) { + if (typeof value === 'undefined') + return getCookie(name); + + return setCookie(name, value, opts); + } + + function delegate(id, event, selector, oldHandler, parent) { + var method, elm, handler; + if (typeof jQuery === 'function') { + elm = jQuery(parent || document); + handler = safeFn(id, oldHandler, { + event: event, + selector: selector, + immediate: false + }); + if (typeof elm.on === 'function') { + return elm.on(event, selector, handler); + } else if (typeof elm.delegate === 'function') { + return elm.delegate(selector, event, handler); + } + } + + if (typeof parent === 'string') { + parent = document.querySelectorAll(parent); + } + + if (typeof document.addEventListener === 'function') { + method = 'addEventListener'; + } else { + method = 'attachEvent'; + event = 'on' + event; + } + + handler = function(e) { + for ( + var target = e.target; target && target !== this; target = target.parentNode + ) { + if (matches(target, selector)) { + var handler = safeFn(id, oldHandler, { + event: event, + selector: selector, + immediate: false + }); + handler.call(target, e); + break; + } + } + }; + + if (Object.prototype.toString.call(parent) === '[object NodeList]') { + for (var parentIndex = 0; parentIndex <= parent.length - 1; parentIndex++) { + (parent[parentIndex] || document)[method](event, handler, false); + } + } else { + (parent || document)[method](event, handler, false); + } + } + + function find(element, selector) { + return element.querySelectorAll(selector); + } + + function flatten(arrs) { + var currentArray, currentElement, i, j; + var result = []; + + if (arrs.length === 1) return arrs[0]; + + while (arrs.length > 0) { + currentArray = arrs.shift(); + for (i = 0; currentArray.length > i; i++) { + currentElement = currentArray[i]; + j = 0; + while (j < result.length && currentElement !== result[j]) { + j += 1; + } + if (j === result.length) result.push(currentElement); + } + } + + return result; + } + + function getKey(key, opt_root) { + if (!key || typeof key !== 'string') return undefined; + + var result = opt_root || window; + var splitKey = key.split('.'); + + for (var i = 0; i < splitKey.length && result != null; i++) { + if (has(result, splitKey[i])) { + result = result[splitKey[i]]; + } else { + return undefined; + } + } + return result; + } + + function has(obj, key) { + return hasOwnProperty.call(obj, key); + } + + function hasClass(e, className) { + if ('classList' in e) return e.classList.contains(className); + + return new RegExp('\\b' + className + '\\b').test(e.className); + } + + function init(opt_options) { + options = merge(options, opt_options); + expose(); + } + + function internalMap(elms, func, exArgs) { + var elm, args; + var ret = []; + for (var index = 0; index < elms.length; index++) { + elm = elms[index]; + if (elm instanceof HTMLElement === false) throw 'internalMap: Esperado elemento HTML'; + args = [elm].concat(exArgs); + ret.push(func.apply(null, args)); + } + return ret; + } + + function log(type, info, obj) { + if (options.debug && typeof getKey('console.' + type) === 'function') { + console[type](info, obj); + } + } + + function matches(elm, seletor) { + if ('matches' in elm) return elm.matches(seletor); + if (typeof jQuery === "function") return jQuery(elm).is(seletor); + + var elms = elm.parentNode.querySelectorAll(seletor); + + for (var i = 0; i < elms.length; i++) { + if (elms[i] === elm) { + return true; + } + } + return false; + } + + function merge(obj, obj2) { + if (obj2) { + for (var key in obj2) { + if (has(obj2, key)) { + obj[key] = obj2[key]; + } + } + } + return obj; + } + + function on(id, event, selector, oldCallback, parent) { + var i, array, elm, callback; + + if (parent) return delegate(id, event, selector, oldCallback, parent); + + callback = safeFn(id, oldCallback, { + event: event, + selector: selector, + immediate: false + }); + + if (typeof jQuery === 'function') { + elm = jQuery(selector); + + if (typeof elm.on === 'function') { + return elm.on(event, callback); + } else if (typeof elm.bind === 'function') { + return elm.bind(event, callback); + } + } + + if (typeof selector === 'string') { + array = document.querySelectorAll(selector); + } else if (typeof selector.length === 'undefined' || selector === window) { + array = [selector]; + } else { + array = selector; + } + + for (i = 0; i < array.length; i++) { + elm = array[i]; + + if (typeof elm.addEventListener === 'function') { + elm.addEventListener(event, callback); + } else { + elm.attachEvent('on' + event, callback); + } + } + } + + function reduceBool(arr) { + for (var i = 0; i < arr.length; i++) { + if (arr[i]) return true; + } + return false; + } + + function sanitize(str, opts) { + var split, i, spacer; + + if (!str) return ''; + opts = opts || {}; + spacer = typeof opts.spacer === 'string' ? opts.spacer : '_'; + str = str.toLowerCase() + .replace(/^\s+/, '') + .replace(/\s+$/, '') + .replace(/\s+/g, '_') + .replace(/[áàâãåäæª]/g, 'a') + .replace(/[éèêëЄ€]/g, 'e') + .replace(/[íìîï]/g, 'i') + .replace(/[óòôõöøº]/g, 'o') + .replace(/[úùûü]/g, 'u') + .replace(/[碩]/g, 'c') + .replace(/[^a-z0-9_\-]/g, '_'); + + if (opts.capitalized) { + split = str.replace(/^_+|_+$/g, '').split(/_+/g); + for (i = 0; i < split.length; i++) { + if (split[i]) split[i] = split[i][0].toUpperCase() + split[i].slice(1); + } + return split.join(spacer); + } + + return str.replace(/^_+|_+$/g, '').replace(/_+/g, spacer); + } + + function text(elm, opts) { + var i, text, children; + opts = opts || {}; + + if (opts.onlyFirst) { + children = elm.childNodes; + text = ''; + + for (i = 0; i < children.length; i++) { + if (children[i].nodeType === 3) { + text += children[i].nodeValue; + } + } + } else { + text = elm.innerText || elm.textContent || elm.innerHTML.replace(/<[^>]+>/g, ''); + } + + return opts.sanitize ? sanitize(text, opts.sanitize) : text; + } + + function getDataLayer(key) { + try { + return google_tag_manager[options.containerId].dataLayer.get(key); + } catch ($$e) { + log('warn', 'Function getDataLayer: Object ' + key + ' is not defined'); + } + } + + function setDataLayer(key, value) { + try { + return google_tag_manager[options.containerId].dataLayer.set(key, value); + } catch ($$e) { + log('warn', $$e); + } + } + internal.eventQueue = []; + + function event(category, action, label, value, object, id) { + try { + if (internal.sentPageview === false && options.waitQueue) { + log('Info', 'The event (' + arguments + ') has been add to the queue'); + return internal.eventQueue.push(arguments); + } + + if (value != null && typeof value === 'object') { + object = value; + value = undefined; + } else { + object = object || {}; + } + + var result = { + event: options.customNameEvent, + eventCategory: category, + eventAction: action, + eventValue: value, + eventLabel: label, + _tag: id + }; + + if (options.gtmCleanup) { + result.eventCallback = options.gtmCleanup; + } + + log('info', result, object); + window[options.dataLayerName].push(merge(result, object)); + } catch (err) { + log('warn', err); + } + } + + function localHelperFactory(conf) { + var localHelper = { + event: function(category, action, label, value, object) { + return event(category, action, label, value, object, conf.id); + }, + pageview: function(path, object) { + return pageview(path, object, conf.id); + }, + timing: function(category, variable, value, label, object) { + return timing(category, variable, value, label, object, conf.id); + }, + safeFn: function(id, callback, opts) { + return safeFn(conf.id, callback, opts); + }, + on: function(event, selector, callback, parent) { + return on(conf.id, event, selector, callback, parent); + }, + delegate: function(event, selector, callback) { + return on(conf.id, event, selector, callback, document.body); + }, + wrap: function(elm) { + if (typeof elm === 'object' && elm._type === 'wrapped') { + return elm; + } else if (typeof elm === 'string') { + elm = find(window.document, elm); + } else if (elm instanceof HTMLElement) { + elm = [elm]; + } else if ((elm instanceof Array || elm instanceof NodeList) === false) { + throw 'wrap: Esperado receber seletor, elemento HTML, NodeList ou Array'; + } + + return { + _type: 'wrapped', + hasClass: function(className, opts) { + var arr = internalMap(elm, hasClass, [className]); + return (opts && opts.toArray) ? arr : reduceBool(arr); + }, + matches: function(selector, opts) { + var arr = internalMap(elm, matches, [selector]); + return (opts && opts.toArray) ? arr : reduceBool(arr); + }, + closest: function(selector) { + return localHelper.wrap(internalMap(elm, closest, [selector])); + }, + text: function(opts) { + var arr = internalMap(elm, text, [opts]); + return (opts && opts.toArray) ? arr : arr.join(''); + }, + find: function(sel) { + var elms = internalMap(elm, find, [sel]); + return localHelper.wrap(flatten(elms)); + }, + map: function(func, params) { + return internalMap(elm, func, params); + }, + on: function(event, parent, callback) { + if (typeof parent === "function") { + on(conf.id, event, elm, parent); + } else { + on(conf.id, event, parent, callback, elm); + } + }, + nodes: elm + }; + }, + sanitize: sanitize, + getDataLayer: getDataLayer, + setDataLayer: setDataLayer, + cookie: cookie, + getKey: getKey, + id: conf.id, + args: conf.args, + fn: fn, + log: log, + _event: conf.event, + _selector: conf.selector + }; + return localHelper; + } + function pageview(path, object, id) { + try { + var result = { + event: options.customNamePageview, + path: path, + _tag: id + }; + + if (options.gtmCleanup) { + result.eventCallback = options.gtmCleanup; + } + + log('info', result, object); + window[options.dataLayerName].push(merge(result, object)); + } catch (err) { + log('warn', err); + } + } + + function safeFn(id, callback, opt) { + opt = opt || {}; + var safe = function() { + try { + callback.call(this === window ? null : this, localHelperFactory({ + id: id, + args: arguments, + event: (typeof opt.event === "string" && opt.event || undefined), + selector: (typeof opt.selector === "string" && opt.selector || undefined) + })); + } catch ($$e) { + if (!options.debug) { + if (Math.random() <= options.errorSampleRate) { + window[options.dataLayerName].push({ + event: options.exceptionEvent, + dataQuality: { + category: options.exceptionCategory, + action: id, + label: String($$e), + event: (typeof opt.event === "string" && opt.event || undefined), + selector: (typeof opt.selector === "string" && opt.selector || undefined) + } + }); + } + } else { + log('warn', 'Exception: ', { + exception: $$e, + tag: id, + event: (typeof opt.event === "string" && opt.event || undefined), + selector: (typeof opt.selector === "string" && opt.selector || undefined) + }); + } + } + }; + + return opt.immediate === false ? safe : safe(); + } + internal.timingQueue = []; + + function timing(category, variable, value, label, object, id) { + try { + if (internal.sentPageview === false && options.waitQueue) { + log('Info', 'The timing event (' + arguments + ') has been add to the queue'); + return internal.timingQueue.push(arguments); + } + + object = object || {}; + + var result = { + event: options.customNameTiming, + timingCategory: category, + timingVariable: variable, + timingValue: value, + timingLabel: label, + _tag: id + }; + + if (options.gtmCleanup) { + result.eventCallback = options.gtmCleanup; + } + + log('info', result, object); + window[options.dataLayerName].push(merge(result, object)); + } catch (err) { + log('warn', err); + } + } + function expose() { + if (window[options.helperName] && !options.overwriteHelper) return; + window[options.helperName] = helper; + } + + expose(); +}()); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index a23562b..f0746bd 100755 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,35 +1,42 @@ 'use strict'; -const gulp = require('gulp'); -const concat = require('gulp-concat'); -const beautify = require('gulp-beautify'); -const include = require('gulp-include'); -const strip = require('gulp-strip-comments'); -const del = require('del'); +var gulp = require('gulp'); +var concat = require('gulp-concat'); +var beautify = require('gulp-beautify'); +var include = require('gulp-include'); +var strip = require('gulp-strip-comments'); +var del = require('del'); +var replace = require('gulp-replace'); -gulp.task('gtm-modules', () => - gulp - .src(['./core/modules/*.js', './gtm/modules/*js']) - .pipe(concat('gtm-modules.js')) - .pipe(beautify({ - indent_size: 2 - })) - .pipe(strip()) - .on('error', console.log) - .pipe(gulp.dest('./tmp')) -); +gulp.task('gtm-modules', function() { + return gulp.src(['./core/modules/*.js', './gtm/modules/*js']) + .pipe(replace(/module.exports[a-z-A-Z.]*\s*=\s*([a-zA-Z\-_]+;|([a-zA-Z\-_:.={},\n\s]+);+)/g, '')) + .pipe(concat('gtm-modules.js')) + .pipe(beautify({ + indent_size: 2, + max_preserve_newlines: 2 + })) + .pipe(strip()) + .on('error', console.log) + .pipe(gulp.dest('./tmp')); +}); -gulp.task('build-gtm', () => - gulp - .src('./gtm/main.js') - .pipe(include({ - includePaths: ['/tmp', '/gtm'].map(path => __dirname + path), - hardFail: true - })) - .on('error', console.log) - .pipe(gulp.dest('./build/gtm')) -); +gulp.task('build-gtm', function() { + return gulp.src('./gtm/main.js') + .pipe(replace(/module.exports\s*=\s*[a-zA-Z]+;/g, '')) + .pipe(include({ + hardFail: true, + includePaths: [ + __dirname + '/tmp', + __dirname + '/gtm', + ] + })) + .on('error', console.log) + .pipe(gulp.dest('./build/gtm')); +}); -gulp.task('clean', () => del(['./tmp'])); +gulp.task('clean', function() { + return del(['./tmp']); +}); -gulp.task('default', gulp.series(['clean', 'gtm-modules', 'build-gtm', 'clean'])); +gulp.task('default', gulp.series(['clean', 'gtm-modules', 'build-gtm', 'clean'])); \ No newline at end of file diff --git a/package.json b/package.json index bca6470..0d072ee 100755 --- a/package.json +++ b/package.json @@ -1,112 +1,109 @@ { - "name": "analytics-helper", - "version": "1.0.1", - "description": "> Biblioteca auxiliar para implementação dos Tag Managers da DP6.", - "main": "index.js", - "repository": { - "type": "git", - "url": "git+https://github.com/joaquimsn/analytics-helper.git" - }, - "author": "", - "contributors": [ - { - "name": "Bruno Munhoz", - "email": "bpm1993@gmail.com" + "name": "analytics-helper", + "version": "1.0.1", + "description": "> Biblioteca auxiliar para implementação dos Tag Managers da DP6.", + "main": "index.js", + "repository": { + "type": "git", + "url": "git+https://github.com/DP6/analytics-helper.git" }, - { - "name": "Paulo Brumatti", - "email": "paulo8624@gmail.com" - } - ], - "license": "ISC", - "bugs": { - "url": "https://github.com/joaquimsn/analytics-helper/issues" - }, - "homepage": "https://github.com/joaquimsn/analytics-helper#readme", - "scripts": { - "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text-summary --reporter=text mocha test/index.test.js", - "coverage": "nyc report --reporter=text-lcov | coveralls" - }, - "nyc": { - "check-coverage": true, - "per-file": true, - "lines": [ - 80, - 99 + "author": "", + "contributors": [ + { + "name": "Bruno Munhoz", + "email": "bpm1993@gmail.com" + }, + { + "name": "Paulo Brumatti", + "email": "paulo8624@gmail.com" + } ], - "statements": [ - 90, - 99 - ], - "functions": [ - 90, - 99 - ], - "branches": [ - 80, - 99 - ], - "exclude": [ - "build", - "documentation-images" - ], - "include": [ - "core/**/*.js", - "gtm/*.js", - "gtm/**/*.js" - ], - "reporter": [ - "lcov", - "text-summary", - "text" - ], - "require": [ - "@babel/register" - ], - "sourceMap": false, - "instrument": false - }, - "release": { - "branch": "master", - "verifyConditions": [ - "@semantic-release/github" - ], - "publish": [ - "@semantic-release/github" - ], - "prepare": [], - "success": [ - "@semantic-release/github" - ], - "fail": [ - "@semantic-release/github" - ] - }, - "devDependencies": { - "@babel/cli": "^7.0.0", - "@babel/core": "^7.0.1", - "@babel/preset-env": "^7.0.0", - "@babel/register": "^7.0.0", - "babel-plugin-istanbul": "^5.0.1", - "chai": "^4.1.2", - "coveralls": "^3.0.2", - "cross-env": "^5.2.0", - "del": "^3.0.0", - "gulp": "^3.9.1", - "gulp-beautify": "^2.0.1", - "gulp-concat": "^2.6.1", - "gulp-include": "^2.3.1", -<<<<<<< HEAD - "gulp-minify": "^2.1.0", - "gulp-replace": "^1.0.0", -======= ->>>>>>> 0f9f68c... Remoção de plugins sem uso - "gulp-strip-comments": "^2.5.2", - "gulp-sync": "^0.1.4", - "gulp-uglify": "^3.0.0", - "mocha": "^5.2.0", - "nyc": "^13.0.1", - "sonarqube-scanner": "^2.1.1" - }, - "dependencies": {} + "license": "ISC", + "bugs": { + "url": "https://github.com/DP6/analytics-helper/issues" + }, + "homepage": "https://github.com/DP6/analytics-helper#readme", + "scripts": { + "test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text-summary --reporter=text mocha test/index.test.js", + "coverage": "nyc report --reporter=text-lcov | coveralls" + }, + "nyc": { + "check-coverage": true, + "per-file": true, + "lines": [ + 80, + 99 + ], + "statements": [ + 90, + 99 + ], + "functions": [ + 90, + 99 + ], + "branches": [ + 80, + 99 + ], + "exclude": [ + "build", + "documentation-images" + ], + "include": [ + "core/**/*.js", + "gtm/*.js", + "gtm/**/*.js" + ], + "reporter": [ + "lcov", + "text-summary", + "text" + ], + "require": [ + "@babel/register" + ], + "sourceMap": false, + "instrument": false + }, + "release": { + "branch": "master", + "verifyConditions": [ + "@semantic-release/github" + ], + "publish": [ + "@semantic-release/github" + ], + "prepare": [], + "success": [ + "@semantic-release/github" + ], + "fail": [ + "@semantic-release/github" + ] + }, + "devDependencies": { + "@babel/cli": "^7.0.0", + "@babel/core": "^7.0.1", + "@babel/preset-env": "^7.0.0", + "@babel/register": "^7.0.0", + "babel-plugin-istanbul": "^5.0.1", + "chai": "^4.1.2", + "coveralls": "^3.0.2", + "cross-env": "^5.2.0", + "del": "^3.0.0", + "gulp": "^4.0.0", + "gulp-beautify": "^2.0.1", + "gulp-concat": "^2.6.1", + "gulp-include": "^2.3.1", + "gulp-minify": "^2.1.0", + "gulp-replace": "^1.0.0", + "gulp-strip-comments": "^2.5.2", + "gulp-uglify": "^3.0.0", + "jsdom": "^13.0.0", + "mocha": "^5.2.0", + "nyc": "^13.0.1", + "sonarqube-scanner": "^2.1.1" + }, + "dependencies": {} } diff --git a/test/core/modules/closet.test.js b/test/core/modules/closet.test.js new file mode 100644 index 0000000..cad1c49 --- /dev/null +++ b/test/core/modules/closet.test.js @@ -0,0 +1,14 @@ +'use strict'; + +const expect = require('chai').expect; +const closet = require('../../.././core/modules/closet.js'); + +describe('Core', () => { + describe('Modules', () => { + describe('.closest(elm, seletor)', () => { + it('should export a function', () => { + expect(closet).to.be.a('function'); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/core/modules/cookie.test.js b/test/core/modules/cookie.test.js new file mode 100644 index 0000000..d8eff9a --- /dev/null +++ b/test/core/modules/cookie.test.js @@ -0,0 +1,20 @@ +'use strict'; + +const expect = require('chai').expect; +const cookie = require('../../.././core/modules/cookie.js'); + +describe('Core', () => { + describe('Modules', () => { + describe('.cookie(name, value, opts)', () => { + it('should export a function', () => { + expect(cookie.cookie).to.be.a('function'); + }); + }); + + describe('.getCookie(key)', () => { + it('should export a function', () => { + expect(cookie.setCookie).to.be.a('function'); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/core/modules/has.test.js b/test/core/modules/has.test.js index 2332d64..efbb3e7 100644 --- a/test/core/modules/has.test.js +++ b/test/core/modules/has.test.js @@ -4,15 +4,15 @@ const expect = require('chai').expect; const has = require('../../.././core/modules/has.js'); describe('Core', () => { - describe('Modules', () => { - describe('.has(obj, key)', () => { - it('should export a function', () => { - expect(has).to.be.a('function'); - }); - - it('should return true if object has attribute with name of the key', () => { - expect(has({ name: 'foo' }, 'name')).true; - }); - }); + describe('Modules', () => { + describe('.has(obj, key)', () => { + it('should export a function', () => { + expect(has).to.be.a('function'); + }); + + it('should return true if object has attribute with name of the key', () => { + expect(has({ name: 'foo' }, 'name')).true; + }); }); + }); }); \ No newline at end of file diff --git a/test/core/modules/sanitize.test.js b/test/core/modules/sanitize.test.js index 51c7616..a431a3f 100644 --- a/test/core/modules/sanitize.test.js +++ b/test/core/modules/sanitize.test.js @@ -4,30 +4,30 @@ const expect = require('chai').expect; const sanitize = require('../../.././core/modules/sanitize.js'); describe('Core', () => { - describe('Modules', () => { - it('should export a function', () => { - expect(sanitize).to.be.a('function'); - }); - - describe('.sanitize(text)', () => { - it('should replace special characters for normalize String to DP6 pattern', () => { - expect(sanitize('O cão é do DONO')).to.equal('o_cao_e_do_dono'); - }); - - it('should return a empty String if text is not truthey', () => { - expect(sanitize()).to.be.empty; - }); - - }); - - describe('.sanitize(text, opts)', () => { - it('should return String with spacer equal to |', () => { - expect(sanitize('bringing science to digital marketing DP6', { spacer: '|' })).to.equal('bringing|science|to|digital|marketing|dp6'); - }); - - it('should return capitalized String', () => { - expect(sanitize('bringing science to digital marketing DP6', { capitalized: true, spacer: ' ' })).to.equal('Bringing Science To Digital eting Dp6'); - }); - }); + describe('Modules', () => { + it('should export a function', () => { + expect(sanitize).to.be.a('function'); + }); + + describe('.sanitize(text)', () => { + it('should replace special characters for normalize String to DP6 pattern', () => { + expect(sanitize('O cão é do DONO')).to.equal('o_cao_e_do_dono'); + }); + + it('should return a empty String if text is not truthey', () => { + expect(sanitize()).to.be.empty; + }); + + }); + + describe('.sanitize(text, opts)', () => { + it('should return String with spacer equal to |', () => { + expect(sanitize('bringing science to digital marketing DP6', { spacer: '|' })).to.equal('bringing|science|to|digital|marketing|dp6'); + }); + + it('should return capitalized String', () => { + expect(sanitize('bringing science to digital marketing DP6', { capitalized: true, spacer: ' ' })).to.equal('Bringing Science To Digital Marketing Dp6'); + }); }); + }); }); diff --git a/test/core/modules/text.test.js b/test/core/modules/text.test.js new file mode 100644 index 0000000..fa6f288 --- /dev/null +++ b/test/core/modules/text.test.js @@ -0,0 +1,17 @@ +'use strict'; + +const expect = require('chai').expect; +const sanitize = require('../../.././core/modules/sanitize.js'); +const text = require('../../.././core/modules/text.js'); +const domMock = require('../.././jsdom-site-mock.js'); + +describe('Core', () => { + describe('Modules', () => { + describe('.text(element)', () => { + it('should export a function', () => { + expect(text).to.be.a('function'); + }); + }); + + }); +}); \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index 8ea1ddc..2d6e861 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,4 +1,6 @@ 'use strict'; require('./core/modules/sanitize.test.js'); -require('./core/modules/has.test.js'); \ No newline at end of file +require('./core/modules/has.test.js'); +require('./core/modules/text.test.js'); +require('./core/modules/cookie.test.js'); \ No newline at end of file diff --git a/test/jsdom-site-mock.js b/test/jsdom-site-mock.js new file mode 100644 index 0000000..04a7560 --- /dev/null +++ b/test/jsdom-site-mock.js @@ -0,0 +1,8 @@ +const jsdom = require('jsdom'); +const fs = require('fs'); +const virtualConsole = new jsdom.VirtualConsole(); +const { JSDOM } = jsdom; + +const dom = {}; //new JSDOM(fs.readFileSync('./.././test/src/site/index.html').toString(), { virtualConsole }); + +module.export = dom; \ No newline at end of file diff --git a/test/src/site/index.html b/test/src/site/index.html new file mode 100644 index 0000000..7328967 --- /dev/null +++ b/test/src/site/index.html @@ -0,0 +1,182 @@ + + + + + + + + + + + Product example for Bootstrap + + + + + + + + + + + + +
+
+

Punny headline

+

And an even wittier subheading to boot. Jumpstart your marketing efforts with this example based on Apple's marketing pages.

+ Coming soon +
+
+
+
+ +
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+ +
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+ +
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+ +
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+
+

Another headline

+

And an even wittier subheading.

+
+
+
+
+ + + + + + + + + + + + + + + \ No newline at end of file