From 8f231bd6eda10241b8def7e31528ee9aeb690f08 Mon Sep 17 00:00:00 2001 From: John Wright Date: Mon, 23 Jun 2014 13:03:09 +0100 Subject: [PATCH] Added tests. --- .gitignore | 1 + angular-ie7-support.js | 61 ++++++++++++++------ angular-ie7-support.min.js | 3 +- bower.json | 6 +- package.json | 10 +++- test/angular-ie7-support.spec.js | 97 ++++++++++++++++++++++++++++++++ test/unit.conf.js | 72 ++++++++++++++++++++++++ 7 files changed, 230 insertions(+), 20 deletions(-) create mode 100644 test/angular-ie7-support.spec.js create mode 100644 test/unit.conf.js diff --git a/.gitignore b/.gitignore index 12fa7e0..151e453 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +bower_components *.log node_modules *.swp diff --git a/angular-ie7-support.js b/angular-ie7-support.js index 94da3e4..bfb719b 100644 --- a/angular-ie7-support.js +++ b/angular-ie7-support.js @@ -1,32 +1,61 @@ (function (ng) { - function switchSce(ie7Config, $sceProvider) { - $sceProvider.enabled(!ie7Config.on); + function ie7ConfigProvider() { + this._hash = {}; + this.init(); } - function fixAnimation(ie7Config, $$asyncCallback, $animate) { + ie7ConfigProvider.prototype.init = function () { + var rootElement = document.getElementById('ng-app'); + this.set('enabled', !!ng.element(rootElement).length); + }; + + ie7ConfigProvider.prototype.set = function (prop, val) { + this._hash[prop] = val; + }; + + ie7ConfigProvider.prototype.get = function (prop) { + return this._hash[prop]; + }; + + ie7ConfigProvider.prototype.$get = function () { + return this._hash; + }; + + function switchSce(ie7ConfigProvider, $sceProvider) { + var enable = $sceProvider.enabled(); + if (ie7ConfigProvider.get('enabled')) { + enable = false; + } + $sceProvider.enabled(enable); + } + + function $animatePatch(ie7Config, $$asyncCallback, $animate) { function async(fn) { - return fn && $$asyncCallback(fn); + fn && $$asyncCallback(fn); } - if (ie7Config.on) { - $animate.addClass = function (element, className, done) { - ng.element(element).addClass(className); - async(done); - }; + function addClass(element, className, done) { + ng.element(element).addClass(className); + async(done); + } + + function removeClass(element, className, done) { + ng.element(element).removeClass(className); + async(done); + } - $animate.removeClass = function (element, className, done) { - ng.element(element).removeClass(className); - async(done); - }; + if (ie7Config.enabled) { + $animate.addClass = addClass; + $animate.removeClass = removeClass; } } ng .module('ie7-support', []) - .constant('ie7Config', { on: !!document.getElementById('ng-app') }) - .config(['ie7Config', '$sceProvider', switchSce]) - .run(['ie7Config', '$$asyncCallback', '$animate', fixAnimation]); + .provider('ie7Config', ie7ConfigProvider) + .config(['ie7ConfigProvider', '$sceProvider', switchSce]) + .run(['ie7Config', '$$asyncCallback', '$animate', $animatePatch]); }(angular)); diff --git a/angular-ie7-support.min.js b/angular-ie7-support.min.js index 722332f..8051cec 100644 --- a/angular-ie7-support.min.js +++ b/angular-ie7-support.min.js @@ -1 +1,2 @@ -(function(a){a.module("ie7-support",[]).constant("ie7Config",{on:!!document.getElementById("ng-app")}).config(["ie7Config","$sceProvider",function(a,c){c.enabled(!a.on)}]).run(["ie7Config","$$asyncCallback","$animate",function(g,c,f){g&&(f.addClass=function(d,e,b){a.element(d).addClass(e);b&&c(b)},f.removeClass=function(d,e,b){a.element(d).removeClass(e);b&&c(b)})}])})(angular); +(function(d){function b(){this._hash={};this.init()}b.prototype.init=function(){var a=document.getElementById("ng-app");this.set("enabled",!!d.element(a).length)};b.prototype.set=function(a,e){this._hash[a]=e};b.prototype.get=function(a){return this._hash[a]};b.prototype.$get=function(){return this._hash};d.module("ie7-support",[]).provider("ie7Config",b).config(["ie7ConfigProvider","$sceProvider",function(a,e){var b=e.enabled();a.get("enabled")&&(b=!1);e.enabled(b)}]).run(["ie7Config","$$asyncCallback", +"$animate",function(a,b,g){function h(a,f,c){d.element(a).addClass(f);c&&b(c)}function k(a,f,c){d.element(a).removeClass(f);c&&b(c)}a.enabled&&(g.addClass=h,g.removeClass=k)}])})(angular); diff --git a/bower.json b/bower.json index c1a800e..03bd5e1 100644 --- a/bower.json +++ b/bower.json @@ -18,5 +18,9 @@ "Gruntfile.coffee", "package.json", "node_modules" - ] + ], + "devDependencies": { + "angular-mocks": "~1.2.18", + "jasmine-sinon": "~0.3.0" + } } diff --git a/package.json b/package.json index 0711b7a..305cc91 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "url": "https://github.com/johngeorgewright/angular-ie7-support/issues" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "postinstall": "bower install" }, "keywords": [ "angular", @@ -21,7 +22,12 @@ "author": "John Wright ", "license": "MIT", "devDependencies": { + "chai": "^1.9.1", "grunt": "^0.4.5", - "grunt-http": "^1.4.0" + "grunt-http": "^1.4.0", + "karma": "^0.12.16", + "karma-jasmine": "^0.1.5", + "karma-phantomjs-launcher": "^0.1.4", + "mocha": "^1.20.1" } } diff --git a/test/angular-ie7-support.spec.js b/test/angular-ie7-support.spec.js new file mode 100644 index 0000000..024f315 --- /dev/null +++ b/test/angular-ie7-support.spec.js @@ -0,0 +1,97 @@ +describe('angular-ie7-support', function () { + + var ngElementStub; + + function enableIE7() { + toggleIE7(true); + } + + function disableIE7() { + toggleIE7(false); + } + + function toggleIE7(on) { + var appEl = document.getElementById('ng-app'), + ie7App = on ? [0] : []; + ngElementStub = sinon.stub(angular, 'element'); + ngElementStub.withArgs(appEl).returns(ie7App); + } + + beforeEach(module('ie7-support')); + + afterEach(function () { + angular.element.restore && angular.element.restore(); + }); + + describe('SCE', function () { + + describe('when enabled', function () { + + beforeEach(enableIE7); + + it('should have turned it off by default', inject(['$sce', function ($sce) { + expect($sce.isEnabled()).toBe(false); + }])); + + }); + + describe('when disabled', function () { + + beforeEach(disableIE7); + + it('should leave it to whatever it was before', inject(['$sce', function ($sce) { + expect($sce.isEnabled()).toBe(true); + }])); + + }); + + }); + + describe('$animate', function () { + + var addClass, removeClass, call; + + function stub() { + addClass = sinon.spy(); + removeClass = sinon.spy(); + ngElementStub.withArgs('mung').returns({ + addClass: addClass, + removeClass: removeClass + }); + } + + call = inject(['$animate', function ($animate) { + $animate.addClass('mung', 'some class'); + $animate.removeClass('mung', 'some other class'); + }]); + + describe('when enabled', function () { + + beforeEach(enableIE7); + beforeEach(stub); + beforeEach(call); + + it("will construct an element and call it's methods", function () { + expect(addClass).toHaveBeenCalledWithExactly('some class'); + expect(removeClass).toHaveBeenCalledWithExactly('some other class'); + }); + + }); + + describe('when disabled', function () { + + beforeEach(disableIE7); + beforeEach(stub); + beforeEach(call); + + it("will fall back", function () { + expect(addClass.callCount).toBe(0); + expect(removeClass.callCount).toBe(0); + }); + + }); + + }); + +}); + diff --git a/test/unit.conf.js b/test/unit.conf.js new file mode 100644 index 0000000..98b0f08 --- /dev/null +++ b/test/unit.conf.js @@ -0,0 +1,72 @@ +// Karma configuration +// Generated on Mon Jun 23 2014 10:57:54 GMT+0100 (BST) + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '..', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['jasmine'], + + + // list of files / patterns to load in the browser + files: [ + 'bower_components/angular/angular.js', + 'bower_components/angular-mocks/angular-mocks.js', + 'bower_components/sinonjs/sinon.js', + 'bower_components/jasmine-sinon/lib/jasmine-sinon.js', + 'angular-ie7-support.js', + 'test/**/*.spec.js' + ], + + + // list of files to exclude + exclude: [ + '**/*.swp' + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + + }, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['PhantomJS'], + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false + }); +};