1- // 'use strict';
2- //
3- // describe('filterWatcherProvider', function() {
4- //
5- // //helpers
6- // function n(n) { return n; }
7- // var stub = { fn: function(x) { return n(x) } };
8- //
9- // beforeEach(module('a8m.filter-watcher'));
10- //
11- // it('should have 2 main functions `isMemoized` and `memozie`', inject(function(filterWatcher) {
12- // expect(filterWatcher.isMemoized).toEqual(jasmine.any(Function));
13- // expect(filterWatcher.memoize).toEqual(jasmine.any(Function));
14- // }));
15- //
16- // it('should called the function if it\'s not cached',
17- // inject(function(filterWatcher) {
18- // var spy = spyOn(stub, 'fn');
19- // (function memoizedOnly(n) {
20- // return filterWatcher.isMemoized('fName',n) || stub.fn(n);
21- // })();
22- // expect(spy).toHaveBeenCalled();
23- // expect(spy.callCount).toEqual(1);
24- // }));
25- //
26- // it('should get the result from cache if it\'s memoize',
27- // inject(function(filterWatcher, $rootScope) {
28- // var scope = $rootScope.$new();
29- // var spy = spyOn(stub, 'fn').andCallFake(function() {
30- // return 1;
31- // });
32- // function memoize(n) {
33- // return filterWatcher.isMemoized('fName', n) ||
34- // filterWatcher.memoize('fName', n, scope, stub.fn(n));
35- // }
36- // [1,1,1,1,4,4,4,4,4].forEach(function(el) {
37- // memoize(el);
38- // });
39- // expect(spy).toHaveBeenCalled();
40- // expect(spy.callCount).toEqual(2);
41- // }));
42- //
43- // it('should clear cache from scope listeners on `$destroy`',
44- // inject(function(filterWatcher, $rootScope) {
45- // var scope;
46- // var spy = spyOn(stub, 'fn').andCallFake(function() {
47- // return 1;
48- // });
49- // function memoize(n) {
50- // return filterWatcher.isMemoized('fName', n) ||
51- // filterWatcher.memoize('fName', n, scope = $rootScope.$new(), stub.fn(n));
52- // }
53- // [1,1,1,1,1,1,1,1,1,1].forEach(function(el) {
54- // memoize(el);
55- // scope.$destroy();
56- // });
57- // expect(spy.callCount).toEqual(10);
58- // }));
59- // });
1+ 'use strict' ;
2+
3+ describe ( 'filterWatcherProvider' , function ( ) {
4+
5+ //helpers
6+ function n ( n ) { return n ; }
7+ var stub = { fn : function ( x ) { return n ( x ) } } ;
8+
9+ beforeEach ( module ( 'a8m.filter-watcher' ) ) ;
10+
11+ it ( 'should have 2 main functions `isMemoized` and `memozie`' , inject ( function ( filterWatcher ) {
12+ expect ( filterWatcher . isMemoized ) . toEqual ( jasmine . any ( Function ) ) ;
13+ expect ( filterWatcher . memoize ) . toEqual ( jasmine . any ( Function ) ) ;
14+ } ) ) ;
15+
16+ it ( 'should called the function if it\'s not cached' ,
17+ inject ( function ( filterWatcher ) {
18+ var spy = spyOn ( stub , 'fn' ) ;
19+ ( function memoizedOnly ( n ) {
20+ return filterWatcher . isMemoized ( 'fName' , n ) || stub . fn ( n ) ;
21+ } ) ( ) ;
22+ expect ( spy ) . toHaveBeenCalled ( ) ;
23+ expect ( spy . callCount ) . toEqual ( 1 ) ;
24+ } ) ) ;
25+
26+ it ( 'should get the result from cache if it\'s memoize' ,
27+ inject ( function ( filterWatcher , $rootScope ) {
28+ var scope = $rootScope . $new ( ) ;
29+ var spy = spyOn ( stub , 'fn' ) . andCallFake ( function ( ) {
30+ return 1 ;
31+ } ) ;
32+ function memoize ( n ) {
33+ return filterWatcher . isMemoized ( 'fName' , n ) ||
34+ filterWatcher . memoize ( 'fName' , n , scope , stub . fn ( n ) ) ;
35+ }
36+ [ 1 , 1 , 1 , 1 , 4 , 4 , 4 , 4 , 4 ] . forEach ( function ( el ) {
37+ memoize ( el ) ;
38+ } ) ;
39+ expect ( spy ) . toHaveBeenCalled ( ) ;
40+ expect ( spy . callCount ) . toEqual ( 2 ) ;
41+ } ) ) ;
42+
43+ it ( 'should clear cache from scope listeners on `$destroy`' ,
44+ inject ( function ( filterWatcher , $rootScope ) {
45+ var scope ;
46+ var spy = spyOn ( stub , 'fn' ) . andCallFake ( function ( ) {
47+ return 1 ;
48+ } ) ;
49+ function memoize ( n ) {
50+ return filterWatcher . isMemoized ( 'fName' , n ) ||
51+ filterWatcher . memoize ( 'fName' , n , scope = $rootScope . $new ( ) , stub . fn ( n ) ) ;
52+ }
53+ [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] . forEach ( function ( el ) {
54+ memoize ( el ) ;
55+ scope . $destroy ( ) ;
56+ } ) ;
57+ expect ( spy . callCount ) . toEqual ( 10 ) ;
58+ } ) ) ;
59+ } ) ;
0 commit comments