From b5e8f6e3d0730755f685c7800c5055be6e229844 Mon Sep 17 00:00:00 2001 From: Thomas Meitz Date: Fri, 10 Nov 2017 17:58:05 +0100 Subject: [PATCH] moca/chai tests and new NgUploaderService setContentTypes method --- .gitignore | 2 +- package.json | 8 ++ .../classes/ngx-uploader.class.ts | 12 +++ src/ngx-uploader/classes/ngx-uploader.spec.ts | 78 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 src/ngx-uploader/classes/ngx-uploader.spec.ts diff --git a/.gitignore b/.gitignore index c33cd24b..5372f468 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ lib/ *.ngfactory.ts *.d.ts yarn.lock -.idea +.idea/ diff --git a/package.json b/package.json index 57a31b41..c677585c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "module": "lib/index.js", "scripts": { "start": "webpack-dev-server --env.client --env.serve --progress", + "start-server": "node ./dist/api/index.js", "start:aot": "webpack-dev-server --env.client --env.serve --env.aot --progress", + "test": "mocha -r ts-node/register -r ignore-styles src/**/*.spec.ts", "build": "tsc && webpack --env.client --env.aot --progress --hide-modules", "build:prod": "tsc && webpack --env.client --env.aot -p --progress --hide-modules", "prebuild": "npm run clean", @@ -39,14 +41,17 @@ "@angular/router": "^5.0.0", "@ngtools/webpack": "^1.8.0", "@types/body-parser": "^1.16.7", + "@types/chai": "^4.0.4", "@types/cors": "^2.8.1", "@types/express": "^4.0.39", "@types/jasmine": "^2.6.2", + "@types/mocha": "^2.2.44", "@types/multer": "^1.3.5", "@types/node": "^8.0.47", "@types/rimraf": "^2.0.2", "add-asset-html-webpack-plugin": "^2.1.2", "bulma": "^0.6.0", + "chai": "^4.1.2", "compression-webpack-plugin": "^1.0.1", "copy-webpack-plugin": "^4.2.0", "core-js": "^2.5.1", @@ -56,6 +61,7 @@ "extract-text-webpack-plugin": "^3.0.2", "file-loader": "^1.1.5", "html-webpack-plugin": "^2.30.1", + "ignore-styles": "^5.0.1", "jasmine": "^2.8.0", "jasmine-spec-reporter": "^4.2.1", "karma": "^1.7.1", @@ -63,6 +69,7 @@ "karma-jasmine": "^1.1.0", "karma-spec-reporter": "^0.0.31", "karma-webpack": "^2.0.5", + "mocha": "^4.0.1", "multer": "^1.3.0", "ng-router-loader": "^2.1.0", "node-sass": "^4.5.3", @@ -70,6 +77,7 @@ "portfinder": "^1.0.13", "protractor": "^5.2.0", "raw-loader": "^0.5.1", + "rewire": "^2.5.2", "rimraf": "^2.6.2", "rxjs": "^5.5.2", "sass-loader": "^6.0.6", diff --git a/src/ngx-uploader/classes/ngx-uploader.class.ts b/src/ngx-uploader/classes/ngx-uploader.class.ts index 3a7736f5..47896e36 100644 --- a/src/ngx-uploader/classes/ngx-uploader.class.ts +++ b/src/ngx-uploader/classes/ngx-uploader.class.ts @@ -248,6 +248,18 @@ export class NgUploaderService { return Math.random().toString(36).substring(7); } + setContentTypes(contentTypes: string[]): void { + if ( typeof contentTypes != 'undefined' && contentTypes instanceof Array ) { + if (contentTypes.find((type: string) => type === '*') !== undefined) { + this.contentTypes = ['*']; + } else { + this.contentTypes = contentTypes; + } + return; + } + this.contentTypes = ['*']; + } + private allContentTypesAllowed(): boolean { if (this.contentTypes.find((type: string) => type === '*') !== undefined) { return true; diff --git a/src/ngx-uploader/classes/ngx-uploader.spec.ts b/src/ngx-uploader/classes/ngx-uploader.spec.ts new file mode 100644 index 00000000..78cedc43 --- /dev/null +++ b/src/ngx-uploader/classes/ngx-uploader.spec.ts @@ -0,0 +1,78 @@ +import { NgUploaderService } from './ngx-uploader.class'; +import { expect } from 'chai'; +import 'mocha'; +import { describe } from 'selenium-webdriver/testing'; + +import rewire = require('rewire'); +let rewiredNgUploaderService = rewire('./ngx-uploader.class'); + +describe('NgUploaderService constructor', () => { + it('without parameters should return allowedContentTypes = [\'*\']', () => { + let uploader = new NgUploaderService(); + expect(uploader.contentTypes).to.have.lengthOf(1); + expect(uploader.contentTypes).to.be.an('array').that.contain('*'); + }); + + it('should return [\'image/jpeg\']', () => { + let uploader = new NgUploaderService(1, ['image/jpeg']); + expect(uploader.contentTypes).to.have.lengthOf(1); + expect(uploader.contentTypes).to.be.an('array').that.not.contain('*'); + expect(uploader.contentTypes).to.be.an('array').that.contains('image/jpeg'); + }); +}); + +describe('setContentTypes function', () => { + let uploader = new NgUploaderService(); + + it('should return [\'*\']', () => { + uploader.setContentTypes(['*']); + expect(uploader.contentTypes).to.have.lengthOf(1); + expect(uploader.contentTypes).to.be.an('array').that.contain('*'); + }); + + it('should return [\'image/jpeg\']', () => { + uploader.setContentTypes(['image/jpeg']); + expect(uploader.contentTypes).to.have.lengthOf(1); + expect(uploader.contentTypes).to.be.an('array').that.contain('image/jpeg'); + }); +}); + +describe('isContentTypeAllowed function', () => { + var private_uploader = rewiredNgUploaderService.__get__('NgUploaderService'); + + it('should return true', () => { + let uploader = new private_uploader(); + expect(uploader.isContentTypeAllowed('all/you-can-eat')).is.true; + }); + + it('should return true', () => { + let uploader = new private_uploader(1, ['image/jpeg', 'image/png', 'image/gif']); + expect(uploader.isContentTypeAllowed('image/jpeg')).is.true; + expect(uploader.isContentTypeAllowed('image/gif')).is.true; + expect(uploader.isContentTypeAllowed('image/png')).is.true; + }); + + it('should return false', () => { + let uploader = new private_uploader(1, ['image/jpeg', 'image/png', 'image/gif']); + expect(uploader.isContentTypeAllowed('image/webm')).is.false; + }); +}); + +describe('allContentTypesAllowed function', () => { + let rewiredUploader = rewiredNgUploaderService.__get__('NgUploaderService'); + + it('should return true', () => { + let uploader = new rewiredUploader(); + expect(uploader.allContentTypesAllowed()).is.true; + }); + + it('should return false', () => { + let uploader = new rewiredUploader(1, ['image/jpeg', 'image/png', 'image/gif']); + expect(uploader.allContentTypesAllowed()).is.false; + }); +}); + + + + +