This repository has been archived on 2020-10-11. You can view files and clone it, but cannot push or open issues or pull requests.
angularjs-batarang/test/mock/ChromeMock.js
2012-07-02 14:09:51 -07:00

55 lines
1.1 KiB
JavaScript

function createChromeExtensionMock() {
var extend = function(obj, source) {
for (var prop in source) {
obj[prop] = source[prop];
}
return obj;
};
// TODO: rename the "jQuery" stuff
var jQueryResult = [];
var defaultMock = {
document: {
getElementsByClassName: function (arg) {
if (arg === 'ng-scope') {
return jQueryResult;
}
throw new Error('unknown selector');
}
}
};
var windowMock = defaultMock;
return {
eval: function (fn, args, cb) {
if (!cb && typeof args === 'function') {
cb = args;
args = {};
} else if (!args) {
args = {};
}
var res = fn(windowMock, args);
if (typeof cb === 'function') {
cb(res);
}
},
__registerWindow: function (win) {
windowMock = extend(windowMock, win);
},
__registerQueryResult: function (res) {
jQueryResult = res;
jQueryResult.each = function (fn) {
var i;
for (i = 0; i < this.length; i++) {
fn(i, this[i]);
}
};
},
sendRequest: jasmine.createSpy('sendRequest')
};
}