diff options
| author | Matias Niemelä | 2013-06-18 09:37:29 -0400 | 
|---|---|---|
| committer | Matias Niemelä | 2013-06-18 09:37:29 -0400 | 
| commit | f6be59c1b9253ad8438ec65ab959e294686c65ab (patch) | |
| tree | e6f012c6c0114589a7ef6cdcedf5baa1cc9ab680 /docs/component-spec | |
| parent | 46dfb92afd185c93f60ca90a72653f33d7cb18e8 (diff) | |
| download | angular.js-f6be59c1b9253ad8438ec65ab959e294686c65ab.tar.bz2 | |
chore(ngdocs): provide test code for lunr search in docs
Diffstat (limited to 'docs/component-spec')
| -rw-r--r-- | docs/component-spec/NavigationCtrlSpec.js | 72 | ||||
| -rw-r--r-- | docs/component-spec/docsSearchSpec.js | 59 | 
2 files changed, 131 insertions, 0 deletions
| diff --git a/docs/component-spec/NavigationCtrlSpec.js b/docs/component-spec/NavigationCtrlSpec.js new file mode 100644 index 00000000..d7a9da45 --- /dev/null +++ b/docs/component-spec/NavigationCtrlSpec.js @@ -0,0 +1,72 @@ +describe("DocsNavigationCtrl", function() { + +  beforeEach(module('docsApp')); + +  var ctrl, $scope; + +  beforeEach(function() { +    module(function($provide) { +      $provide.value('docsPages', []); +      $provide.factory('docsSearch', function() { +        return function(q) { +          return ['one','two','three']; +        }; +      }); +    }); +    inject(function($controller, $rootScope, $location, docsSearch) { +      $scope = $rootScope.$new(); +      ctrl = $controller('DocsNavigationCtrl', { +        $scope : $scope, +        $location : $location, +        docsSearch : docsSearch +      }); +    }); +  }); + +  it("should search and return data from docsSearch", function() { +    $scope.search('1234') +    expect($scope.results.join(',')).toBe('one,two,three'); +    expect($scope.hasResults).toBe(true); +  }); + +  it("should avoid searching if the search term is too short", function() { +    $scope.search('1') +    expect($scope.results.length).toBe(0); +    expect($scope.hasResults).toBe(false); +  }); + +  it("should set the columns classname based on the total grouped results", function() { +    $scope.search('1234'); +    expect($scope.colClassName).toBe('cols-3'); + +    $scope.search('1'); +    expect($scope.colClassName).toBe(null); +  }); + +  it("should hide and clear the results when called", function() { +    $scope.hasResults = true; +    $scope.results = ['one']; +    $scope.colClassName = '...'; +    $scope.hideResults(); +    expect($scope.hasResults).toBe(false); +    expect($scope.results.length).toBe(0); +    expect($scope.colClassName).toBe(null); +  }); + +  it("should hide, clear and change the path of the page when submitted", inject(function($location) { +    $scope.hasResults = true; +    $scope.results = { +      api : [ +        {url : '/home'} +      ], +      tutorial : [ +        {url : '/tutorial'} +      ] +    }; +    $scope.submit(); +    expect($location.path()).toBe('/home'); +    expect($scope.results.length).toBe(0); +    expect($scope.hasResults).toBe(false); +  })); + +}); diff --git a/docs/component-spec/docsSearchSpec.js b/docs/component-spec/docsSearchSpec.js new file mode 100644 index 00000000..38e6937a --- /dev/null +++ b/docs/component-spec/docsSearchSpec.js @@ -0,0 +1,59 @@ +describe("docsSearch", function() { + +  beforeEach(module('docsApp')); + +  var interceptedLunrResults; +  beforeEach(function() { +    interceptedLunrResults = []; +  }); + +  beforeEach(function() { +    module(function($provide) { +      var results = []; +      results[0] = { section: 'tutorial', shortName: 'item one', keywords: 'item, one, 1' }; +      results[1] = { section: 'tutorial', shortName: 'item man', keywords: 'item, man' }; +      results[2] = { section: 'api', shortName: 'item other', keywords: 'item, other' }; +      results[3] = { section: 'cookbook', shortName: 'item cookbook', keywords: 'item, other' }; +      results[4] = { section: 'api', shortName: 'ngRepeat', keywords: 'item, other' }; + +      $provide.value('NG_PAGES', results); +      $provide.factory('lunrSearch', function() { +        return function() { +          return { +            store : function(value) { +              interceptedLunrResults.push(value); +            }, +            search : function(q) { +              var data = []; +              angular.forEach(results, function(res, i) { +                data.push({ ref : i }); +              }); +              return data; +            } +          }  +        }; +      }); +    }); +  }); + +  it("should lookup and organize values properly", inject(function(docsSearch) { +    var items = docsSearch('item'); +    expect(items['api'].length).toBe(2); +  })); + +  it("should place cookbook items in the tutorial", inject(function(docsSearch) { +    var items = docsSearch('item'); +    expect(items['tutorial'].length).toBe(3); +  })); + +  it("should return all results without a search", inject(function(docsSearch) { +    var items = docsSearch(); +    expect(items['tutorial'].length).toBe(3); +    expect(items['api'].length).toBe(2); +  })); + +  it("should store values with and without a ng prefix", inject(function(docsSearch) { +    expect(interceptedLunrResults[4].title).toBe('ngRepeat repeat'); +  })); + +}); | 
