From 32df85cf60f09509297e751547b161583ad5632b Mon Sep 17 00:00:00 2001 From: sueanyv Date: Tue, 28 Mar 2017 18:01:00 -0700 Subject: [PATCH 1/4] adding files and started styling --- .babelrc | 3 + .eslintrc | 22 +++++ .gitignore | 119 +++++++++++++++++++++++++ app/component/login/_login.scss | 0 app/component/login/login.html | 32 +++++++ app/component/login/login.js | 27 ++++++ app/component/signup/_signup.scss | 0 app/component/signup/signup.html | 39 ++++++++ app/component/signup/signup.js | 25 ++++++ app/config/router-config.js | 31 +++++++ app/entry.js | 39 ++++++++ app/index.html | 12 +++ app/scss/main.scss | 3 + app/service/auth-service.js | 88 ++++++++++++++++++ app/view/home/_home.scss | 0 app/view/home/home-controller.js | 9 ++ app/view/home/home.html | 3 + app/view/landing/_landing.scss | 0 app/view/landing/landing-controller.js | 10 +++ app/view/landing/landing.html | 25 ++++++ package.json | 42 +++++++++ webpack.config.js | 42 +++++++++ 22 files changed, 571 insertions(+) create mode 100644 .babelrc create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 app/component/login/_login.scss create mode 100644 app/component/login/login.html create mode 100644 app/component/login/login.js create mode 100644 app/component/signup/_signup.scss create mode 100644 app/component/signup/signup.html create mode 100644 app/component/signup/signup.js create mode 100644 app/config/router-config.js create mode 100644 app/entry.js create mode 100644 app/index.html create mode 100644 app/scss/main.scss create mode 100644 app/service/auth-service.js create mode 100644 app/view/home/_home.scss create mode 100644 app/view/home/home-controller.js create mode 100644 app/view/home/home.html create mode 100644 app/view/landing/_landing.scss create mode 100644 app/view/landing/landing-controller.js create mode 100644 app/view/landing/landing.html create mode 100644 package.json create mode 100644 webpack.config.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..c13c5f62 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..9fff93a5 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,22 @@ + +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..abb1a67c --- /dev/null +++ b/.gitignore @@ -0,0 +1,119 @@ +# Created by https://www.gitignore.io/api/node,vim,osx,macos,linux + +*node_modules +.env +build +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + + + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon +# Thumbnails +._* +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### macOS ### +# Icon must end with two \r +# Thumbnails +# Files that might appear in the root of a volume +# Directories potentially created on remote AFP share + + +### Linux ### + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# End of https://www.gitignore.io/api/node,vim,osx,macos,linux +l diff --git a/app/component/login/_login.scss b/app/component/login/_login.scss new file mode 100644 index 00000000..e69de29b diff --git a/app/component/login/login.html b/app/component/login/login.html new file mode 100644 index 00000000..97b0b545 --- /dev/null +++ b/app/component/login/login.html @@ -0,0 +1,32 @@ +
+
+
+ +
+ +
+ +
+ + +
+
+ diff --git a/app/component/login/login.js b/app/component/login/login.js new file mode 100644 index 00000000..77c2c83e --- /dev/null +++ b/app/component/login/login.js @@ -0,0 +1,27 @@ +'use strict'; + +require('./_login.scss'); + +module.exports = { + template: require('./login.html'), + controller: ['$log', '$location', 'authService', LoginController], + controllerAs: 'loginCtrl' +}; + +function LoginController($log, $location, authService) { + $log.debug('LoginController'); + + authService.getToken() + .then( () => { + $location.url('/home'); + }); + + this.login = function() { + $log.debug('loginCtrl.login'); + + authService.login(this.user) + .then( () => { + $location.url('/home'); + }); + }; +} diff --git a/app/component/signup/_signup.scss b/app/component/signup/_signup.scss new file mode 100644 index 00000000..e69de29b diff --git a/app/component/signup/signup.html b/app/component/signup/signup.html new file mode 100644 index 00000000..92095966 --- /dev/null +++ b/app/component/signup/signup.html @@ -0,0 +1,39 @@ + + + + + + routes app + + +
+

cfgram

+
+
+ +
+ + + + diff --git a/app/component/signup/signup.js b/app/component/signup/signup.js new file mode 100644 index 00000000..2f58215c --- /dev/null +++ b/app/component/signup/signup.js @@ -0,0 +1,25 @@ +'use strict'; + +module.exports = { + template: require('./signup.html'), + controller: ['$log', '$location', 'authService', SignupController], + controllerAs: 'signupCtrl' +}; + +function SignupController($log, $location, authService) { + $log.debug('SignupController'); + + authService.getToken() + .then( () => { + $location.url('/home'); + }); + + this.signup = function(user) { + $log.debug('signupCtrl.signup'); + + authService.signup(user) + .then( () => { + $location.url('/home'); + }); + }; +} diff --git a/app/config/router-config.js b/app/config/router-config.js new file mode 100644 index 00000000..31d817b6 --- /dev/null +++ b/app/config/router-config.js @@ -0,0 +1,31 @@ +'use strict'; + +module.exports = ['$stateProvider', '$urlRouterProvider', routerConfig]; + +function routerConfig($stateProvider, $urlRouterProvider) { + $urlRouterProvider.when('', '/join#signup'); + $urlRouterProvider.when('/', '/join#signup'); + $urlRouterProvider.when('/signup', '/join#signup'); + $urlRouterProvider.when('/login', '/join#login'); + + let states = [ + { + name: 'home', + url: '/home', + template: require('../view/home/home.html'), + controller: 'HomeController', + controllerAs: 'homeCtrl' + }, + { + name: 'landing', + url: '/join', + template: require('../view/landing/landing.html'), + controller: 'LandingController', + controllerAs: 'landingCtrl' + } + ]; + + states.forEach( state => { + $stateProvider.state(state); + }); +} diff --git a/app/entry.js b/app/entry.js new file mode 100644 index 00000000..358681e9 --- /dev/null +++ b/app/entry.js @@ -0,0 +1,39 @@ +'use strict'; + +require('./scss/main.scss'); + +const path = require('path'); +const angular = require('angular'); +const camelcase = require('camelcase'); +const pascalcase = require('pascalcase'); +const uiRouter = require('angular-ui-router'); +const ngTouch = require('angular-touch'); +const ngAnimate = require('angular-animate'); + +const cfgram = angular.module('cfgram', [ngTouch, ngAnimate, uiRouter]); + +let context = require.context('./config/', true, /\.js$/); +context.keys().forEach( key => { + cfgram.config(context(key)); +}); + +context = require.context('./view/', true, /\.js$/); +context.keys().forEach( key => { + let name = pascalcase(path.basename(key, '.js')); + let module = context(key); + cfgram.controller(name, module); +}); + +context = require.context('./service/', true, /\.js$/); +context.keys().forEach( key => { + let name = camelcase(path.basename(key, '.js')); + let module = context(key); + cfgram.service(name, module); +}); + +context = require.context('./component/', true, /\.js$/); +context.keys().forEach( key => { + let name = camelcase(path.basename(key, '.js')); + let module = context(key); + cfgram.component(name, module); +}); diff --git a/app/index.html b/app/index.html new file mode 100644 index 00000000..dff91bf6 --- /dev/null +++ b/app/index.html @@ -0,0 +1,12 @@ + + + + + + cfgram. + + + + + + diff --git a/app/scss/main.scss b/app/scss/main.scss new file mode 100644 index 00000000..76b442f5 --- /dev/null +++ b/app/scss/main.scss @@ -0,0 +1,3 @@ +body { + background: lightgrey; +} diff --git a/app/service/auth-service.js b/app/service/auth-service.js new file mode 100644 index 00000000..1f748844 --- /dev/null +++ b/app/service/auth-service.js @@ -0,0 +1,88 @@ +'use strict'; + +module.exports = ['$q', '$log', '$http', '$window', authService]; + +function authService($q, $log, $http, $window) { + $log.debug('authService'); + + let service = {}; + let token = null; + + function setToken(_token) { + $log.debug('authService.setToken'); + + if (! _token) { + return $q.reject(new Error('no token')); + } + + $window.localStorage.setItem('token', _token); + token = _token; + return $q.resolve(token); + } + + service.getToken = function() { + $log.debug('authService.getToken'); + if (token) { + return $q.resolve(token); + } + + token = $window.localStorage.getItem('token'); + if (token) return $q.resolve(token); + return $q.reject(new Error('token not found')); + }; + + service.signup = function(user) { + $log.debug('authService.signup'); + + let url = `${__API_URL__}/api/signup`; + let config = { + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }; + + return $http.post(url, user, config) + .then( res => { + $log.log('success:', res.data); + return setToken(res.data); + }) + .catch( err => { + $log.error('failure:', err.message); + return $q.reject(err); + }); + }; + + service.logout = function() { + $log.debug('authService.logout'); + + $window.localStorage.removeItem('token'); + token = null; + return $q.resolve(); + }; + + service.login = function(user) { + $log.debug('authService.login'); + + let url = `${__API_URL__}/api/login`; + let base64 = $window.btoa(`${user.username}:${user.password}`); + let config = { + headers: { + Accept: 'application/json', + Authorization: `Basic ${base64}` + } + }; + + return $http.get(url, config) + .then( res => { + $log.log('success', res.data); + return setToken(res.data); + }) + .catch( err => { + $log.error(err.message); + return $q.reject(err); + }); + }; + + return service; +} diff --git a/app/view/home/_home.scss b/app/view/home/_home.scss new file mode 100644 index 00000000..e69de29b diff --git a/app/view/home/home-controller.js b/app/view/home/home-controller.js new file mode 100644 index 00000000..dd9d54bf --- /dev/null +++ b/app/view/home/home-controller.js @@ -0,0 +1,9 @@ +'use strict'; + +require('./_home.scss'); + +module.exports = ['$log', HomeController]; + +function HomeController($log) { + $log.debug('HomeController'); +} diff --git a/app/view/home/home.html b/app/view/home/home.html new file mode 100644 index 00000000..eeeb0858 --- /dev/null +++ b/app/view/home/home.html @@ -0,0 +1,3 @@ +
+

welcome home!

+
diff --git a/app/view/landing/_landing.scss b/app/view/landing/_landing.scss new file mode 100644 index 00000000..e69de29b diff --git a/app/view/landing/landing-controller.js b/app/view/landing/landing-controller.js new file mode 100644 index 00000000..ad4e3b7e --- /dev/null +++ b/app/view/landing/landing-controller.js @@ -0,0 +1,10 @@ +'use strict'; + +require('./_landing.scss'); + +module.exports = ['$log', '$location', '$rootScope', 'authService', LandingController]; + +function LandingController($log, $location, authService) { + let url = $location.url(); + this.showSignup = url === '/join#signup' || url === '/join'; +} diff --git a/app/view/landing/landing.html b/app/view/landing/landing.html new file mode 100644 index 00000000..efa4547a --- /dev/null +++ b/app/view/landing/landing.html @@ -0,0 +1,25 @@ +
+
+
+ +

already a member?

+ + sign in here. + +
+
+ +
+
+ +

want to sign up?

+ + sign up here. + +
+
+
diff --git a/package.json b/package.json new file mode 100644 index 00000000..37b4ace9 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "cfgram-auth", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "build": "./node_modules/webpack/bin/webpack.js", + "watch": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "angular": "^1.6.3", + "angular-animate": "^1.6.3", + "angular-route": "^1.6.3", + "angular-touch": "^1.6.3", + "angular-ui-router": "^0.4.2", + "babel-core": "^6.24.0", + "babel-loader": "^6.4.1", + "babel-preset-es2015": "^6.24.0", + "camelcase": "^4.0.0", + "clean-webpack-plugin": "^0.1.16", + "css-loader": "^0.27.3", + "dotenv": "^4.0.0", + "extract-text-webpack-plugin": "^2.1.0", + "file-loader": "^0.10.1", + "html-loader": "^0.4.5", + "html-webpack-plugin": "^2.28.0", + "node-sass": "^4.5.1", + "pascalcase": "^0.1.1", + "resolve-url-loader": "^2.0.2", + "sass-loader": "^6.0.3", + "style-loader": "^0.16.1", + "ui-router": "^1.0.0-alpha.3", + "url-loader": "^0.5.8", + "webpack": "^2.3.2" + }, + "devDependencies": { + "webpack-dev-server": "^2.4.2" + } +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 00000000..5125b9f7 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,42 @@ +'use strict'; + +require('dotenv').load(); + +const webpack = require('webpack'); +const HTMLPlugin = require('html-webpack-plugin'); +const ExtractTextPlugin = require('extract-text-webpack-plugin'); + +module.exports = { + devtool: 'eval', + entry: `${__dirname}/app/entry.js`, + output: { + filename: 'bundle.js', + path: `${__dirname}/build` + }, + plugins: [ + new HTMLPlugin({ + template: `${__dirname}/app/index.html` + }), + new ExtractTextPlugin('bundle.css'), + new webpack.DefinePlugin({ + __API_URL__: JSON.stringify(process.env.API_URL) + }) + ], + module: { + rules: [ + { + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader' + }, + { + test: /\.html$/, + loader: 'html-loader' + }, + { + test: /\.scss$/, + loader: ExtractTextPlugin.extract(['css-loader', 'sass-loader']) + } + ] + } +}; From ea99a974f76d5e211f0ed9a00c9a7d331b39a7ea Mon Sep 17 00:00:00 2001 From: sueanyv Date: Tue, 4 Apr 2017 19:00:00 -0700 Subject: [PATCH 2/4] adding files --- test/auth-service-test.js | 32 +++++++++++++++++++++++++ test/example-test.js | 7 ++++++ test/gallery-service-test.js | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 test/auth-service-test.js create mode 100644 test/example-test.js create mode 100644 test/gallery-service-test.js diff --git a/test/auth-service-test.js b/test/auth-service-test.js new file mode 100644 index 00000000..f5b4af04 --- /dev/null +++ b/test/auth-service-test.js @@ -0,0 +1,32 @@ + +'use strict'; + +describe('Auth Service', function() { + + beforeEach(() => { + angular.mock.module('cfgram'); + angular.mock.inject(($rootScope, authService, $window, $httpBackend) => { + this.$window = $window; + this.$rootScope = $rootScope; + this.authService = authService; + this.$httpBackend = $httpBackend; + }); + }); + + describe('authService.getToken', () => { + it('should return a token', () => { + this.authService.token = null; + this.$window.localStorage.setItem('token', 'test token'); + + this.authService.getToken() + .then( token => { + expect(token).toEqual('test token'); + }) + .catch( err => { + expect(err).toEqual(null); + }); + + this.$rootScope.$apply(); + }); + }); +}); diff --git a/test/example-test.js b/test/example-test.js new file mode 100644 index 00000000..23ae19f5 --- /dev/null +++ b/test/example-test.js @@ -0,0 +1,7 @@ +'use strict'; + +describe('Example Test', function() { + it('should pass this test', () => { + expect(true).toEqual(true); + }); +}); diff --git a/test/gallery-service-test.js b/test/gallery-service-test.js new file mode 100644 index 00000000..b98a7e75 --- /dev/null +++ b/test/gallery-service-test.js @@ -0,0 +1,45 @@ +'use strict'; + +describe('Gallery Service', function() { + + beforeEach(() => { + angular.mock.module('cfgram'); + angular.mock.inject(($rootScope, authService, galleryService, $window, $httpBackend) => { + this.$window = $window; + this.$rootScope = $rootScope; + this.authService = authService; + this.galleryService = galleryService; + this.$httpBackend = $httpBackend; + }); + }); + + describe('galleryService.createGallery', () => { + it('should create a new gallery', () => { + let galleryData = { + name: 'example gallery', + desc: 'example description' + }; + + let headers = { + 'Content-Type': 'application/json', + Accept: 'application/json', + Authorization: 'Bearer test token' + }; + + this.$httpBackend.expectPOST('http://localhost:8000/api/gallery', galleryData, headers) + .respond(200, { + _id: '1234', + username: 'testuser', + name: galleryData.name, + desc: galleryData.desc, + pics: [] + }); + + this.galleryService.createGallery(galleryData); + this.$httpBackend.flush(); + this.$rootScope.$apply(); + }); + }); + + // create another test for deleting a gallery +}); From f94164150a559ce8b03bb3d2c2f398650614916a Mon Sep 17 00:00:00 2001 From: sueanyv Date: Wed, 5 Apr 2017 23:17:47 -0700 Subject: [PATCH 3/4] lab --- karma.conf.js | 51 +++++++++++++++++++++++ package.json | 12 +++++- test/edit-gallery-component-test.js | 63 +++++++++++++++++++++++++++++ test/gallery-item-component-test.js | 62 ++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 karma.conf.js create mode 100644 test/edit-gallery-component-test.js create mode 100644 test/gallery-item-component-test.js diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 00000000..7541585f --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,51 @@ + +const webpack = require('./webpack.config.js'); +delete webpack.entry; + +module.exports = function(config) { + config.set({ + webpack, + basePath: '', + + + frameworks: ['jasmine'], + + + files: [ + 'app/entry.js', + 'test/**/*-test.js', + 'node_modules/angular-mocks/angular-mocks.js' + ], + + + exclude: [ + ], + + + preprocessors: { + 'test/**/*-test.js': ['webpack'], + 'app/entry.js': ['webpack'] + }, + + + reporters: ['mocha'], + + + port: 9876, + colors: true, + + + logLevel: config.LOG_INFO, + + + autoWatch: true, + + + browsers: ['Chrome'], + + + singleRun: false, + + concurrency: Infinity + }); +}; diff --git a/package.json b/package.json index 37b4ace9..850ec2b3 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,9 @@ "main": "index.js", "scripts": { "build": "./node_modules/webpack/bin/webpack.js", - "watch": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot" + "build-watch": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot", + "test": "./node_modules/karma/bin/karma start --single-run", + "test-watch": "./node_modules/karma/bin/karma start" }, "keywords": [], "author": "", @@ -27,6 +29,7 @@ "file-loader": "^0.10.1", "html-loader": "^0.4.5", "html-webpack-plugin": "^2.28.0", + "ng-file-upload": "^12.2.13", "node-sass": "^4.5.1", "pascalcase": "^0.1.1", "resolve-url-loader": "^2.0.2", @@ -37,6 +40,13 @@ "webpack": "^2.3.2" }, "devDependencies": { + "angular-mocks": "^1.6.4", + "jasmine-core": "^2.5.2", + "karma": "^1.5.0", + "karma-chrome-launcher": "^2.0.0", + "karma-jasmine": "^1.1.0", + "karma-mocha-reporter": "^2.2.3", + "karma-webpack": "^2.0.3", "webpack-dev-server": "^2.4.2" } } diff --git a/test/edit-gallery-component-test.js b/test/edit-gallery-component-test.js new file mode 100644 index 00000000..a8fe0988 --- /dev/null +++ b/test/edit-gallery-component-test.js @@ -0,0 +1,63 @@ + +'use strict'; + +describe('Edit Gallery Component', function(){ + + beforeEach(() => { + angular.mock.module('cfgram'); + angular.mock.inject(($rootScope, $componentController, $httpBackend, authService) => { + this.$rootScope = $rootScope; + this.$componentController = $componentController; + this.$httpBackend = $httpBackend; + this.authService = authService; + }); + }); + + it('should contain the proper component bindings', () => { + let mockBindings = { + gallery: { + name: 'test gallery name', + desc: 'test gallery description' + } + }; + + let editGalleryCtrl = this.$componentController('editGallery', null, mockBindings); + expect(editGalleryCtrl.gallery.name).toEqual(mockBindings.gallery.name); + expect(editGalleryCtrl.gallery.desc).toEqual(mockBindings.gallery.desc); + + this.$rootScope.$apply(); + }); + + describe('editGalleryCtrl.updateGallery()', () => { + it('should make a valid PUT request', () => { + let url = 'http://localhost:8000/api/gallery/12345'; + let headers = { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + Authorization: 'Bearer test token' + }; + + this.$httpBackend.expectPUT(url, { + _id: '12345', + name: 'updated name', + desc: 'updated description' + }, headers).respond(200); + + let mockBindings = { + gallery: { + _id: '12345', + name: 'updated name', + desc: 'updated description' + } + }; + + let editGalleryCtrl = this.$componentController('editGallery', null, mockBindings); + editGalleryCtrl.gallery.name = 'updated name'; + editGalleryCtrl.gallery.desc = 'updated description'; + editGalleryCtrl.updateGallery(); + + this.$httpBackend.flush(); + this.$rootScope.$apply(); + }); + }); +}); diff --git a/test/gallery-item-component-test.js b/test/gallery-item-component-test.js new file mode 100644 index 00000000..9070cd5b --- /dev/null +++ b/test/gallery-item-component-test.js @@ -0,0 +1,62 @@ +'use strict'; + +describe('Gallery Item Component', function(){ + beforeEach(() => { + angular.mock.module('cfgram'); + angular.mock.inject(($rootScope, $componentController, $httpBackend, authService) => { + this.$rootScope = $rootScope; + this.$componentController = $componentController; + this.$httpBackend = $httpBackend; + this.authService = authService; + }); + }); + + describe('galleryItemCtrl.deleteDone()', () => { + it('should call deleteDone', () => { + let mockBindings = { + gallery: { + _id: '12345', + name: 'test name', + desc: 'test description', + pics: [] + }, + deleteDone: function(data){ + expect(data.galleryData._id).toEqual('12345'); + } + }; + + let galleryItemCtrl = this.$componentController('galleryItem', null, mockBindings); + galleryItemCtrl.deleteDone({galleryData: galleryItemCtrl.gallery}); + + this.$rootScope.$apply(); + }); + }); + + it('should call deleteDone with gallery after galleryDelete', () => { + let url = 'http://localhost:8000/api/gallery/12345'; + let headers = { + Authorization: 'Bearer test token', + Accept: 'application/json, text/plain, */*' + }; + + let mockBindings = { + gallery: { + _id: '12345', + name: 'test name', + desc: 'test description', + pics: [] + }, + deleteDone: function(data){ + expect(data.galleryData._id).toEqual(mockBindings.gallery._id); + } + }; + + this.$httpBackend.expectDELETE(url, headers).respond(204); + + let galleryItemCtrl = this.$componentController('galleryItem', null, mockBindings); + galleryItemCtrl.deleteGallery(); + + this.$httpBackend.flush(); + this.$rootScope.$apply(); + }); +}); From c5d02e58d767dce4cce6b43dd2174dfc43519291 Mon Sep 17 00:00:00 2001 From: sueanyv Date: Thu, 6 Apr 2017 23:15:21 -0700 Subject: [PATCH 4/4] adding filter folder and file --- filter/gallery-search.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 filter/gallery-search.js diff --git a/filter/gallery-search.js b/filter/gallery-search.js new file mode 100644 index 00000000..32609edd --- /dev/null +++ b/filter/gallery-search.js @@ -0,0 +1,17 @@ +'use strict'; + +module.exports = function() { + return function(galleries, searchTerm) { + let fuzzyRegex = generateFuzzyRegex(searchTerm); + + return galleries.filter(gallery => { + return fuzzyRegex.test(gallery.name.toUpperCase()); + }); + }; +}; + +function generateFuzzyRegex(input) { + if (!input) return /.*/; + let fuzzyString = '.*' + input.toUpperCase().split('').join('.*') + '.*'; + return new RegExp(fuzzyString); +}