diff --git a/js/utils.js b/js/utils.js new file mode 100644 index 0000000..79e132d --- /dev/null +++ b/js/utils.js @@ -0,0 +1,10 @@ +function validateExtension(file) { + const fileName = file.name; + const fileExtension =fileName.substr(fileName.lastIndexOf('.')).toLowerCase(); + + const _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"]; + + return _validFileExtensions.find((ext) => fileExtension === ext) || null; +} + +module.exports = {validateExtension}; \ No newline at end of file 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" } } 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