123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465 |
- /**
- * @license AngularJS v1.6.5
- * (c) 2010-2017 Google, Inc. http://angularjs.org
- * License: MIT
- */
- (function (window, angular) {
- 'use strict';
- /**
- * @ngdoc object
- * @name angular.mock
- * @description
- *
- * Namespace from 'angular-mocks.js' which contains testing related code.
- *
- */
- angular.mock = {};
- /**
- * ! This is a private undocumented service !
- *
- * @name $browser
- *
- * @description
- * This service is a mock implementation of {@link ng.$browser}. It provides fake
- * implementation for commonly used browser apis that are hard to test, e.g. setTimeout, xhr,
- * cookies, etc.
- *
- * The api of this service is the same as that of the real {@link ng.$browser $browser}, except
- * that there are several helper methods available which can be used in tests.
- */
- angular.mock.$BrowserProvider = function () {
- this.$get = function () {
- return new angular.mock.$Browser();
- };
- };
- angular.mock.$Browser = function () {
- var self = this;
- this.isMock = true;
- self.$$url = 'http://server/';
- self.$$lastUrl = self.$$url; // used by url polling fn
- self.pollFns = [];
- // Testability API
- var outstandingRequestCount = 0;
- var outstandingRequestCallbacks = [];
- self.$$incOutstandingRequestCount = function () {
- outstandingRequestCount++;
- };
- self.$$completeOutstandingRequest = function (fn) {
- try {
- fn();
- } finally {
- outstandingRequestCount--;
- if (!outstandingRequestCount) {
- while (outstandingRequestCallbacks.length) {
- outstandingRequestCallbacks.pop()();
- }
- }
- }
- };
- self.notifyWhenNoOutstandingRequests = function (callback) {
- if (outstandingRequestCount) {
- outstandingRequestCallbacks.push(callback);
- } else {
- callback();
- }
- };
- // register url polling fn
- self.onUrlChange = function (listener) {
- self.pollFns.push(
- function () {
- if (self.$$lastUrl !== self.$$url || self.$$state !== self.$$lastState) {
- self.$$lastUrl = self.$$url;
- self.$$lastState = self.$$state;
- listener(self.$$url, self.$$state);
- }
- }
- );
- return listener;
- };
- self.$$applicationDestroyed = angular.noop;
- self.$$checkUrlChange = angular.noop;
- self.deferredFns = [];
- self.deferredNextId = 0;
- self.defer = function (fn, delay) {
- // Note that we do not use `$$incOutstandingRequestCount` or `$$completeOutstandingRequest`
- // in this mock implementation.
- delay = delay || 0;
- self.deferredFns.push({time: (self.defer.now + delay), fn: fn, id: self.deferredNextId});
- self.deferredFns.sort(function (a, b) {
- return a.time - b.time;
- });
- return self.deferredNextId++;
- };
- /**
- * @name $browser#defer.now
- *
- * @description
- * Current milliseconds mock time.
- */
- self.defer.now = 0;
- self.defer.cancel = function (deferId) {
- var fnIndex;
- angular.forEach(self.deferredFns, function (fn, index) {
- if (fn.id === deferId) fnIndex = index;
- });
- if (angular.isDefined(fnIndex)) {
- self.deferredFns.splice(fnIndex, 1);
- return true;
- }
- return false;
- };
- /**
- * @name $browser#defer.flush
- *
- * @description
- * Flushes all pending requests and executes the defer callbacks.
- *
- * @param {number=} number of milliseconds to flush. See {@link #defer.now}
- */
- self.defer.flush = function (delay) {
- var nextTime;
- if (angular.isDefined(delay)) {
- // A delay was passed so compute the next time
- nextTime = self.defer.now + delay;
- } else {
- if (self.deferredFns.length) {
- // No delay was passed so set the next time so that it clears the deferred queue
- nextTime = self.deferredFns[self.deferredFns.length - 1].time;
- } else {
- // No delay passed, but there are no deferred tasks so flush - indicates an error!
- throw new Error('No deferred tasks to be flushed');
- }
- }
- while (self.deferredFns.length && self.deferredFns[0].time <= nextTime) {
- // Increment the time and call the next deferred function
- self.defer.now = self.deferredFns[0].time;
- self.deferredFns.shift().fn();
- }
- // Ensure that the current time is correct
- self.defer.now = nextTime;
- };
- self.$$baseHref = '/';
- self.baseHref = function () {
- return this.$$baseHref;
- };
- };
- angular.mock.$Browser.prototype = {
- /**
- * @name $browser#poll
- *
- * @description
- * run all fns in pollFns
- */
- poll: function poll() {
- angular.forEach(this.pollFns, function (pollFn) {
- pollFn();
- });
- },
- url: function (url, replace, state) {
- if (angular.isUndefined(state)) {
- state = null;
- }
- if (url) {
- this.$$url = url;
- // Native pushState serializes & copies the object; simulate it.
- this.$$state = angular.copy(state);
- return this;
- }
- return this.$$url;
- },
- state: function () {
- return this.$$state;
- }
- };
- /**
- * @ngdoc provider
- * @name $exceptionHandlerProvider
- *
- * @description
- * Configures the mock implementation of {@link ng.$exceptionHandler} to rethrow or to log errors
- * passed to the `$exceptionHandler`.
- */
- /**
- * @ngdoc service
- * @name $exceptionHandler
- *
- * @description
- * Mock implementation of {@link ng.$exceptionHandler} that rethrows or logs errors passed
- * to it. See {@link ngMock.$exceptionHandlerProvider $exceptionHandlerProvider} for configuration
- * information.
- *
- *
- * ```js
- * describe('$exceptionHandlerProvider', function() {
- *
- * it('should capture log messages and exceptions', function() {
- *
- * module(function($exceptionHandlerProvider) {
- * $exceptionHandlerProvider.mode('log');
- * });
- *
- * inject(function($log, $exceptionHandler, $timeout) {
- * $timeout(function() { $log.log(1); });
- * $timeout(function() { $log.log(2); throw 'banana peel'; });
- * $timeout(function() { $log.log(3); });
- * expect($exceptionHandler.errors).toEqual([]);
- * expect($log.assertEmpty());
- * $timeout.flush();
- * expect($exceptionHandler.errors).toEqual(['banana peel']);
- * expect($log.log.logs).toEqual([[1], [2], [3]]);
- * });
- * });
- * });
- * ```
- */
- angular.mock.$ExceptionHandlerProvider = function () {
- var handler;
- /**
- * @ngdoc method
- * @name $exceptionHandlerProvider#mode
- *
- * @description
- * Sets the logging mode.
- *
- * @param {string} mode Mode of operation, defaults to `rethrow`.
- *
- * - `log`: Sometimes it is desirable to test that an error is thrown, for this case the `log`
- * mode stores an array of errors in `$exceptionHandler.errors`, to allow later assertion of
- * them. See {@link ngMock.$log#assertEmpty assertEmpty()} and
- * {@link ngMock.$log#reset reset()}.
- * - `rethrow`: If any errors are passed to the handler in tests, it typically means that there
- * is a bug in the application or test, so this mock will make these tests fail. For any
- * implementations that expect exceptions to be thrown, the `rethrow` mode will also maintain
- * a log of thrown errors in `$exceptionHandler.errors`.
- */
- this.mode = function (mode) {
- switch (mode) {
- case 'log':
- case 'rethrow':
- var errors = [];
- handler = function (e) {
- if (arguments.length === 1) {
- errors.push(e);
- } else {
- errors.push([].slice.call(arguments, 0));
- }
- if (mode === 'rethrow') {
- throw e;
- }
- };
- handler.errors = errors;
- break;
- default:
- throw new Error('Unknown mode \'' + mode + '\', only \'log\'/\'rethrow\' modes are allowed!');
- }
- };
- this.$get = function () {
- return handler;
- };
- this.mode('rethrow');
- };
- /**
- * @ngdoc service
- * @name $log
- *
- * @description
- * Mock implementation of {@link ng.$log} that gathers all logged messages in arrays
- * (one array per logging level). These arrays are exposed as `logs` property of each of the
- * level-specific log function, e.g. for level `error` the array is exposed as `$log.error.logs`.
- *
- */
- angular.mock.$LogProvider = function () {
- var debug = true;
- function concat(array1, array2, index) {
- return array1.concat(Array.prototype.slice.call(array2, index));
- }
- this.debugEnabled = function (flag) {
- if (angular.isDefined(flag)) {
- debug = flag;
- return this;
- } else {
- return debug;
- }
- };
- this.$get = function () {
- var $log = {
- log: function () {
- $log.log.logs.push(concat([], arguments, 0));
- },
- warn: function () {
- $log.warn.logs.push(concat([], arguments, 0));
- },
- info: function () {
- $log.info.logs.push(concat([], arguments, 0));
- },
- error: function () {
- $log.error.logs.push(concat([], arguments, 0));
- },
- debug: function () {
- if (debug) {
- $log.debug.logs.push(concat([], arguments, 0));
- }
- }
- };
- /**
- * @ngdoc method
- * @name $log#reset
- *
- * @description
- * Reset all of the logging arrays to empty.
- */
- $log.reset = function () {
- /**
- * @ngdoc property
- * @name $log#log.logs
- *
- * @description
- * Array of messages logged using {@link ng.$log#log `log()`}.
- *
- * @example
- * ```js
- * $log.log('Some Log');
- * var first = $log.log.logs.unshift();
- * ```
- */
- $log.log.logs = [];
- /**
- * @ngdoc property
- * @name $log#info.logs
- *
- * @description
- * Array of messages logged using {@link ng.$log#info `info()`}.
- *
- * @example
- * ```js
- * $log.info('Some Info');
- * var first = $log.info.logs.unshift();
- * ```
- */
- $log.info.logs = [];
- /**
- * @ngdoc property
- * @name $log#warn.logs
- *
- * @description
- * Array of messages logged using {@link ng.$log#warn `warn()`}.
- *
- * @example
- * ```js
- * $log.warn('Some Warning');
- * var first = $log.warn.logs.unshift();
- * ```
- */
- $log.warn.logs = [];
- /**
- * @ngdoc property
- * @name $log#error.logs
- *
- * @description
- * Array of messages logged using {@link ng.$log#error `error()`}.
- *
- * @example
- * ```js
- * $log.error('Some Error');
- * var first = $log.error.logs.unshift();
- * ```
- */
- $log.error.logs = [];
- /**
- * @ngdoc property
- * @name $log#debug.logs
- *
- * @description
- * Array of messages logged using {@link ng.$log#debug `debug()`}.
- *
- * @example
- * ```js
- * $log.debug('Some Error');
- * var first = $log.debug.logs.unshift();
- * ```
- */
- $log.debug.logs = [];
- };
- /**
- * @ngdoc method
- * @name $log#assertEmpty
- *
- * @description
- * Assert that all of the logging methods have no logged messages. If any messages are present,
- * an exception is thrown.
- */
- $log.assertEmpty = function () {
- var errors = [];
- angular.forEach(['error', 'warn', 'info', 'log', 'debug'], function (logLevel) {
- angular.forEach($log[logLevel].logs, function (log) {
- angular.forEach(log, function (logItem) {
- errors.push('MOCK $log (' + logLevel + '): ' + String(logItem) + '\n' +
- (logItem.stack || ''));
- });
- });
- });
- if (errors.length) {
- errors.unshift('Expected $log to be empty! Either a message was logged unexpectedly, or ' +
- 'an expected log message was not checked and removed:');
- errors.push('');
- throw new Error(errors.join('\n---------\n'));
- }
- };
- $log.reset();
- return $log;
- };
- };
- /**
- * @ngdoc service
- * @name $interval
- *
- * @description
- * Mock implementation of the $interval service.
- *
- * Use {@link ngMock.$interval#flush `$interval.flush(millis)`} to
- * move forward by `millis` milliseconds and trigger any functions scheduled to run in that
- * time.
- *
- * @param {function()} fn A function that should be called repeatedly.
- * @param {number} delay Number of milliseconds between each function call.
- * @param {number=} [count=0] Number of times to repeat. If not set, or 0, will repeat
- * indefinitely.
- * @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
- * will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
- * @param {...*=} Pass additional parameters to the executed function.
- * @returns {promise} A promise which will be notified on each iteration.
- */
- angular.mock.$IntervalProvider = function () {
- this.$get = ['$browser', '$rootScope', '$q', '$$q',
- function ($browser, $rootScope, $q, $$q) {
- var repeatFns = [],
- nextRepeatId = 0,
- now = 0;
- var $interval = function (fn, delay, count, invokeApply) {
- var hasParams = arguments.length > 4,
- args = hasParams ? Array.prototype.slice.call(arguments, 4) : [],
- iteration = 0,
- skipApply = (angular.isDefined(invokeApply) && !invokeApply),
- deferred = (skipApply ? $$q : $q).defer(),
- promise = deferred.promise;
- count = (angular.isDefined(count)) ? count : 0;
- promise.then(null, function () {
- }, (!hasParams) ? fn : function () {
- fn.apply(null, args);
- });
- promise.$$intervalId = nextRepeatId;
- function tick() {
- deferred.notify(iteration++);
- if (count > 0 && iteration >= count) {
- var fnIndex;
- deferred.resolve(iteration);
- angular.forEach(repeatFns, function (fn, index) {
- if (fn.id === promise.$$intervalId) fnIndex = index;
- });
- if (angular.isDefined(fnIndex)) {
- repeatFns.splice(fnIndex, 1);
- }
- }
- if (skipApply) {
- $browser.defer.flush();
- } else {
- $rootScope.$apply();
- }
- }
- repeatFns.push({
- nextTime: (now + (delay || 0)),
- delay: delay || 1,
- fn: tick,
- id: nextRepeatId,
- deferred: deferred
- });
- repeatFns.sort(function (a, b) {
- return a.nextTime - b.nextTime;
- });
- nextRepeatId++;
- return promise;
- };
- /**
- * @ngdoc method
- * @name $interval#cancel
- *
- * @description
- * Cancels a task associated with the `promise`.
- *
- * @param {promise} promise A promise from calling the `$interval` function.
- * @returns {boolean} Returns `true` if the task was successfully cancelled.
- */
- $interval.cancel = function (promise) {
- if (!promise) return false;
- var fnIndex;
- angular.forEach(repeatFns, function (fn, index) {
- if (fn.id === promise.$$intervalId) fnIndex = index;
- });
- if (angular.isDefined(fnIndex)) {
- repeatFns[fnIndex].deferred.promise.then(undefined, function () {
- });
- repeatFns[fnIndex].deferred.reject('canceled');
- repeatFns.splice(fnIndex, 1);
- return true;
- }
- return false;
- };
- /**
- * @ngdoc method
- * @name $interval#flush
- * @description
- *
- * Runs interval tasks scheduled to be run in the next `millis` milliseconds.
- *
- * @param {number=} millis maximum timeout amount to flush up until.
- *
- * @return {number} The amount of time moved forward.
- */
- $interval.flush = function (millis) {
- var before = now;
- now += millis;
- while (repeatFns.length && repeatFns[0].nextTime <= now) {
- var task = repeatFns[0];
- task.fn();
- if (task.nextTime === before) {
- // this can only happen the first time
- // a zero-delay interval gets triggered
- task.nextTime++;
- }
- task.nextTime += task.delay;
- repeatFns.sort(function (a, b) {
- return a.nextTime - b.nextTime;
- });
- }
- return millis;
- };
- return $interval;
- }];
- };
- function jsonStringToDate(string) {
- // The R_ISO8061_STR regex is never going to fit into the 100 char limit!
- // eslit-disable-next-line max-len
- var R_ISO8061_STR = /^(-?\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d{3}))?)?)?(Z|([+-])(\d\d):?(\d\d)))?$/;
- var match;
- if ((match = string.match(R_ISO8061_STR))) {
- var date = new Date(0),
- tzHour = 0,
- tzMin = 0;
- if (match[9]) {
- tzHour = toInt(match[9] + match[10]);
- tzMin = toInt(match[9] + match[11]);
- }
- date.setUTCFullYear(toInt(match[1]), toInt(match[2]) - 1, toInt(match[3]));
- date.setUTCHours(toInt(match[4] || 0) - tzHour,
- toInt(match[5] || 0) - tzMin,
- toInt(match[6] || 0),
- toInt(match[7] || 0));
- return date;
- }
- return string;
- }
- function toInt(str) {
- return parseInt(str, 10);
- }
- function padNumberInMock(num, digits, trim) {
- var neg = '';
- if (num < 0) {
- neg = '-';
- num = -num;
- }
- num = '' + num;
- while (num.length < digits) num = '0' + num;
- if (trim) {
- num = num.substr(num.length - digits);
- }
- return neg + num;
- }
- /**
- * @ngdoc type
- * @name angular.mock.TzDate
- * @description
- *
- * *NOTE*: this is not an injectable instance, just a globally available mock class of `Date`.
- *
- * Mock of the Date type which has its timezone specified via constructor arg.
- *
- * The main purpose is to create Date-like instances with timezone fixed to the specified timezone
- * offset, so that we can test code that depends on local timezone settings without dependency on
- * the time zone settings of the machine where the code is running.
- *
- * @param {number} offset Offset of the *desired* timezone in hours (fractions will be honored)
- * @param {(number|string)} timestamp Timestamp representing the desired time in *UTC*
- *
- * @example
- * !!!! WARNING !!!!!
- * This is not a complete Date object so only methods that were implemented can be called safely.
- * To make matters worse, TzDate instances inherit stuff from Date via a prototype.
- *
- * We do our best to intercept calls to "unimplemented" methods, but since the list of methods is
- * incomplete we might be missing some non-standard methods. This can result in errors like:
- * "Date.prototype.foo called on incompatible Object".
- *
- * ```js
- * var newYearInBratislava = new TzDate(-1, '2009-12-31T23:00:00Z');
- * newYearInBratislava.getTimezoneOffset() => -60;
- * newYearInBratislava.getFullYear() => 2010;
- * newYearInBratislava.getMonth() => 0;
- * newYearInBratislava.getDate() => 1;
- * newYearInBratislava.getHours() => 0;
- * newYearInBratislava.getMinutes() => 0;
- * newYearInBratislava.getSeconds() => 0;
- * ```
- *
- */
- angular.mock.TzDate = function (offset, timestamp) {
- var self = new Date(0);
- if (angular.isString(timestamp)) {
- var tsStr = timestamp;
- self.origDate = jsonStringToDate(timestamp);
- timestamp = self.origDate.getTime();
- if (isNaN(timestamp)) {
- // eslint-disable-next-line no-throw-literal
- throw {
- name: 'Illegal Argument',
- message: 'Arg \'' + tsStr + '\' passed into TzDate constructor is not a valid date string'
- };
- }
- } else {
- self.origDate = new Date(timestamp);
- }
- var localOffset = new Date(timestamp).getTimezoneOffset();
- self.offsetDiff = localOffset * 60 * 1000 - offset * 1000 * 60 * 60;
- self.date = new Date(timestamp + self.offsetDiff);
- self.getTime = function () {
- return self.date.getTime() - self.offsetDiff;
- };
- self.toLocaleDateString = function () {
- return self.date.toLocaleDateString();
- };
- self.getFullYear = function () {
- return self.date.getFullYear();
- };
- self.getMonth = function () {
- return self.date.getMonth();
- };
- self.getDate = function () {
- return self.date.getDate();
- };
- self.getHours = function () {
- return self.date.getHours();
- };
- self.getMinutes = function () {
- return self.date.getMinutes();
- };
- self.getSeconds = function () {
- return self.date.getSeconds();
- };
- self.getMilliseconds = function () {
- return self.date.getMilliseconds();
- };
- self.getTimezoneOffset = function () {
- return offset * 60;
- };
- self.getUTCFullYear = function () {
- return self.origDate.getUTCFullYear();
- };
- self.getUTCMonth = function () {
- return self.origDate.getUTCMonth();
- };
- self.getUTCDate = function () {
- return self.origDate.getUTCDate();
- };
- self.getUTCHours = function () {
- return self.origDate.getUTCHours();
- };
- self.getUTCMinutes = function () {
- return self.origDate.getUTCMinutes();
- };
- self.getUTCSeconds = function () {
- return self.origDate.getUTCSeconds();
- };
- self.getUTCMilliseconds = function () {
- return self.origDate.getUTCMilliseconds();
- };
- self.getDay = function () {
- return self.date.getDay();
- };
- // provide this method only on browsers that already have it
- if (self.toISOString) {
- self.toISOString = function () {
- return padNumberInMock(self.origDate.getUTCFullYear(), 4) + '-' +
- padNumberInMock(self.origDate.getUTCMonth() + 1, 2) + '-' +
- padNumberInMock(self.origDate.getUTCDate(), 2) + 'T' +
- padNumberInMock(self.origDate.getUTCHours(), 2) + ':' +
- padNumberInMock(self.origDate.getUTCMinutes(), 2) + ':' +
- padNumberInMock(self.origDate.getUTCSeconds(), 2) + '.' +
- padNumberInMock(self.origDate.getUTCMilliseconds(), 3) + 'Z';
- };
- }
- //hide all methods not implemented in this mock that the Date prototype exposes
- var unimplementedMethods = ['getUTCDay',
- 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds',
- 'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear',
- 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds',
- 'setYear', 'toDateString', 'toGMTString', 'toJSON', 'toLocaleFormat', 'toLocaleString',
- 'toLocaleTimeString', 'toSource', 'toString', 'toTimeString', 'toUTCString', 'valueOf'];
- angular.forEach(unimplementedMethods, function (methodName) {
- self[methodName] = function () {
- throw new Error('Method \'' + methodName + '\' is not implemented in the TzDate mock');
- };
- });
- return self;
- };
- //make "tzDateInstance instanceof Date" return true
- angular.mock.TzDate.prototype = Date.prototype;
- /**
- * @ngdoc service
- * @name $animate
- *
- * @description
- * Mock implementation of the {@link ng.$animate `$animate`} service. Exposes two additional methods
- * for testing animations.
- *
- * You need to require the `ngAnimateMock` module in your test suite for instance `beforeEach(module('ngAnimateMock'))`
- */
- angular.mock.animate = angular.module('ngAnimateMock', ['ng'])
- .info({angularVersion: '1.6.5'})
- .config(['$provide', function ($provide) {
- $provide.factory('$$forceReflow', function () {
- function reflowFn() {
- reflowFn.totalReflows++;
- }
- reflowFn.totalReflows = 0;
- return reflowFn;
- });
- $provide.factory('$$animateAsyncRun', function () {
- var queue = [];
- var queueFn = function () {
- return function (fn) {
- queue.push(fn);
- };
- };
- queueFn.flush = function () {
- if (queue.length === 0) return false;
- for (var i = 0; i < queue.length; i++) {
- queue[i]();
- }
- queue = [];
- return true;
- };
- return queueFn;
- });
- $provide.decorator('$$animateJs', ['$delegate', function ($delegate) {
- var runners = [];
- var animateJsConstructor = function () {
- var animator = $delegate.apply($delegate, arguments);
- // If no javascript animation is found, animator is undefined
- if (animator) {
- runners.push(animator);
- }
- return animator;
- };
- animateJsConstructor.$closeAndFlush = function () {
- runners.forEach(function (runner) {
- runner.end();
- });
- runners = [];
- };
- return animateJsConstructor;
- }]);
- $provide.decorator('$animateCss', ['$delegate', function ($delegate) {
- var runners = [];
- var animateCssConstructor = function (element, options) {
- var animator = $delegate(element, options);
- runners.push(animator);
- return animator;
- };
- animateCssConstructor.$closeAndFlush = function () {
- runners.forEach(function (runner) {
- runner.end();
- });
- runners = [];
- };
- return animateCssConstructor;
- }]);
- $provide.decorator('$animate', ['$delegate', '$timeout', '$browser', '$$rAF', '$animateCss', '$$animateJs',
- '$$forceReflow', '$$animateAsyncRun', '$rootScope',
- function ($delegate, $timeout, $browser, $$rAF, $animateCss, $$animateJs,
- $$forceReflow, $$animateAsyncRun, $rootScope) {
- var animate = {
- queue: [],
- cancel: $delegate.cancel,
- on: $delegate.on,
- off: $delegate.off,
- pin: $delegate.pin,
- get reflows() {
- return $$forceReflow.totalReflows;
- },
- enabled: $delegate.enabled,
- /**
- * @ngdoc method
- * @name $animate#closeAndFlush
- * @description
- *
- * This method will close all pending animations (both {@link ngAnimate#javascript-based-animations Javascript}
- * and {@link ngAnimate.$animateCss CSS}) and it will also flush any remaining animation frames and/or callbacks.
- */
- closeAndFlush: function () {
- // we allow the flush command to swallow the errors
- // because depending on whether CSS or JS animations are
- // used, there may not be a RAF flush. The primary flush
- // at the end of this function must throw an exception
- // because it will track if there were pending animations
- this.flush(true);
- $animateCss.$closeAndFlush();
- $$animateJs.$closeAndFlush();
- this.flush();
- },
- /**
- * @ngdoc method
- * @name $animate#flush
- * @description
- *
- * This method is used to flush the pending callbacks and animation frames to either start
- * an animation or conclude an animation. Note that this will not actually close an
- * actively running animation (see {@link ngMock.$animate#closeAndFlush `closeAndFlush()`} for that).
- */
- flush: function (hideErrors) {
- $rootScope.$digest();
- var doNextRun, somethingFlushed = false;
- do {
- doNextRun = false;
- if ($$rAF.queue.length) {
- $$rAF.flush();
- doNextRun = somethingFlushed = true;
- }
- if ($$animateAsyncRun.flush()) {
- doNextRun = somethingFlushed = true;
- }
- } while (doNextRun);
- if (!somethingFlushed && !hideErrors) {
- throw new Error('No pending animations ready to be closed or flushed');
- }
- $rootScope.$digest();
- }
- };
- angular.forEach(
- ['animate', 'enter', 'leave', 'move', 'addClass', 'removeClass', 'setClass'], function (method) {
- animate[method] = function () {
- animate.queue.push({
- event: method,
- element: arguments[0],
- options: arguments[arguments.length - 1],
- args: arguments
- });
- return $delegate[method].apply($delegate, arguments);
- };
- });
- return animate;
- }]);
- }]);
- /**
- * @ngdoc function
- * @name angular.mock.dump
- * @description
- *
- * *NOTE*: This is not an injectable instance, just a globally available function.
- *
- * Method for serializing common angular objects (scope, elements, etc..) into strings.
- * It is useful for logging objects to the console when debugging.
- *
- * @param {*} object - any object to turn into string.
- * @return {string} a serialized string of the argument
- */
- angular.mock.dump = function (object) {
- return serialize(object);
- function serialize(object) {
- var out;
- if (angular.isElement(object)) {
- object = angular.element(object);
- out = angular.element('<div></div>');
- angular.forEach(object, function (element) {
- out.append(angular.element(element).clone());
- });
- out = out.html();
- } else if (angular.isArray(object)) {
- out = [];
- angular.forEach(object, function (o) {
- out.push(serialize(o));
- });
- out = '[ ' + out.join(', ') + ' ]';
- } else if (angular.isObject(object)) {
- if (angular.isFunction(object.$eval) && angular.isFunction(object.$apply)) {
- out = serializeScope(object);
- } else if (object instanceof Error) {
- out = object.stack || ('' + object.name + ': ' + object.message);
- } else {
- // TODO(i): this prevents methods being logged,
- // we should have a better way to serialize objects
- out = angular.toJson(object, true);
- }
- } else {
- out = String(object);
- }
- return out;
- }
- function serializeScope(scope, offset) {
- offset = offset || ' ';
- var log = [offset + 'Scope(' + scope.$id + '): {'];
- for (var key in scope) {
- if (Object.prototype.hasOwnProperty.call(scope, key) && !key.match(/^(\$|this)/)) {
- log.push(' ' + key + ': ' + angular.toJson(scope[key]));
- }
- }
- var child = scope.$$childHead;
- while (child) {
- log.push(serializeScope(child, offset + ' '));
- child = child.$$nextSibling;
- }
- log.push('}');
- return log.join('\n' + offset);
- }
- };
- /**
- * @ngdoc service
- * @name $httpBackend
- * @description
- * Fake HTTP backend implementation suitable for unit testing applications that use the
- * {@link ng.$http $http service}.
- *
- * <div class="alert alert-info">
- * **Note**: For fake HTTP backend implementation suitable for end-to-end testing or backend-less
- * development please see {@link ngMockE2E.$httpBackend e2e $httpBackend mock}.
- * </div>
- *
- * During unit testing, we want our unit tests to run quickly and have no external dependencies so
- * we don’t want to send [XHR](https://developer.mozilla.org/en/xmlhttprequest) or
- * [JSONP](http://en.wikipedia.org/wiki/JSONP) requests to a real server. All we really need is
- * to verify whether a certain request has been sent or not, or alternatively just let the
- * application make requests, respond with pre-trained responses and assert that the end result is
- * what we expect it to be.
- *
- * This mock implementation can be used to respond with static or dynamic responses via the
- * `expect` and `when` apis and their shortcuts (`expectGET`, `whenPOST`, etc).
- *
- * When an Angular application needs some data from a server, it calls the $http service, which
- * sends the request to a real server using $httpBackend service. With dependency injection, it is
- * easy to inject $httpBackend mock (which has the same API as $httpBackend) and use it to verify
- * the requests and respond with some testing data without sending a request to a real server.
- *
- * There are two ways to specify what test data should be returned as http responses by the mock
- * backend when the code under test makes http requests:
- *
- * - `$httpBackend.expect` - specifies a request expectation
- * - `$httpBackend.when` - specifies a backend definition
- *
- *
- * ## Request Expectations vs Backend Definitions
- *
- * Request expectations provide a way to make assertions about requests made by the application and
- * to define responses for those requests. The test will fail if the expected requests are not made
- * or they are made in the wrong order.
- *
- * Backend definitions allow you to define a fake backend for your application which doesn't assert
- * if a particular request was made or not, it just returns a trained response if a request is made.
- * The test will pass whether or not the request gets made during testing.
- *
- *
- * <table class="table">
- * <tr><th width="220px"></th><th>Request expectations</th><th>Backend definitions</th></tr>
- * <tr>
- * <th>Syntax</th>
- * <td>.expect(...).respond(...)</td>
- * <td>.when(...).respond(...)</td>
- * </tr>
- * <tr>
- * <th>Typical usage</th>
- * <td>strict unit tests</td>
- * <td>loose (black-box) unit testing</td>
- * </tr>
- * <tr>
- * <th>Fulfills multiple requests</th>
- * <td>NO</td>
- * <td>YES</td>
- * </tr>
- * <tr>
- * <th>Order of requests matters</th>
- * <td>YES</td>
- * <td>NO</td>
- * </tr>
- * <tr>
- * <th>Request required</th>
- * <td>YES</td>
- * <td>NO</td>
- * </tr>
- * <tr>
- * <th>Response required</th>
- * <td>optional (see below)</td>
- * <td>YES</td>
- * </tr>
- * </table>
- *
- * In cases where both backend definitions and request expectations are specified during unit
- * testing, the request expectations are evaluated first.
- *
- * If a request expectation has no response specified, the algorithm will search your backend
- * definitions for an appropriate response.
- *
- * If a request didn't match any expectation or if the expectation doesn't have the response
- * defined, the backend definitions are evaluated in sequential order to see if any of them match
- * the request. The response from the first matched definition is returned.
- *
- *
- * ## Flushing HTTP requests
- *
- * The $httpBackend used in production always responds to requests asynchronously. If we preserved
- * this behavior in unit testing, we'd have to create async unit tests, which are hard to write,
- * to follow and to maintain. But neither can the testing mock respond synchronously; that would
- * change the execution of the code under test. For this reason, the mock $httpBackend has a
- * `flush()` method, which allows the test to explicitly flush pending requests. This preserves
- * the async api of the backend, while allowing the test to execute synchronously.
- *
- *
- * ## Unit testing with mock $httpBackend
- * The following code shows how to setup and use the mock backend when unit testing a controller.
- * First we create the controller under test:
- *
- ```js
- // The module code
- angular
- .module('MyApp', [])
- .controller('MyController', MyController);
- // The controller code
- function MyController($scope, $http) {
- var authToken;
- $http.get('/auth.py').then(function(response) {
- authToken = response.headers('A-Token');
- $scope.user = response.data;
- }).catch(function() {
- $scope.status = 'Failed...';
- });
- $scope.saveMessage = function(message) {
- var headers = { 'Authorization': authToken };
- $scope.status = 'Saving...';
- $http.post('/add-msg.py', message, { headers: headers } ).then(function(response) {
- $scope.status = '';
- }).catch(function() {
- $scope.status = 'Failed...';
- });
- };
- }
- ```
- *
- * Now we setup the mock backend and create the test specs:
- *
- ```js
- // testing controller
- describe('MyController', function() {
- var $httpBackend, $rootScope, createController, authRequestHandler;
- // Set up the module
- beforeEach(module('MyApp'));
- beforeEach(inject(function($injector) {
- // Set up the mock http service responses
- $httpBackend = $injector.get('$httpBackend');
- // backend definition common for all tests
- authRequestHandler = $httpBackend.when('GET', '/auth.py')
- .respond({userId: 'userX'}, {'A-Token': 'xxx'});
- // Get hold of a scope (i.e. the root scope)
- $rootScope = $injector.get('$rootScope');
- // The $controller service is used to create instances of controllers
- var $controller = $injector.get('$controller');
- createController = function() {
- return $controller('MyController', {'$scope' : $rootScope });
- };
- }));
- afterEach(function() {
- $httpBackend.verifyNoOutstandingExpectation();
- $httpBackend.verifyNoOutstandingRequest();
- });
- it('should fetch authentication token', function() {
- $httpBackend.expectGET('/auth.py');
- var controller = createController();
- $httpBackend.flush();
- });
- it('should fail authentication', function() {
- // Notice how you can change the response even after it was set
- authRequestHandler.respond(401, '');
- $httpBackend.expectGET('/auth.py');
- var controller = createController();
- $httpBackend.flush();
- expect($rootScope.status).toBe('Failed...');
- });
- it('should send msg to server', function() {
- var controller = createController();
- $httpBackend.flush();
- // now you don’t care about the authentication, but
- // the controller will still send the request and
- // $httpBackend will respond without you having to
- // specify the expectation and response for this request
- $httpBackend.expectPOST('/add-msg.py', 'message content').respond(201, '');
- $rootScope.saveMessage('message content');
- expect($rootScope.status).toBe('Saving...');
- $httpBackend.flush();
- expect($rootScope.status).toBe('');
- });
- it('should send auth header', function() {
- var controller = createController();
- $httpBackend.flush();
- $httpBackend.expectPOST('/add-msg.py', undefined, function(headers) {
- // check if the header was sent, if it wasn't the expectation won't
- // match the request and the test will fail
- return headers['Authorization'] === 'xxx';
- }).respond(201, '');
- $rootScope.saveMessage('whatever');
- $httpBackend.flush();
- });
- });
- ```
- *
- * ## Dynamic responses
- *
- * You define a response to a request by chaining a call to `respond()` onto a definition or expectation.
- * If you provide a **callback** as the first parameter to `respond(callback)` then you can dynamically generate
- * a response based on the properties of the request.
- *
- * The `callback` function should be of the form `function(method, url, data, headers, params)`.
- *
- * ### Query parameters
- *
- * By default, query parameters on request URLs are parsed into the `params` object. So a request URL
- * of `/list?q=searchstr&orderby=-name` would set `params` to be `{q: 'searchstr', orderby: '-name'}`.
- *
- * ### Regex parameter matching
- *
- * If an expectation or definition uses a **regex** to match the URL, you can provide an array of **keys** via a
- * `params` argument. The index of each **key** in the array will match the index of a **group** in the
- * **regex**.
- *
- * The `params` object in the **callback** will now have properties with these keys, which hold the value of the
- * corresponding **group** in the **regex**.
- *
- * This also applies to the `when` and `expect` shortcut methods.
- *
- *
- * ```js
- * $httpBackend.expect('GET', /\/user\/(.+)/, undefined, undefined, ['id'])
- * .respond(function(method, url, data, headers, params) {
- * // for requested url of '/user/1234' params is {id: '1234'}
- * });
- *
- * $httpBackend.whenPATCH(/\/user\/(.+)\/article\/(.+)/, undefined, undefined, ['user', 'article'])
- * .respond(function(method, url, data, headers, params) {
- * // for url of '/user/1234/article/567' params is {user: '1234', article: '567'}
- * });
- * ```
- *
- * ## Matching route requests
- *
- * For extra convenience, `whenRoute` and `expectRoute` shortcuts are available. These methods offer colon
- * delimited matching of the url path, ignoring the query string. This allows declarations
- * similar to how application routes are configured with `$routeProvider`. Because these methods convert
- * the definition url to regex, declaration order is important. Combined with query parameter parsing,
- * the following is possible:
- *
- ```js
- $httpBackend.whenRoute('GET', '/users/:id')
- .respond(function(method, url, data, headers, params) {
- return [200, MockUserList[Number(params.id)]];
- });
- $httpBackend.whenRoute('GET', '/users')
- .respond(function(method, url, data, headers, params) {
- var userList = angular.copy(MockUserList),
- defaultSort = 'lastName',
- count, pages, isPrevious, isNext;
- // paged api response '/v1/users?page=2'
- params.page = Number(params.page) || 1;
- // query for last names '/v1/users?q=Archer'
- if (params.q) {
- userList = $filter('filter')({lastName: params.q});
- }
- pages = Math.ceil(userList.length / pagingLength);
- isPrevious = params.page > 1;
- isNext = params.page < pages;
- return [200, {
- count: userList.length,
- previous: isPrevious,
- next: isNext,
- // sort field -> '/v1/users?sortBy=firstName'
- results: $filter('orderBy')(userList, params.sortBy || defaultSort)
- .splice((params.page - 1) * pagingLength, pagingLength)
- }];
- });
- ```
- */
- angular.mock.$httpBackendDecorator =
- ['$rootScope', '$timeout', '$delegate', createHttpBackendMock];
- /**
- * General factory function for $httpBackend mock.
- * Returns instance for unit testing (when no arguments specified):
- * - passing through is disabled
- * - auto flushing is disabled
- *
- * Returns instance for e2e testing (when `$delegate` and `$browser` specified):
- * - passing through (delegating request to real backend) is enabled
- * - auto flushing is enabled
- *
- * @param {Object=} $delegate Real $httpBackend instance (allow passing through if specified)
- * @param {Object=} $browser Auto-flushing enabled if specified
- * @return {Object} Instance of $httpBackend mock
- */
- function createHttpBackendMock($rootScope, $timeout, $delegate, $browser) {
- var definitions = [],
- expectations = [],
- responses = [],
- responsesPush = angular.bind(responses, responses.push),
- copy = angular.copy,
- // We cache the original backend so that if both ngMock and ngMockE2E override the
- // service the ngMockE2E version can pass through to the real backend
- originalHttpBackend = $delegate.$$originalHttpBackend || $delegate;
- function createResponse(status, data, headers, statusText) {
- if (angular.isFunction(status)) return status;
- return function () {
- return angular.isNumber(status)
- ? [status, data, headers, statusText]
- : [200, status, data, headers];
- };
- }
- // TODO(vojta): change params to: method, url, data, headers, callback
- function $httpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers) {
- var xhr = new MockXhr(),
- expectation = expectations[0],
- wasExpected = false;
- xhr.$$events = eventHandlers;
- xhr.upload.$$events = uploadEventHandlers;
- function prettyPrint(data) {
- return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
- ? data
- : angular.toJson(data);
- }
- function wrapResponse(wrapped) {
- if (!$browser && timeout) {
- if (timeout.then) {
- timeout.then(handleTimeout);
- } else {
- $timeout(handleTimeout, timeout);
- }
- }
- handleResponse.description = method + ' ' + url;
- return handleResponse;
- function handleResponse() {
- var response = wrapped.response(method, url, data, headers, wrapped.params(url));
- xhr.$$respHeaders = response[2];
- callback(copy(response[0]), copy(response[1]), xhr.getAllResponseHeaders(),
- copy(response[3] || ''));
- }
- function handleTimeout() {
- for (var i = 0, ii = responses.length; i < ii; i++) {
- if (responses[i] === handleResponse) {
- responses.splice(i, 1);
- callback(-1, undefined, '');
- break;
- }
- }
- }
- }
- if (expectation && expectation.match(method, url)) {
- if (!expectation.matchData(data)) {
- throw new Error('Expected ' + expectation + ' with different data\n' +
- 'EXPECTED: ' + prettyPrint(expectation.data) + '\nGOT: ' + data);
- }
- if (!expectation.matchHeaders(headers)) {
- throw new Error('Expected ' + expectation + ' with different headers\n' +
- 'EXPECTED: ' + prettyPrint(expectation.headers) + '\nGOT: ' +
- prettyPrint(headers));
- }
- expectations.shift();
- if (expectation.response) {
- responses.push(wrapResponse(expectation));
- return;
- }
- wasExpected = true;
- }
- var i = -1, definition;
- while ((definition = definitions[++i])) {
- if (definition.match(method, url, data, headers || {})) {
- if (definition.response) {
- // if $browser specified, we do auto flush all requests
- ($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
- } else if (definition.passThrough) {
- originalHttpBackend(method, url, data, callback, headers, timeout, withCredentials, responseType, eventHandlers, uploadEventHandlers);
- } else throw new Error('No response defined !');
- return;
- }
- }
- throw wasExpected ?
- new Error('No response defined !') :
- new Error('Unexpected request: ' + method + ' ' + url + '\n' +
- (expectation ? 'Expected ' + expectation : 'No more request expected'));
- }
- /**
- * @ngdoc method
- * @name $httpBackend#when
- * @description
- * Creates a new backend definition.
- *
- * @param {string} method HTTP method.
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
- * object and returns true if the headers match the current definition.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- *
- * - respond –
- * ```js
- * {function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers, params)}
- * ```
- * – The respond method takes a set of static data to be returned or a function that can
- * return an array containing response status (number), response data (Array|Object|string),
- * response headers (Object), and the text for the status (string). The respond method returns
- * the `requestHandler` object for possible overrides.
- */
- $httpBackend.when = function (method, url, data, headers, keys) {
- assertArgDefined(arguments, 1, 'url');
- var definition = new MockHttpExpectation(method, url, data, headers, keys),
- chain = {
- respond: function (status, data, headers, statusText) {
- definition.passThrough = undefined;
- definition.response = createResponse(status, data, headers, statusText);
- return chain;
- }
- };
- if ($browser) {
- chain.passThrough = function () {
- definition.response = undefined;
- definition.passThrough = true;
- return chain;
- };
- }
- definitions.push(definition);
- return chain;
- };
- /**
- * @ngdoc method
- * @name $httpBackend#whenGET
- * @description
- * Creates a new backend definition for GET requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenHEAD
- * @description
- * Creates a new backend definition for HEAD requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenDELETE
- * @description
- * Creates a new backend definition for DELETE requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenPOST
- * @description
- * Creates a new backend definition for POST requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenPUT
- * @description
- * Creates a new backend definition for PUT requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenJSONP
- * @description
- * Creates a new backend definition for JSONP requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- createShortMethods('when');
- /**
- * @ngdoc method
- * @name $httpBackend#whenRoute
- * @description
- * Creates a new backend definition that compares only with the requested route.
- *
- * @param {string} method HTTP method.
- * @param {string} url HTTP url string that supports colon param matching.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled. See #when for more info.
- */
- $httpBackend.whenRoute = function (method, url) {
- var pathObj = parseRoute(url);
- return $httpBackend.when(method, pathObj.regexp, undefined, undefined, pathObj.keys);
- };
- function parseRoute(url) {
- var ret = {
- regexp: url
- },
- keys = ret.keys = [];
- if (!url || !angular.isString(url)) return ret;
- url = url
- .replace(/([().])/g, '\\$1')
- .replace(/(\/)?:(\w+)([?*])?/g, function (_, slash, key, option) {
- var optional = option === '?' ? option : null;
- var star = option === '*' ? option : null;
- keys.push({name: key, optional: !!optional});
- slash = slash || '';
- return ''
- + (optional ? '' : slash)
- + '(?:'
- + (optional ? slash : '')
- + (star && '(.+?)' || '([^/]+)')
- + (optional || '')
- + ')'
- + (optional || '');
- })
- .replace(/([/$*])/g, '\\$1');
- ret.regexp = new RegExp('^' + url, 'i');
- return ret;
- }
- /**
- * @ngdoc method
- * @name $httpBackend#expect
- * @description
- * Creates a new request expectation.
- *
- * @param {string} method HTTP method.
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
- * receives data string and returns true if the data is as expected, or Object if request body
- * is in JSON format.
- * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
- * object and returns true if the headers match the current expectation.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- *
- * - respond –
- * ```
- * { function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers, params)}
- * ```
- * – The respond method takes a set of static data to be returned or a function that can
- * return an array containing response status (number), response data (Array|Object|string),
- * response headers (Object), and the text for the status (string). The respond method returns
- * the `requestHandler` object for possible overrides.
- */
- $httpBackend.expect = function (method, url, data, headers, keys) {
- assertArgDefined(arguments, 1, 'url');
- var expectation = new MockHttpExpectation(method, url, data, headers, keys),
- chain = {
- respond: function (status, data, headers, statusText) {
- expectation.response = createResponse(status, data, headers, statusText);
- return chain;
- }
- };
- expectations.push(expectation);
- return chain;
- };
- /**
- * @ngdoc method
- * @name $httpBackend#expectGET
- * @description
- * Creates a new request expectation for GET requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled. See #expect for more info.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectHEAD
- * @description
- * Creates a new request expectation for HEAD requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectDELETE
- * @description
- * Creates a new request expectation for DELETE requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectPOST
- * @description
- * Creates a new request expectation for POST requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
- * receives data string and returns true if the data is as expected, or Object if request body
- * is in JSON format.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectPUT
- * @description
- * Creates a new request expectation for PUT requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
- * receives data string and returns true if the data is as expected, or Object if request body
- * is in JSON format.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectPATCH
- * @description
- * Creates a new request expectation for PATCH requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string)|Object)=} data HTTP request body or function that
- * receives data string and returns true if the data is as expected, or Object if request body
- * is in JSON format.
- * @param {Object=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#expectJSONP
- * @description
- * Creates a new request expectation for JSONP requests. For more info see `expect()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives an url
- * and returns true if the url matches the current definition.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described above.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled.
- */
- createShortMethods('expect');
- /**
- * @ngdoc method
- * @name $httpBackend#expectRoute
- * @description
- * Creates a new request expectation that compares only with the requested route.
- *
- * @param {string} method HTTP method.
- * @param {string} url HTTP url string that supports colon param matching.
- * @returns {requestHandler} Returns an object with `respond` method that controls how a matched
- * request is handled. You can save this object for later use and invoke `respond` again in
- * order to change how a matched request is handled. See #expect for more info.
- */
- $httpBackend.expectRoute = function (method, url) {
- var pathObj = parseRoute(url);
- return $httpBackend.expect(method, pathObj.regexp, undefined, undefined, pathObj.keys);
- };
- /**
- * @ngdoc method
- * @name $httpBackend#flush
- * @description
- * Flushes pending requests using the trained responses. Requests are flushed in the order they
- * were made, but it is also possible to skip one or more requests (for example to have them
- * flushed later). This is useful for simulating scenarios where responses arrive from the server
- * in any order.
- *
- * If there are no pending requests to flush when the method is called, an exception is thrown (as
- * this is typically a sign of programming error).
- *
- * @param {number=} count - Number of responses to flush. If undefined/null, all pending requests
- * (starting after `skip`) will be flushed.
- * @param {number=} [skip=0] - Number of pending requests to skip. For example, a value of `5`
- * would skip the first 5 pending requests and start flushing from the 6th onwards.
- */
- $httpBackend.flush = function (count, skip, digest) {
- if (digest !== false) $rootScope.$digest();
- skip = skip || 0;
- if (skip >= responses.length) throw new Error('No pending request to flush !');
- if (angular.isDefined(count) && count !== null) {
- while (count--) {
- var part = responses.splice(skip, 1);
- if (!part.length) throw new Error('No more pending request to flush !');
- part[0]();
- }
- } else {
- while (responses.length > skip) {
- responses.splice(skip, 1)[0]();
- }
- }
- $httpBackend.verifyNoOutstandingExpectation(digest);
- };
- /**
- * @ngdoc method
- * @name $httpBackend#verifyNoOutstandingExpectation
- * @description
- * Verifies that all of the requests defined via the `expect` api were made. If any of the
- * requests were not made, verifyNoOutstandingExpectation throws an exception.
- *
- * Typically, you would call this method following each test case that asserts requests using an
- * "afterEach" clause.
- *
- * ```js
- * afterEach($httpBackend.verifyNoOutstandingExpectation);
- * ```
- */
- $httpBackend.verifyNoOutstandingExpectation = function (digest) {
- if (digest !== false) $rootScope.$digest();
- if (expectations.length) {
- throw new Error('Unsatisfied requests: ' + expectations.join(', '));
- }
- };
- /**
- * @ngdoc method
- * @name $httpBackend#verifyNoOutstandingRequest
- * @description
- * Verifies that there are no outstanding requests that need to be flushed.
- *
- * Typically, you would call this method following each test case that asserts requests using an
- * "afterEach" clause.
- *
- * ```js
- * afterEach($httpBackend.verifyNoOutstandingRequest);
- * ```
- */
- $httpBackend.verifyNoOutstandingRequest = function (digest) {
- if (digest !== false) $rootScope.$digest();
- if (responses.length) {
- var unflushedDescriptions = responses.map(function (res) {
- return res.description;
- });
- throw new Error('Unflushed requests: ' + responses.length + '\n ' +
- unflushedDescriptions.join('\n '));
- }
- };
- /**
- * @ngdoc method
- * @name $httpBackend#resetExpectations
- * @description
- * Resets all request expectations, but preserves all backend definitions. Typically, you would
- * call resetExpectations during a multiple-phase test when you want to reuse the same instance of
- * $httpBackend mock.
- */
- $httpBackend.resetExpectations = function () {
- expectations.length = 0;
- responses.length = 0;
- };
- $httpBackend.$$originalHttpBackend = originalHttpBackend;
- return $httpBackend;
- function createShortMethods(prefix) {
- angular.forEach(['GET', 'DELETE', 'JSONP', 'HEAD'], function (method) {
- $httpBackend[prefix + method] = function (url, headers, keys) {
- assertArgDefined(arguments, 0, 'url');
- // Change url to `null` if `undefined` to stop it throwing an exception further down
- if (angular.isUndefined(url)) url = null;
- return $httpBackend[prefix](method, url, undefined, headers, keys);
- };
- });
- angular.forEach(['PUT', 'POST', 'PATCH'], function (method) {
- $httpBackend[prefix + method] = function (url, data, headers, keys) {
- assertArgDefined(arguments, 0, 'url');
- // Change url to `null` if `undefined` to stop it throwing an exception further down
- if (angular.isUndefined(url)) url = null;
- return $httpBackend[prefix](method, url, data, headers, keys);
- };
- });
- }
- }
- function assertArgDefined(args, index, name) {
- if (args.length > index && angular.isUndefined(args[index])) {
- throw new Error('Undefined argument `' + name + '`; the argument is provided but not defined');
- }
- }
- function MockHttpExpectation(method, url, data, headers, keys) {
- function getUrlParams(u) {
- var params = u.slice(u.indexOf('?') + 1).split('&');
- return params.sort();
- }
- function compareUrl(u) {
- return (url.slice(0, url.indexOf('?')) === u.slice(0, u.indexOf('?')) &&
- getUrlParams(url).join() === getUrlParams(u).join());
- }
- this.data = data;
- this.headers = headers;
- this.match = function (m, u, d, h) {
- if (method !== m) return false;
- if (!this.matchUrl(u)) return false;
- if (angular.isDefined(d) && !this.matchData(d)) return false;
- if (angular.isDefined(h) && !this.matchHeaders(h)) return false;
- return true;
- };
- this.matchUrl = function (u) {
- if (!url) return true;
- if (angular.isFunction(url.test)) return url.test(u);
- if (angular.isFunction(url)) return url(u);
- return (url === u || compareUrl(u));
- };
- this.matchHeaders = function (h) {
- if (angular.isUndefined(headers)) return true;
- if (angular.isFunction(headers)) return headers(h);
- return angular.equals(headers, h);
- };
- this.matchData = function (d) {
- if (angular.isUndefined(data)) return true;
- if (data && angular.isFunction(data.test)) return data.test(d);
- if (data && angular.isFunction(data)) return data(d);
- if (data && !angular.isString(data)) {
- return angular.equals(angular.fromJson(angular.toJson(data)), angular.fromJson(d));
- }
- // eslint-disable-next-line eqeqeq
- return data == d;
- };
- this.toString = function () {
- return method + ' ' + url;
- };
- this.params = function (u) {
- return angular.extend(parseQuery(), pathParams());
- function pathParams() {
- var keyObj = {};
- if (!url || !angular.isFunction(url.test) || !keys || keys.length === 0) return keyObj;
- var m = url.exec(u);
- if (!m) return keyObj;
- for (var i = 1, len = m.length; i < len; ++i) {
- var key = keys[i - 1];
- var val = m[i];
- if (key && val) {
- keyObj[key.name || key] = val;
- }
- }
- return keyObj;
- }
- function parseQuery() {
- var obj = {}, key_value, key,
- queryStr = u.indexOf('?') > -1
- ? u.substring(u.indexOf('?') + 1)
- : '';
- angular.forEach(queryStr.split('&'), function (keyValue) {
- if (keyValue) {
- key_value = keyValue.replace(/\+/g, '%20').split('=');
- key = tryDecodeURIComponent(key_value[0]);
- if (angular.isDefined(key)) {
- var val = angular.isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
- if (!hasOwnProperty.call(obj, key)) {
- obj[key] = val;
- } else if (angular.isArray(obj[key])) {
- obj[key].push(val);
- } else {
- obj[key] = [obj[key], val];
- }
- }
- }
- });
- return obj;
- }
- function tryDecodeURIComponent(value) {
- try {
- return decodeURIComponent(value);
- } catch (e) {
- // Ignore any invalid uri component
- }
- }
- };
- }
- function createMockXhr() {
- return new MockXhr();
- }
- function MockXhr() {
- // hack for testing $http, $httpBackend
- MockXhr.$$lastInstance = this;
- this.open = function (method, url, async) {
- this.$$method = method;
- this.$$url = url;
- this.$$async = async;
- this.$$reqHeaders = {};
- this.$$respHeaders = {};
- };
- this.send = function (data) {
- this.$$data = data;
- };
- this.setRequestHeader = function (key, value) {
- this.$$reqHeaders[key] = value;
- };
- this.getResponseHeader = function (name) {
- // the lookup must be case insensitive,
- // that's why we try two quick lookups first and full scan last
- var header = this.$$respHeaders[name];
- if (header) return header;
- name = angular.lowercase(name);
- header = this.$$respHeaders[name];
- if (header) return header;
- header = undefined;
- angular.forEach(this.$$respHeaders, function (headerVal, headerName) {
- if (!header && angular.lowercase(headerName) === name) header = headerVal;
- });
- return header;
- };
- this.getAllResponseHeaders = function () {
- var lines = [];
- angular.forEach(this.$$respHeaders, function (value, key) {
- lines.push(key + ': ' + value);
- });
- return lines.join('\n');
- };
- this.abort = angular.noop;
- // This section simulates the events on a real XHR object (and the upload object)
- // When we are testing $httpBackend (inside the angular project) we make partial use of this
- // but store the events directly ourselves on `$$events`, instead of going through the `addEventListener`
- this.$$events = {};
- this.addEventListener = function (name, listener) {
- if (angular.isUndefined(this.$$events[name])) this.$$events[name] = [];
- this.$$events[name].push(listener);
- };
- this.upload = {
- $$events: {},
- addEventListener: this.addEventListener
- };
- }
- /**
- * @ngdoc service
- * @name $timeout
- * @description
- *
- * This service is just a simple decorator for {@link ng.$timeout $timeout} service
- * that adds a "flush" and "verifyNoPendingTasks" methods.
- */
- angular.mock.$TimeoutDecorator = ['$delegate', '$browser', function ($delegate, $browser) {
- /**
- * @ngdoc method
- * @name $timeout#flush
- * @description
- *
- * Flushes the queue of pending tasks.
- *
- * @param {number=} delay maximum timeout amount to flush up until
- */
- $delegate.flush = function (delay) {
- $browser.defer.flush(delay);
- };
- /**
- * @ngdoc method
- * @name $timeout#verifyNoPendingTasks
- * @description
- *
- * Verifies that there are no pending tasks that need to be flushed.
- */
- $delegate.verifyNoPendingTasks = function () {
- if ($browser.deferredFns.length) {
- throw new Error('Deferred tasks to flush (' + $browser.deferredFns.length + '): ' +
- formatPendingTasksAsString($browser.deferredFns));
- }
- };
- function formatPendingTasksAsString(tasks) {
- var result = [];
- angular.forEach(tasks, function (task) {
- result.push('{id: ' + task.id + ', time: ' + task.time + '}');
- });
- return result.join(', ');
- }
- return $delegate;
- }];
- angular.mock.$RAFDecorator = ['$delegate', function ($delegate) {
- var rafFn = function (fn) {
- var index = rafFn.queue.length;
- rafFn.queue.push(fn);
- return function () {
- rafFn.queue.splice(index, 1);
- };
- };
- rafFn.queue = [];
- rafFn.supported = $delegate.supported;
- rafFn.flush = function () {
- if (rafFn.queue.length === 0) {
- throw new Error('No rAF callbacks present');
- }
- var length = rafFn.queue.length;
- for (var i = 0; i < length; i++) {
- rafFn.queue[i]();
- }
- rafFn.queue = rafFn.queue.slice(i);
- };
- return rafFn;
- }];
- /**
- *
- */
- var originalRootElement;
- angular.mock.$RootElementProvider = function () {
- this.$get = ['$injector', function ($injector) {
- originalRootElement = angular.element('<div ng-app></div>').data('$injector', $injector);
- return originalRootElement;
- }];
- };
- /**
- * @ngdoc service
- * @name $controller
- * @description
- * A decorator for {@link ng.$controller} with additional `bindings` parameter, useful when testing
- * controllers of directives that use {@link $compile#-bindtocontroller- `bindToController`}.
- *
- * Depending on the value of
- * {@link ng.$compileProvider#preAssignBindingsEnabled `preAssignBindingsEnabled()`}, the properties
- * will be bound before or after invoking the constructor.
- *
- *
- * ## Example
- *
- * ```js
- *
- * // Directive definition ...
- *
- * myMod.directive('myDirective', {
- * controller: 'MyDirectiveController',
- * bindToController: {
- * name: '@'
- * }
- * });
- *
- *
- * // Controller definition ...
- *
- * myMod.controller('MyDirectiveController', ['$log', function($log) {
- * this.log = function() {
- * $log.info(this.name);
- * };
- * }]);
- *
- *
- * // In a test ...
- *
- * describe('myDirectiveController', function() {
- * describe('log()', function() {
- * it('should write the bound name to the log', inject(function($controller, $log) {
- * var ctrl = $controller('MyDirectiveController', { /* no locals */ }, { name: 'Clark Kent' });
- * ctrl.log();
- *
- * expect(ctrl.name).toEqual('Clark Kent');
- * expect($log.info.logs).toEqual(['Clark Kent']);
- * }));
- * });
- * });
- *
- * ```
- *
- * @param {Function|string} constructor If called with a function then it's considered to be the
- * controller constructor function. Otherwise it's considered to be a string which is used
- * to retrieve the controller constructor using the following steps:
- *
- * * check if a controller with given name is registered via `$controllerProvider`
- * * check if evaluating the string on the current scope returns a constructor
- * * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
- * `window` object (deprecated, not recommended)
- *
- * The string can use the `controller as property` syntax, where the controller instance is published
- * as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
- * to work correctly.
- *
- * @param {Object} locals Injection locals for Controller.
- * @param {Object=} bindings Properties to add to the controller instance. This is used to simulate
- * the `bindToController` feature and simplify certain kinds of tests.
- * @return {Object} Instance of given controller.
- */
- function createControllerDecorator(compileProvider) {
- angular.mock.$ControllerDecorator = ['$delegate', function ($delegate) {
- return function (expression, locals, later, ident) {
- if (later && typeof later === 'object') {
- var preAssignBindingsEnabled = compileProvider.preAssignBindingsEnabled();
- var instantiate = $delegate(expression, locals, true, ident);
- if (preAssignBindingsEnabled) {
- angular.extend(instantiate.instance, later);
- }
- var instance = instantiate();
- if (!preAssignBindingsEnabled || instance !== instantiate.instance) {
- angular.extend(instance, later);
- }
- return instance;
- }
- return $delegate(expression, locals, later, ident);
- };
- }];
- return angular.mock.$ControllerDecorator;
- }
- /**
- * @ngdoc service
- * @name $componentController
- * @description
- * A service that can be used to create instances of component controllers. Useful for unit-testing.
- *
- * Be aware that the controller will be instantiated and attached to the scope as specified in
- * the component definition object. If you do not provide a `$scope` object in the `locals` param
- * then the helper will create a new isolated scope as a child of `$rootScope`.
- *
- * If you are using `$element` or `$attrs` in the controller, make sure to provide them as `locals`.
- * The `$element` must be a jqLite-wrapped DOM element, and `$attrs` should be an object that
- * has all properties / functions that you are using in the controller. If this is getting too complex,
- * you should compile the component instead and access the component's controller via the
- * {@link angular.element#methods `controller`} function.
- *
- * See also the section on {@link guide/component#unit-testing-component-controllers unit-testing component controllers}
- * in the guide.
- *
- * @param {string} componentName the name of the component whose controller we want to instantiate
- * @param {Object} locals Injection locals for Controller.
- * @param {Object=} bindings Properties to add to the controller before invoking the constructor. This is used
- * to simulate the `bindToController` feature and simplify certain kinds of tests.
- * @param {string=} ident Override the property name to use when attaching the controller to the scope.
- * @return {Object} Instance of requested controller.
- */
- angular.mock.$ComponentControllerProvider = ['$compileProvider',
- function ComponentControllerProvider($compileProvider) {
- this.$get = ['$controller', '$injector', '$rootScope', function ($controller, $injector, $rootScope) {
- return function $componentController(componentName, locals, bindings, ident) {
- // get all directives associated to the component name
- var directives = $injector.get(componentName + 'Directive');
- // look for those directives that are components
- var candidateDirectives = directives.filter(function (directiveInfo) {
- // components have controller, controllerAs and restrict:'E'
- return directiveInfo.controller && directiveInfo.controllerAs && directiveInfo.restrict === 'E';
- });
- // check if valid directives found
- if (candidateDirectives.length === 0) {
- throw new Error('No component found');
- }
- if (candidateDirectives.length > 1) {
- throw new Error('Too many components found');
- }
- // get the info of the component
- var directiveInfo = candidateDirectives[0];
- // create a scope if needed
- locals = locals || {};
- locals.$scope = locals.$scope || $rootScope.$new(true);
- return $controller(directiveInfo.controller, locals, bindings, ident || directiveInfo.controllerAs);
- };
- }];
- }];
- /**
- * @ngdoc module
- * @name ngMock
- * @packageName angular-mocks
- * @description
- *
- * # ngMock
- *
- * The `ngMock` module provides support to inject and mock Angular services into unit tests.
- * In addition, ngMock also extends various core ng services such that they can be
- * inspected and controlled in a synchronous manner within test code.
- *
- *
- * <div doc-module-components="ngMock"></div>
- *
- * @installation
- *
- * First, download the file:
- * * [Google CDN](https://developers.google.com/speed/libraries/devguide#angularjs) e.g.
- * `"//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-mocks.js"`
- * * [NPM](https://www.npmjs.com/) e.g. `npm install angular-mocks@X.Y.Z`
- * * [Yarn](https://yarnpkg.com) e.g. `yarn add angular-mocks@X.Y.Z`
- * * [Bower](http://bower.io) e.g. `bower install angular-mocks#X.Y.Z`
- * * [code.angularjs.org](https://code.angularjs.org/) (discouraged for production use) e.g.
- * `"//code.angularjs.org/X.Y.Z/angular-mocks.js"`
- *
- * where X.Y.Z is the AngularJS version you are running.
- *
- * Then, configure your test runner to load `angular-mocks.js` after `angular.js`.
- * This example uses <a href="http://karma-runner.github.io/">Karma</a>:
- *
- * ```
- * config.set({
- * files: [
- * 'build/angular.js', // and other module files you need
- * 'build/angular-mocks.js',
- * '<path/to/application/files>',
- * '<path/to/spec/files>'
- * ]
- * });
- * ```
- *
- * Including the `angular-mocks.js` file automatically adds the `ngMock` module, so your tests
- * are ready to go!
- */
- angular.module('ngMock', ['ng']).provider({
- $browser: angular.mock.$BrowserProvider,
- $exceptionHandler: angular.mock.$ExceptionHandlerProvider,
- $log: angular.mock.$LogProvider,
- $interval: angular.mock.$IntervalProvider,
- $rootElement: angular.mock.$RootElementProvider,
- $componentController: angular.mock.$ComponentControllerProvider
- }).config(['$provide', '$compileProvider', function ($provide, $compileProvider) {
- $provide.decorator('$timeout', angular.mock.$TimeoutDecorator);
- $provide.decorator('$$rAF', angular.mock.$RAFDecorator);
- $provide.decorator('$rootScope', angular.mock.$RootScopeDecorator);
- $provide.decorator('$controller', createControllerDecorator($compileProvider));
- $provide.decorator('$httpBackend', angular.mock.$httpBackendDecorator);
- }]).info({angularVersion: '1.6.5'});
- /**
- * @ngdoc module
- * @name ngMockE2E
- * @module ngMockE2E
- * @packageName angular-mocks
- * @description
- *
- * The `ngMockE2E` is an angular module which contains mocks suitable for end-to-end testing.
- * Currently there is only one mock present in this module -
- * the {@link ngMockE2E.$httpBackend e2e $httpBackend} mock.
- */
- angular.module('ngMockE2E', ['ng']).config(['$provide', function ($provide) {
- $provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
- }]).info({angularVersion: '1.6.5'});
- /**
- * @ngdoc service
- * @name $httpBackend
- * @module ngMockE2E
- * @description
- * Fake HTTP backend implementation suitable for end-to-end testing or backend-less development of
- * applications that use the {@link ng.$http $http service}.
- *
- * <div class="alert alert-info">
- * **Note**: For fake http backend implementation suitable for unit testing please see
- * {@link ngMock.$httpBackend unit-testing $httpBackend mock}.
- * </div>
- *
- * This implementation can be used to respond with static or dynamic responses via the `when` api
- * and its shortcuts (`whenGET`, `whenPOST`, etc) and optionally pass through requests to the
- * real $httpBackend for specific requests (e.g. to interact with certain remote apis or to fetch
- * templates from a webserver).
- *
- * As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application
- * is being developed with the real backend api replaced with a mock, it is often desirable for
- * certain category of requests to bypass the mock and issue a real http request (e.g. to fetch
- * templates or static files from the webserver). To configure the backend with this behavior
- * use the `passThrough` request handler of `when` instead of `respond`.
- *
- * Additionally, we don't want to manually have to flush mocked out requests like we do during unit
- * testing. For this reason the e2e $httpBackend flushes mocked out requests
- * automatically, closely simulating the behavior of the XMLHttpRequest object.
- *
- * To setup the application to run with this http backend, you have to create a module that depends
- * on the `ngMockE2E` and your application modules and defines the fake backend:
- *
- * ```js
- * var myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
- * myAppDev.run(function($httpBackend) {
- * var phones = [{name: 'phone1'}, {name: 'phone2'}];
- *
- * // returns the current list of phones
- * $httpBackend.whenGET('/phones').respond(phones);
- *
- * // adds a new phone to the phones array
- * $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
- * var phone = angular.fromJson(data);
- * phones.push(phone);
- * return [200, phone, {}];
- * });
- * $httpBackend.whenGET(/^\/templates\//).passThrough(); // Requests for templates are handled by the real server
- * //...
- * });
- * ```
- *
- * Afterwards, bootstrap your app with this new module.
- *
- * ## Example
- * <example name="httpbackend-e2e-testing" module="myAppE2E" deps="angular-mocks.js">
- * <file name="app.js">
- * var myApp = angular.module('myApp', []);
- *
- * myApp.controller('MainCtrl', function MainCtrl($http) {
- * var ctrl = this;
- *
- * ctrl.phones = [];
- * ctrl.newPhone = {
- * name: ''
- * };
- *
- * ctrl.getPhones = function() {
- * $http.get('/phones').then(function(response) {
- * ctrl.phones = response.data;
- * });
- * };
- *
- * ctrl.addPhone = function(phone) {
- * $http.post('/phones', phone).then(function() {
- * ctrl.newPhone = {name: ''};
- * return ctrl.getPhones();
- * });
- * };
- *
- * ctrl.getPhones();
- * });
- * </file>
- * <file name="e2e.js">
- * var myAppDev = angular.module('myAppE2E', ['myApp', 'ngMockE2E']);
- *
- * myAppDev.run(function($httpBackend) {
- * var phones = [{name: 'phone1'}, {name: 'phone2'}];
- *
- * // returns the current list of phones
- * $httpBackend.whenGET('/phones').respond(phones);
- *
- * // adds a new phone to the phones array
- * $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
- * var phone = angular.fromJson(data);
- * phones.push(phone);
- * return [200, phone, {}];
- * });
- * });
- * </file>
- * <file name="index.html">
- * <div ng-controller="MainCtrl as $ctrl">
- * <form name="newPhoneForm" ng-submit="$ctrl.addPhone($ctrl.newPhone)">
- * <input type="text" ng-model="$ctrl.newPhone.name">
- * <input type="submit" value="Add Phone">
- * </form>
- * <h1>Phones</h1>
- * <ul>
- * <li ng-repeat="phone in $ctrl.phones">{{phone.name}}</li>
- * </ul>
- * </div>
- * </file>
- * </example>
- *
- *
- */
- /**
- * @ngdoc method
- * @name $httpBackend#when
- * @module ngMockE2E
- * @description
- * Creates a new backend definition.
- *
- * @param {string} method HTTP method.
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers or function that receives http header
- * object and returns true if the headers match the current definition.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- *
- * - respond –
- * ```
- * { function([status,] data[, headers, statusText])
- * | function(function(method, url, data, headers, params)}
- * ```
- * – The respond method takes a set of static data to be returned or a function that can return
- * an array containing response status (number), response data (Array|Object|string), response
- * headers (Object), and the text for the status (string).
- * - passThrough – `{function()}` – Any request matching a backend definition with
- * `passThrough` handler will be passed through to the real backend (an XHR request will be made
- * to the server.)
- * - Both methods return the `requestHandler` object for possible overrides.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenGET
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for GET requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenHEAD
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for HEAD requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenDELETE
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for DELETE requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenPOST
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for POST requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenPUT
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for PUT requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenPATCH
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for PATCH requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(string|RegExp|function(string))=} data HTTP request body or function that receives
- * data string and returns true if the data is as expected.
- * @param {(Object|function(Object))=} headers HTTP headers.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenJSONP
- * @module ngMockE2E
- * @description
- * Creates a new backend definition for JSONP requests. For more info see `when()`.
- *
- * @param {string|RegExp|function(string)=} url HTTP url or function that receives a url
- * and returns true if the url matches the current definition.
- * @param {(Array)=} keys Array of keys to assign to regex matches in request url described on
- * {@link ngMock.$httpBackend $httpBackend mock}.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- /**
- * @ngdoc method
- * @name $httpBackend#whenRoute
- * @module ngMockE2E
- * @description
- * Creates a new backend definition that compares only with the requested route.
- *
- * @param {string} method HTTP method.
- * @param {string} url HTTP url string that supports colon param matching.
- * @returns {requestHandler} Returns an object with `respond` and `passThrough` methods that
- * control how a matched request is handled. You can save this object for later use and invoke
- * `respond` or `passThrough` again in order to change how a matched request is handled.
- */
- angular.mock.e2e = {};
- angular.mock.e2e.$httpBackendDecorator =
- ['$rootScope', '$timeout', '$delegate', '$browser', createHttpBackendMock];
- /**
- * @ngdoc type
- * @name $rootScope.Scope
- * @module ngMock
- * @description
- * {@link ng.$rootScope.Scope Scope} type decorated with helper methods useful for testing. These
- * methods are automatically available on any {@link ng.$rootScope.Scope Scope} instance when
- * `ngMock` module is loaded.
- *
- * In addition to all the regular `Scope` methods, the following helper methods are available:
- */
- angular.mock.$RootScopeDecorator = ['$delegate', function ($delegate) {
- var $rootScopePrototype = Object.getPrototypeOf($delegate);
- $rootScopePrototype.$countChildScopes = countChildScopes;
- $rootScopePrototype.$countWatchers = countWatchers;
- return $delegate;
- // ------------------------------------------------------------------------------------------ //
- /**
- * @ngdoc method
- * @name $rootScope.Scope#$countChildScopes
- * @module ngMock
- * @this $rootScope.Scope
- * @description
- * Counts all the direct and indirect child scopes of the current scope.
- *
- * The current scope is excluded from the count. The count includes all isolate child scopes.
- *
- * @returns {number} Total number of child scopes.
- */
- function countChildScopes() {
- var count = 0; // exclude the current scope
- var pendingChildHeads = [this.$$childHead];
- var currentScope;
- while (pendingChildHeads.length) {
- currentScope = pendingChildHeads.shift();
- while (currentScope) {
- count += 1;
- pendingChildHeads.push(currentScope.$$childHead);
- currentScope = currentScope.$$nextSibling;
- }
- }
- return count;
- }
- /**
- * @ngdoc method
- * @name $rootScope.Scope#$countWatchers
- * @this $rootScope.Scope
- * @module ngMock
- * @description
- * Counts all the watchers of direct and indirect child scopes of the current scope.
- *
- * The watchers of the current scope are included in the count and so are all the watchers of
- * isolate child scopes.
- *
- * @returns {number} Total number of watchers.
- */
- function countWatchers() {
- var count = this.$$watchers ? this.$$watchers.length : 0; // include the current scope
- var pendingChildHeads = [this.$$childHead];
- var currentScope;
- while (pendingChildHeads.length) {
- currentScope = pendingChildHeads.shift();
- while (currentScope) {
- count += currentScope.$$watchers ? currentScope.$$watchers.length : 0;
- pendingChildHeads.push(currentScope.$$childHead);
- currentScope = currentScope.$$nextSibling;
- }
- }
- return count;
- }
- }];
- (function (jasmineOrMocha) {
- if (!jasmineOrMocha) {
- return;
- }
- var currentSpec = null,
- injectorState = new InjectorState(),
- annotatedFunctions = [],
- wasInjectorCreated = function () {
- return !!currentSpec;
- };
- angular.mock.$$annotate = angular.injector.$$annotate;
- angular.injector.$$annotate = function (fn) {
- if (typeof fn === 'function' && !fn.$inject) {
- annotatedFunctions.push(fn);
- }
- return angular.mock.$$annotate.apply(this, arguments);
- };
- /**
- * @ngdoc function
- * @name angular.mock.module
- * @description
- *
- * *NOTE*: This function is also published on window for easy access.<br>
- * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
- *
- * This function registers a module configuration code. It collects the configuration information
- * which will be used when the injector is created by {@link angular.mock.inject inject}.
- *
- * See {@link angular.mock.inject inject} for usage example
- *
- * @param {...(string|Function|Object)} fns any number of modules which are represented as string
- * aliases or as anonymous module initialization functions. The modules are used to
- * configure the injector. The 'ng' and 'ngMock' modules are automatically loaded. If an
- * object literal is passed each key-value pair will be registered on the module via
- * {@link auto.$provide $provide}.value, the key being the string name (or token) to associate
- * with the value on the injector.
- */
- var module = window.module = angular.mock.module = function () {
- var moduleFns = Array.prototype.slice.call(arguments, 0);
- return wasInjectorCreated() ? workFn() : workFn;
- /////////////////////
- function workFn() {
- if (currentSpec.$injector) {
- throw new Error('Injector already created, can not register a module!');
- } else {
- var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);
- angular.forEach(moduleFns, function (module) {
- if (angular.isObject(module) && !angular.isArray(module)) {
- fn = ['$provide', function ($provide) {
- angular.forEach(module, function (value, key) {
- $provide.value(key, value);
- });
- }];
- } else {
- fn = module;
- }
- if (currentSpec.$providerInjector) {
- currentSpec.$providerInjector.invoke(fn);
- } else {
- modules.push(fn);
- }
- });
- }
- }
- };
- module.$$beforeAllHook = (window.before || window.beforeAll);
- module.$$afterAllHook = (window.after || window.afterAll);
- // purely for testing ngMock itself
- module.$$currentSpec = function (to) {
- if (arguments.length === 0) return to;
- currentSpec = to;
- };
- /**
- * @ngdoc function
- * @name angular.mock.module.sharedInjector
- * @description
- *
- * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
- *
- * This function ensures a single injector will be used for all tests in a given describe context.
- * This contrasts with the default behaviour where a new injector is created per test case.
- *
- * Use sharedInjector when you want to take advantage of Jasmine's `beforeAll()`, or mocha's
- * `before()` methods. Call `module.sharedInjector()` before you setup any other hooks that
- * will create (i.e call `module()`) or use (i.e call `inject()`) the injector.
- *
- * You cannot call `sharedInjector()` from within a context already using `sharedInjector()`.
- *
- * ## Example
- *
- * Typically beforeAll is used to make many assertions about a single operation. This can
- * cut down test run-time as the test setup doesn't need to be re-run, and enabling focussed
- * tests each with a single assertion.
- *
- * ```js
- * describe("Deep Thought", function() {
- *
- * module.sharedInjector();
- *
- * beforeAll(module("UltimateQuestion"));
- *
- * beforeAll(inject(function(DeepThought) {
- * expect(DeepThought.answer).toBeUndefined();
- * DeepThought.generateAnswer();
- * }));
- *
- * it("has calculated the answer correctly", inject(function(DeepThought) {
- * // Because of sharedInjector, we have access to the instance of the DeepThought service
- * // that was provided to the beforeAll() hook. Therefore we can test the generated answer
- * expect(DeepThought.answer).toBe(42);
- * }));
- *
- * it("has calculated the answer within the expected time", inject(function(DeepThought) {
- * expect(DeepThought.runTimeMillennia).toBeLessThan(8000);
- * }));
- *
- * it("has double checked the answer", inject(function(DeepThought) {
- * expect(DeepThought.absolutelySureItIsTheRightAnswer).toBe(true);
- * }));
- *
- * });
- *
- * ```
- */
- module.sharedInjector = function () {
- if (!(module.$$beforeAllHook && module.$$afterAllHook)) {
- throw Error('sharedInjector() cannot be used unless your test runner defines beforeAll/afterAll');
- }
- var initialized = false;
- module.$$beforeAllHook(/** @this */ function () {
- if (injectorState.shared) {
- injectorState.sharedError = Error('sharedInjector() cannot be called inside a context that has already called sharedInjector()');
- throw injectorState.sharedError;
- }
- initialized = true;
- currentSpec = this;
- injectorState.shared = true;
- });
- module.$$afterAllHook(function () {
- if (initialized) {
- injectorState = new InjectorState();
- module.$$cleanup();
- } else {
- injectorState.sharedError = null;
- }
- });
- };
- module.$$beforeEach = function () {
- if (injectorState.shared && currentSpec && currentSpec !== this) {
- var state = currentSpec;
- currentSpec = this;
- angular.forEach(['$injector', '$modules', '$providerInjector', '$injectorStrict'], function (k) {
- currentSpec[k] = state[k];
- state[k] = null;
- });
- } else {
- currentSpec = this;
- originalRootElement = null;
- annotatedFunctions = [];
- }
- };
- module.$$afterEach = function () {
- if (injectorState.cleanupAfterEach()) {
- module.$$cleanup();
- }
- };
- module.$$cleanup = function () {
- var injector = currentSpec.$injector;
- annotatedFunctions.forEach(function (fn) {
- delete fn.$inject;
- });
- currentSpec.$injector = null;
- currentSpec.$modules = null;
- currentSpec.$providerInjector = null;
- currentSpec = null;
- if (injector) {
- // Ensure `$rootElement` is instantiated, before checking `originalRootElement`
- var $rootElement = injector.get('$rootElement');
- var rootNode = $rootElement && $rootElement[0];
- var cleanUpNodes = !originalRootElement ? [] : [originalRootElement[0]];
- if (rootNode && (!originalRootElement || rootNode !== originalRootElement[0])) {
- cleanUpNodes.push(rootNode);
- }
- angular.element.cleanData(cleanUpNodes);
- // Ensure `$destroy()` is available, before calling it
- // (a mocked `$rootScope` might not implement it (or not even be an object at all))
- var $rootScope = injector.get('$rootScope');
- if ($rootScope && $rootScope.$destroy) $rootScope.$destroy();
- }
- // clean up jquery's fragment cache
- angular.forEach(angular.element.fragments, function (val, key) {
- delete angular.element.fragments[key];
- });
- MockXhr.$$lastInstance = null;
- angular.forEach(angular.callbacks, function (val, key) {
- delete angular.callbacks[key];
- });
- angular.callbacks.$$counter = 0;
- };
- (window.beforeEach || window.setup)(module.$$beforeEach);
- (window.afterEach || window.teardown)(module.$$afterEach);
- /**
- * @ngdoc function
- * @name angular.mock.inject
- * @description
- *
- * *NOTE*: This function is also published on window for easy access.<br>
- * *NOTE*: This function is declared ONLY WHEN running tests with jasmine or mocha
- *
- * The inject function wraps a function into an injectable function. The inject() creates new
- * instance of {@link auto.$injector $injector} per test, which is then used for
- * resolving references.
- *
- *
- * ## Resolving References (Underscore Wrapping)
- * Often, we would like to inject a reference once, in a `beforeEach()` block and reuse this
- * in multiple `it()` clauses. To be able to do this we must assign the reference to a variable
- * that is declared in the scope of the `describe()` block. Since we would, most likely, want
- * the variable to have the same name of the reference we have a problem, since the parameter
- * to the `inject()` function would hide the outer variable.
- *
- * To help with this, the injected parameters can, optionally, be enclosed with underscores.
- * These are ignored by the injector when the reference name is resolved.
- *
- * For example, the parameter `_myService_` would be resolved as the reference `myService`.
- * Since it is available in the function body as `_myService_`, we can then assign it to a variable
- * defined in an outer scope.
- *
- * ```
- * // Defined out reference variable outside
- * var myService;
- *
- * // Wrap the parameter in underscores
- * beforeEach( inject( function(_myService_){
- * myService = _myService_;
- * }));
- *
- * // Use myService in a series of tests.
- * it('makes use of myService', function() {
- * myService.doStuff();
- * });
- *
- * ```
- *
- * See also {@link angular.mock.module angular.mock.module}
- *
- * ## Example
- * Example of what a typical jasmine tests looks like with the inject method.
- * ```js
- *
- * angular.module('myApplicationModule', [])
- * .value('mode', 'app')
- * .value('version', 'v1.0.1');
- *
- *
- * describe('MyApp', function() {
- *
- * // You need to load modules that you want to test,
- * // it loads only the "ng" module by default.
- * beforeEach(module('myApplicationModule'));
- *
- *
- * // inject() is used to inject arguments of all given functions
- * it('should provide a version', inject(function(mode, version) {
- * expect(version).toEqual('v1.0.1');
- * expect(mode).toEqual('app');
- * }));
- *
- *
- * // The inject and module method can also be used inside of the it or beforeEach
- * it('should override a version and test the new version is injected', function() {
- * // module() takes functions or strings (module aliases)
- * module(function($provide) {
- * $provide.value('version', 'overridden'); // override version here
- * });
- *
- * inject(function(version) {
- * expect(version).toEqual('overridden');
- * });
- * });
- * });
- *
- * ```
- *
- * @param {...Function} fns any number of functions which will be injected using the injector.
- */
- var ErrorAddingDeclarationLocationStack = function ErrorAddingDeclarationLocationStack(e, errorForStack) {
- this.message = e.message;
- this.name = e.name;
- if (e.line) this.line = e.line;
- if (e.sourceId) this.sourceId = e.sourceId;
- if (e.stack && errorForStack)
- this.stack = e.stack + '\n' + errorForStack.stack;
- if (e.stackArray) this.stackArray = e.stackArray;
- };
- ErrorAddingDeclarationLocationStack.prototype = Error.prototype;
- window.inject = angular.mock.inject = function () {
- var blockFns = Array.prototype.slice.call(arguments, 0);
- var errorForStack = new Error('Declaration Location');
- // IE10+ and PhanthomJS do not set stack trace information, until the error is thrown
- if (!errorForStack.stack) {
- try {
- throw errorForStack;
- } catch (e) { /* empty */
- }
- }
- return wasInjectorCreated() ? WorkFn.call(currentSpec) : WorkFn;
- /////////////////////
- function WorkFn() {
- var modules = currentSpec.$modules || [];
- var strictDi = !!currentSpec.$injectorStrict;
- modules.unshift(['$injector', function ($injector) {
- currentSpec.$providerInjector = $injector;
- }]);
- modules.unshift('ngMock');
- modules.unshift('ng');
- var injector = currentSpec.$injector;
- if (!injector) {
- if (strictDi) {
- // If strictDi is enabled, annotate the providerInjector blocks
- angular.forEach(modules, function (moduleFn) {
- if (typeof moduleFn === 'function') {
- angular.injector.$$annotate(moduleFn);
- }
- });
- }
- injector = currentSpec.$injector = angular.injector(modules, strictDi);
- currentSpec.$injectorStrict = strictDi;
- }
- for (var i = 0, ii = blockFns.length; i < ii; i++) {
- if (currentSpec.$injectorStrict) {
- // If the injector is strict / strictDi, and the spec wants to inject using automatic
- // annotation, then annotate the function here.
- injector.annotate(blockFns[i]);
- }
- try {
- injector.invoke(blockFns[i] || angular.noop, this);
- } catch (e) {
- if (e.stack && errorForStack) {
- throw new ErrorAddingDeclarationLocationStack(e, errorForStack);
- }
- throw e;
- } finally {
- errorForStack = null;
- }
- }
- }
- };
- angular.mock.inject.strictDi = function (value) {
- value = arguments.length ? !!value : true;
- return wasInjectorCreated() ? workFn() : workFn;
- function workFn() {
- if (value !== currentSpec.$injectorStrict) {
- if (currentSpec.$injector) {
- throw new Error('Injector already created, can not modify strict annotations');
- } else {
- currentSpec.$injectorStrict = value;
- }
- }
- }
- };
- function InjectorState() {
- this.shared = false;
- this.sharedError = null;
- this.cleanupAfterEach = function () {
- return !this.shared || this.sharedError;
- };
- }
- })(window.jasmine || window.mocha);
- 'use strict';
- (function () {
- /**
- * Triggers a browser event. Attempts to choose the right event if one is
- * not specified.
- *
- * @param {Object} element Either a wrapped jQuery/jqLite node or a DOMElement
- * @param {string} eventType Optional event type
- * @param {Object=} eventData An optional object which contains additional event data (such as x,y
- * coordinates, keys, etc...) that are passed into the event when triggered
- */
- window.browserTrigger = function browserTrigger(element, eventType, eventData) {
- if (element && !element.nodeName) element = element[0];
- if (!element) return;
- eventData = eventData || {};
- var relatedTarget = eventData.relatedTarget || element;
- var keys = eventData.keys;
- var x = eventData.x;
- var y = eventData.y;
- var inputType = (element.type) ? element.type.toLowerCase() : null,
- nodeName = element.nodeName.toLowerCase();
- if (!eventType) {
- eventType = {
- 'text': 'change',
- 'textarea': 'change',
- 'hidden': 'change',
- 'password': 'change',
- 'button': 'click',
- 'submit': 'click',
- 'reset': 'click',
- 'image': 'click',
- 'checkbox': 'click',
- 'radio': 'click',
- 'select-one': 'change',
- 'select-multiple': 'change',
- '_default_': 'click'
- }[inputType || '_default_'];
- }
- if (nodeName === 'option') {
- element.parentNode.value = element.value;
- element = element.parentNode;
- eventType = 'change';
- }
- keys = keys || [];
- function pressed(key) {
- return keys.indexOf(key) !== -1;
- }
- var evnt;
- if (/transitionend/.test(eventType)) {
- if (window.WebKitTransitionEvent) {
- evnt = new window.WebKitTransitionEvent(eventType, eventData);
- evnt.initEvent(eventType, false, true);
- } else {
- try {
- evnt = new window.TransitionEvent(eventType, eventData);
- } catch (e) {
- evnt = window.document.createEvent('TransitionEvent');
- evnt.initTransitionEvent(eventType, null, null, null, eventData.elapsedTime || 0);
- }
- }
- } else if (/animationend/.test(eventType)) {
- if (window.WebKitAnimationEvent) {
- evnt = new window.WebKitAnimationEvent(eventType, eventData);
- evnt.initEvent(eventType, false, true);
- } else {
- try {
- evnt = new window.AnimationEvent(eventType, eventData);
- } catch (e) {
- evnt = window.document.createEvent('AnimationEvent');
- evnt.initAnimationEvent(eventType, null, null, null, eventData.elapsedTime || 0);
- }
- }
- } else if (/touch/.test(eventType) && supportsTouchEvents()) {
- evnt = createTouchEvent(element, eventType, x, y);
- } else if (/key/.test(eventType)) {
- evnt = window.document.createEvent('Events');
- evnt.initEvent(eventType, eventData.bubbles, eventData.cancelable);
- evnt.view = window;
- evnt.ctrlKey = pressed('ctrl');
- evnt.altKey = pressed('alt');
- evnt.shiftKey = pressed('shift');
- evnt.metaKey = pressed('meta');
- evnt.keyCode = eventData.keyCode;
- evnt.charCode = eventData.charCode;
- evnt.which = eventData.which;
- } else {
- evnt = window.document.createEvent('MouseEvents');
- x = x || 0;
- y = y || 0;
- evnt.initMouseEvent(eventType, true, true, window, 0, x, y, x, y, pressed('ctrl'),
- pressed('alt'), pressed('shift'), pressed('meta'), 0, relatedTarget);
- }
- /* we're unable to change the timeStamp value directly so this
- * is only here to allow for testing where the timeStamp value is
- * read */
- evnt.$manualTimeStamp = eventData.timeStamp;
- if (!evnt) return;
- var originalPreventDefault = evnt.preventDefault,
- appWindow = element.ownerDocument.defaultView,
- fakeProcessDefault = true,
- finalProcessDefault,
- angular = appWindow.angular || {};
- // igor: temporary fix for https://bugzilla.mozilla.org/show_bug.cgi?id=684208
- angular['ff-684208-preventDefault'] = false;
- evnt.preventDefault = function () {
- fakeProcessDefault = false;
- return originalPreventDefault.apply(evnt, arguments);
- };
- if (!eventData.bubbles || supportsEventBubblingInDetachedTree() || isAttachedToDocument(element)) {
- element.dispatchEvent(evnt);
- } else {
- triggerForPath(element, evnt);
- }
- finalProcessDefault = !(angular['ff-684208-preventDefault'] || !fakeProcessDefault);
- delete angular['ff-684208-preventDefault'];
- return finalProcessDefault;
- };
- function supportsTouchEvents() {
- if ('_cached' in supportsTouchEvents) {
- return supportsTouchEvents._cached;
- }
- if (!window.document.createTouch || !window.document.createTouchList) {
- supportsTouchEvents._cached = false;
- return false;
- }
- try {
- window.document.createEvent('TouchEvent');
- } catch (e) {
- supportsTouchEvents._cached = false;
- return false;
- }
- supportsTouchEvents._cached = true;
- return true;
- }
- function createTouchEvent(element, eventType, x, y) {
- var evnt = new window.Event(eventType);
- x = x || 0;
- y = y || 0;
- var touch = window.document.createTouch(window, element, Date.now(), x, y, x, y);
- var touches = window.document.createTouchList(touch);
- evnt.touches = touches;
- return evnt;
- }
- function supportsEventBubblingInDetachedTree() {
- if ('_cached' in supportsEventBubblingInDetachedTree) {
- return supportsEventBubblingInDetachedTree._cached;
- }
- supportsEventBubblingInDetachedTree._cached = false;
- var doc = window.document;
- if (doc) {
- var parent = doc.createElement('div'),
- child = parent.cloneNode();
- parent.appendChild(child);
- parent.addEventListener('e', function () {
- supportsEventBubblingInDetachedTree._cached = true;
- });
- var evnt = window.document.createEvent('Events');
- evnt.initEvent('e', true, true);
- child.dispatchEvent(evnt);
- }
- return supportsEventBubblingInDetachedTree._cached;
- }
- function triggerForPath(element, evnt) {
- var stop = false;
- var _stopPropagation = evnt.stopPropagation;
- evnt.stopPropagation = function () {
- stop = true;
- _stopPropagation.apply(evnt, arguments);
- };
- patchEventTargetForBubbling(evnt, element);
- do {
- element.dispatchEvent(evnt);
- // eslint-disable-next-line no-unmodified-loop-condition
- } while (!stop && (element = element.parentNode));
- }
- function patchEventTargetForBubbling(event, target) {
- event._target = target;
- Object.defineProperty(event, 'target', {
- get: function () {
- return this._target;
- }
- });
- }
- function isAttachedToDocument(element) {
- while ((element = element.parentNode)) {
- if (element === window) {
- return true;
- }
- }
- return false;
- }
- })();
- })(window, window.angular);
|