Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngeorgewright committed Jun 23, 2014
1 parent e22eb82 commit 8f231bd
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
bower_components
*.log
node_modules
*.swp
Expand Down
61 changes: 45 additions & 16 deletions 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));

3 changes: 2 additions & 1 deletion angular-ie7-support.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bower.json
Expand Up @@ -18,5 +18,9 @@
"Gruntfile.coffee",
"package.json",
"node_modules"
]
],
"devDependencies": {
"angular-mocks": "~1.2.18",
"jasmine-sinon": "~0.3.0"
}
}
10 changes: 8 additions & 2 deletions package.json
Expand Up @@ -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",
Expand All @@ -21,7 +22,12 @@
"author": "John Wright <johngeorge.wright@gmail.com>",
"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"
}
}
97 changes: 97 additions & 0 deletions 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);
});

});

});

});

72 changes: 72 additions & 0 deletions 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
});
};

0 comments on commit 8f231bd

Please sign in to comment.