Skip to content

Commit

Permalink
moca/chai tests and new NgUploaderService setContentTypes method
Browse files Browse the repository at this point in the history
  • Loading branch information
retailify authored and jkuri committed Nov 10, 2017
1 parent 53780d7 commit b5e8f6e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ lib/
*.ngfactory.ts
*.d.ts
yarn.lock
.idea
.idea/
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -56,20 +61,23 @@
"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",
"karma-chrome-launcher": "^2.2.0",
"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",
"nodemon": "^1.12.1",
"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",
Expand Down
12 changes: 12 additions & 0 deletions src/ngx-uploader/classes/ngx-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
78 changes: 78 additions & 0 deletions src/ngx-uploader/classes/ngx-uploader.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});





0 comments on commit b5e8f6e

Please sign in to comment.