Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
santthosh committed Jun 18, 2014
1 parent 8d66cf9 commit 609b4d7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory" : "bower_components"
}
110 changes: 64 additions & 46 deletions dist/js/mentio.spec.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,87 @@
'use strict';

describe('mentio-menu directive', function () {
var $compile, $rootScope;
describe('mentio-menu', function () {
var $compile, $rootScope, $templateCache, mentionRuleScope, menuScope, searchSpy, mockItems;

beforeEach(module('mentio'));

beforeEach(inject(function (_$compile_, _$rootScope_) {
beforeEach(inject(function (_$compile_, _$rootScope_, _$templateCache_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));

it('should search on items', function () {
var element, $renderScope, $scope, searchSpy, mockItems;

mockItems = [
{
id: 1
},
{
id: 2
$templateCache = _$templateCache_;

$templateCache.put('/people-mentions.tpl',
'<div>' +
'<li mentio-menu-item="person" ng-repeat="person in items">' +
' <img ng-src="{{person._source.imageUrl}}"><p class="name">{{person._source.name}}</p>' +
' <p>{{person._source.bio.substring(0,30)}}</p>' +
'</li>' +
'</ul>' +
'</div>');

var $scope = $rootScope.$new();

var mentionableTextArea = angular.element('<div><textarea ng-model="textArea" ng-trim="false"></textarea>' +
'<span>Mentioned: {{atVar}}</span></div>');

$compile(mentionableTextArea)($scope);
var mentionableTextAreaScope = mentionableTextArea.scope();
mentionableTextAreaScope.$apply();

var mentionMenu = angular.element('<mentio-menu bind="textArea" ng-model="atVar2" ng-cloak>' +
'<mentio-rule trigger-char="@" items="people" template="/people-mentions.tpl" ' +
'search="searchPeople(term)" select="getPeopleTextRaw(item)"></mentio-rule>' +
'</mentio-menu>');

$compile(mentionMenu)($scope);
var mentionMenuScope = mentionMenu.scope();
mentionMenuScope.$apply();

// This is ugly, uses undocumented method to access the child scopes
for(var cs = $scope.$$childHead; cs; cs = cs.$$nextSibling) {
if(cs.hide) {
mentionRuleScope = cs;
}
];
if(cs.query) {
menuScope = cs;
}
}

$renderScope = $rootScope.$new();
mockItems = [ { id: 1 }, { id: 2 } ];

$renderScope.search = function (term) {
if (term === 'foo') {
$renderScope.items = mockItems;
mentionRuleScope.search = function (object) {
if (object.term === 'foo') {
mentionRuleScope.items = mockItems;
} else {
$renderScope.items = [];
mentionRuleScope.items = [];
}
};

searchSpy = spyOn($renderScope, 'search');
searchSpy = spyOn(mentionRuleScope, 'search');
searchSpy.andCallThrough();
}));

element = $compile('<div mentio-menu bind="content" items="items"' +
'search="search(term)" select="select(item)" ng-model="menuContent">')($renderScope);

$renderScope.$digest();

$scope = element.isolateScope();

// TODO: move getAtMentionInfo() into a service so it can be mocked
$scope.atVar = 'foo';
expect($scope.hide).toBeTruthy();
$scope.query();

$renderScope.$digest();
it('should show mentio for valid search term', function () {
expect(mentionRuleScope.hide).toBeTruthy();

expect(searchSpy).toHaveBeenCalledWith('foo');
expect($scope.items).toEqual(mockItems);
expect($scope.hide).toBeFalsy();
menuScope.atVar = 'foo';
menuScope.query('@');
menuScope.$apply();

expect(searchSpy).toHaveBeenCalledWith({ term : 'foo' });
expect(mentionRuleScope.items).toEqual(mockItems);
expect(mentionRuleScope.hide).toBeFalsy();
});

/*
* The menu should not display when the search returns no items
*/
$scope.atVar = 'bar';
$scope.query();
it('should hide mentio for invalid search term', function () {
expect(mentionRuleScope.hide).toBeTruthy();

$renderScope.$digest();
menuScope.atVar = 'fox';
menuScope.query('@');
menuScope.$apply();

expect(searchSpy.callCount).toBe(2);
expect($scope.items).toEqual([]);
expect($scope.hide).toBeTruthy();
expect(searchSpy).toHaveBeenCalledWith({ term : 'fox' });
expect(mentionRuleScope.items).toEqual([]);
expect(mentionRuleScope.hide).toBeTruthy();
});
});

0 comments on commit 609b4d7

Please sign in to comment.