From 371e21768c29f82f8ba53cde94a4543b99d02358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Gutie=CC=81rrez=20Rojas?= Date: Mon, 16 Jan 2017 15:18:27 +0100 Subject: [PATCH 1/4] added mocha/chai to package.json --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3658c84..b3fc019 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "HTML5 inApp support chat using your own firebase account", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "./node_modules/mocha/bin/mocha" }, "repository": { "type": "git", @@ -34,6 +34,7 @@ "homepage": "https://github.com/CodingCarlos/inapp-support#readme", "devDependencies": { "browser-sync": "^2.18.2", + "chai": "^3.5.0", "gulp": "^3.9.1", "gulp-clean": "^0.3.2", "gulp-concat": "^2.6.1", @@ -42,6 +43,7 @@ "gulp-rename": "^1.2.2", "gulp-sourcemaps": "^1.9.1", "gulp-uglify": "^2.0.0", + "mocha": "^3.2.0", "run-sequence": "^1.2.2" } } From bd9ae905473cba0685135ee2c736eb983f68fd86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Gutie=CC=81rrez=20Rojas?= Date: Mon, 16 Jan 2017 16:12:20 +0100 Subject: [PATCH 2/4] extracted validateExtension functionality to utils module --- js/utils.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 js/utils.js diff --git a/js/utils.js b/js/utils.js new file mode 100644 index 0000000..0410d67 --- /dev/null +++ b/js/utils.js @@ -0,0 +1,25 @@ +function validateExtension(file) { + + var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; + var fileName = file.name; + + var extension = null; + + if (fileName.length > 0) { + + for (var j = 0; j < _validFileExtensions.length; j++) { + var thisExt = _validFileExtensions[j]; + + // Check the extension is valid + if (fileName.substr(fileName.length - thisExt.length, thisExt.length).toLowerCase() == thisExt.toLowerCase()) { + extension = thisExt; + break; + } + } + + } + + return extension; +} + +module.exports = {validateExtension}; \ No newline at end of file From 12ffd93fd8763272d438ff19434e70f111023cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Gutie=CC=81rrez=20Rojas?= Date: Mon, 16 Jan 2017 16:12:47 +0100 Subject: [PATCH 3/4] created tests for the validateExtension method --- test/chat.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test/chat.js diff --git a/test/chat.js b/test/chat.js new file mode 100644 index 0000000..1857c08 --- /dev/null +++ b/test/chat.js @@ -0,0 +1,56 @@ +const expect = require('chai').expect; + +const utils = require('../js/utils'); + +describe('Chat inapp', () => { + describe('validates file extensions', () => { + it('jpg is a valid extension', () => { + const file = {name:'chat.jpg'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.equal('.jpg'); + }); + + it('png is a valid extension', () => { + const file = {name:'chat.png'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.equal('.png'); + }); + + it('js is NOT a valid extension', () => { + const file = {name:'chat.js'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.be.null; + }); + + it('PNG is a valid extension', () => { + const file = {name:'chat.PNG'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.equal('.png'); + }); + + it('no extension is NOT valid', () => { + const file = {name:'chat'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.be.null; + }); + + it('just extension is NOT valid', () => { + const file = {name:'jpg'}; + + const ext = utils.validateExtension(file); + + expect(ext).to.be.null; + }); + + }); +}); \ No newline at end of file From 3555e55fd40e3bd206909ff502171523c40a0eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Gutie=CC=81rrez=20Rojas?= Date: Mon, 16 Jan 2017 16:19:59 +0100 Subject: [PATCH 4/4] refactor of validateExtension --- js/utils.js | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/js/utils.js b/js/utils.js index 0410d67..79e132d 100644 --- a/js/utils.js +++ b/js/utils.js @@ -1,25 +1,10 @@ function validateExtension(file) { + const fileName = file.name; + const fileExtension =fileName.substr(fileName.lastIndexOf('.')).toLowerCase(); - var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; - var fileName = file.name; + const _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; - var extension = null; - - if (fileName.length > 0) { - - for (var j = 0; j < _validFileExtensions.length; j++) { - var thisExt = _validFileExtensions[j]; - - // Check the extension is valid - if (fileName.substr(fileName.length - thisExt.length, thisExt.length).toLowerCase() == thisExt.toLowerCase()) { - extension = thisExt; - break; - } - } - - } - - return extension; + return _validFileExtensions.find((ext) => fileExtension === ext) || null; } module.exports = {validateExtension}; \ No newline at end of file