diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5702678 --- /dev/null +++ b/.gitignore @@ -0,0 +1,65 @@ + +# Created by https://www.gitignore.io/api/node + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.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 + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# 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 + +# dotenv environment variables file +.env + + +# End of https://www.gitignore.io/api/node diff --git a/app/entry.js b/app/entry.js new file mode 100644 index 0000000..8527519 --- /dev/null +++ b/app/entry.js @@ -0,0 +1,61 @@ +'use strict'; + +require('./scss/reset.scss'); +require('./scss/main.scss'); + +const angular = require('angular'); +const cowsay = require('cowsay-browser'); + +const cowsayApp = angular.module('cowsayApp', []); + +cowsayApp.controller('CowsayController', ['$log', CowsayController]); + +function CowsayController($log) { + $log.debug('CowsayController'); + + this.title = 'Welcome to Cowville'; + this.history = []; + + cowsay.list((err, cowfiles) => { + this.cowfiles = cowfiles; + this.current = this.cowfiles[0]; + }); + + this.update = function(input) { + $log.debug('cowsayCtrl.update'); + return cowsay.say({ text: input || 'moo', f: this.current }); + }; + + this.speak = function(input) { + $log.debug('cowsayCtrl.speak'); + this.spoken = this.update(input); + this.history.push(this.spoken); + }; + + this.undo = function() { + $log.debug('cowsayCtrl.undo'); + this.history.pop(); + this.spoken = this.history.pop(); + }; +} + +cowsayApp.controller('NavController', ['$log', NavController]); + +function NavController($log) { + $log.debug('NavController'); + + this.routes = [ + { + name: 'home', + url: '/home' + }, + { + name: 'cow creator', + url: '/cowcreator' + }, + { + name: 'account', + url: '/myaccount' + } + ]; +} diff --git a/app/index.html b/app/index.html new file mode 100644 index 0000000..78b6f1a --- /dev/null +++ b/app/index.html @@ -0,0 +1,52 @@ + + + + Cowsville! + + +
+ +
+ +
+
+

{{ cowsayCtrl.title }}

+ +
+          {{ cowsayCtrl.update(cowsayCtrl.text) }}
+        
+ +
+ + + + + +
+ +
+              {{cowsayCtrl.spoken}}
+            
+ +
+
+
+
+ + diff --git a/app/scss/main.scss b/app/scss/main.scss new file mode 100644 index 0000000..e69de29 diff --git a/app/scss/reset.scss b/app/scss/reset.scss new file mode 100644 index 0000000..e69de29 diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..b174dbe --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,26 @@ +const webpackConfig = require('./webpack.config.js'); +webpackConfig.entry = {}; + +module.exports = function(config) { + config.set({ + webpack: webpackConfig, + basePath: '', + frameworks: ['jasmine'], + files: [ + 'test/**/*-test.js' + ], + exclude: [ + ], + preprocessors: { + 'test/**/*-test.js': ['webpack'] + }, + reporters: ['mocha'], + port: 9876, + colors: true, + logLevel: config.LOG_INFO, + autoWatch: true, + browsers: ['PhantomJS'], + singleRun: false, + concurrency: Infinity + }); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..c7ef3c6 --- /dev/null +++ b/package.json @@ -0,0 +1,47 @@ +{ + "name": "22-testing_controllers", + "version": "1.0.0", + "description": "![cf](https://i.imgur.com/7v5ASc8.png) Lab 22 - Testing Angular Controllers\r ======", + "main": "index.js", + "scripts": { + "build": "./node_modules/webpack/bin/webpack.js", + "watch": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot", + "test": "./node_modules/karma/bin/karma start", + "test-watch": "./node_modules/karma/bin/karma start --single-run" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/noahgribbin/22-testing_controllers.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/noahgribbin/22-testing_controllers/issues" + }, + "homepage": "https://github.com/noahgribbin/22-testing_controllers#readme", + "dependencies": { + "angular": "^1.6.3", + "babel-core": "^6.24.0", + "babel-loader": "^6.4.1", + "babel-preset-es2015": "^6.24.0", + "cowsay-browser": "^1.1.8", + "css-loader": "^0.27.3", + "extract-text-webpack-plugin": "^1.0.1", + "html-webpack-plugin": "^2.28.0", + "node-sass": "^4.5.1", + "sass-loader": "^6.0.3", + "style-loader": "^0.16.0", + "webpack": "^1.14.0" + }, + "devDependencies": { + "angular-mocks": "^1.6.3", + "jasmine-core": "^2.5.2", + "karma": "^1.5.0", + "karma-jasmine": "^1.1.0", + "karma-mocha-reporter": "^2.2.3", + "karma-phantomjs-launcher": "^1.0.4", + "karma-webpack": "^2.0.3", + "webpack-dev-server": "^1.16.2" + } +} diff --git a/test/cowsay-ctrl-test.js b/test/cowsay-ctrl-test.js new file mode 100644 index 0000000..683d2d4 --- /dev/null +++ b/test/cowsay-ctrl-test.js @@ -0,0 +1,58 @@ +'use strict'; + +require('./lib/test-setup.js'); + +const angular = require('angular'); +const cowsay = require('cowsay-browser'); + +describe('Cowsay Controller', function() { + beforeEach(() => { + angular.mock.module('cowsayApp'); + angular.mock.inject($controller => { + this.cowsayCtrl = new $controller('CowsayController'); + }); + }); + + describe('starting properties', () => { + it('title should be "Welcome to Cowville"', () => { + expect(this.cowsayCtrl.title).toBe('Welcome to Cowville'); + }); + it('history should be an empty array', () => { + expect(Array.isArray(this.cowsayCtrl.history)).toBe(true); + }); + it('cowfiles should be populated', () => { + cowsay.list((err, list) => { + expect(this.cowsayCtrl.cowfiles).toEqual(list); + }); + }); + it('current should be the first cowfile in the array', () => { + cowsay.list((err, list) => { + expect(this.cowsayCtrl.current).toEqual(list[0]); + }); + }); + }); + describe('#update', () => { + it('should return a cow that says "testing"', () => { + let expected = cowsay.say({text:'testing', f: this.cowsayCtrl.current}); + let result = this.cowsayCtrl.update('testing'); + expect(result).toEqual(expected); + }); + }); + describe('#speak', () => { + it('should expect spoken and history to equal a cowsay "testing"', () => { + let expected = cowsay.say({text:'testing', f: this.cowsayCtrl.current}); + this.cowsayCtrl.speak('testing'); + expect(this.cowsayCtrl.spoken).toEqual(expected); + expect(this.cowsayCtrl.history[0]).toEqual(expected); + }); + }); + describe('#undo', () => { + it('should pop off the second "speak" call, and return the first call', () => { + let expected = cowsay.say({text:'testing', f: this.cowsayCtrl.current}); + this.cowsayCtrl.speak('testing'); + this.cowsayCtrl.speak('testing to be popped off'); + this.cowsayCtrl.undo(); + expect(this.cowsayCtrl.spoken).toEqual(expected); + }); + }); +}); diff --git a/test/lib/test-setup.js b/test/lib/test-setup.js new file mode 100644 index 0000000..6368526 --- /dev/null +++ b/test/lib/test-setup.js @@ -0,0 +1,4 @@ +'use strict'; + +require('../../app/entry.js'); +require('angular-mocks'); diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..2d18951 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,35 @@ +'use strict'; + +const HTMLPlugin = require('html-webpack-plugin'); +const ExtractTextPlugin = require('extract-text-webpack-plugin'); + +module.exports = { + entry: `${__dirname}/app/entry.js`, + output: { + filename: 'bundle.js', + path: 'build' + }, + plugins: [ + new HTMLPlugin({ + template: `${__dirname}/app/index.html` + }), + new ExtractTextPlugin('bundle.css') + ], + module: { + loaders: [ + { + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel' + }, + { + test: /\.scss$/, + loader: 'style!css!sass' + }, + { + test: /\.(eot|woff|ttft|svg).*/, + loader: 'url?limit=10000&name=fonts/[hash].[ext]' + } + ] + } +};