Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 5ddd01f

Browse files
test(typeahead): re-organize tests
1 parent 7e178a3 commit 5ddd01f

File tree

4 files changed

+370
-362
lines changed

4 files changed

+370
-362
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe('typeaheadHighlight', function () {
2+
3+
var highlightFilter;
4+
5+
beforeEach(module('ui.bootstrap.typeahead'));
6+
beforeEach(inject(function (typeaheadHighlightFilter) {
7+
highlightFilter = typeaheadHighlightFilter;
8+
}));
9+
10+
it('should higlight a match', function () {
11+
expect(highlightFilter('before match after', 'match')).toEqual('before <strong>match</strong> after');
12+
});
13+
14+
it('should higlight a match with mixed case', function () {
15+
expect(highlightFilter('before MaTch after', 'match')).toEqual('before <strong>MaTch</strong> after');
16+
});
17+
18+
it('should higlight all matches', function () {
19+
expect(highlightFilter('before MaTch after match', 'match')).toEqual('before <strong>MaTch</strong> after <strong>match</strong>');
20+
});
21+
22+
it('should do nothing if no match', function () {
23+
expect(highlightFilter('before match after', 'nomatch')).toEqual('before match after');
24+
});
25+
26+
it('issue 316 - should work correctly for regexp reserved words', function () {
27+
expect(highlightFilter('before (match after', '(match')).toEqual('before <strong>(match</strong> after');
28+
});
29+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
describe('syntax parser', function () {
2+
3+
var typeaheadParser, scope, filterFilter;
4+
5+
beforeEach(module('ui.bootstrap.typeahead'));
6+
beforeEach(inject(function (_$rootScope_, _filterFilter_, _typeaheadParser_) {
7+
typeaheadParser = _typeaheadParser_;
8+
scope = _$rootScope_;
9+
filterFilter = _filterFilter_;
10+
}));
11+
12+
it('should parse the simplest array-based syntax', function () {
13+
scope.states = ['Alabama', 'California', 'Delaware'];
14+
var result = typeaheadParser.parse('state for state in states | filter:$viewValue');
15+
16+
var itemName = result.itemName;
17+
var locals = {$viewValue:'al'};
18+
expect(result.source(scope, locals)).toEqual(['Alabama', 'California']);
19+
20+
locals[itemName] = 'Alabama';
21+
expect(result.viewMapper(scope, locals)).toEqual('Alabama');
22+
expect(result.modelMapper(scope, locals)).toEqual('Alabama');
23+
});
24+
25+
it('should parse the simplest function-based syntax', function () {
26+
scope.getStates = function ($viewValue) {
27+
return filterFilter(['Alabama', 'California', 'Delaware'], $viewValue);
28+
};
29+
var result = typeaheadParser.parse('state for state in getStates($viewValue)');
30+
31+
var itemName = result.itemName;
32+
var locals = {$viewValue:'al'};
33+
expect(result.source(scope, locals)).toEqual(['Alabama', 'California']);
34+
35+
locals[itemName] = 'Alabama';
36+
expect(result.viewMapper(scope, locals)).toEqual('Alabama');
37+
expect(result.modelMapper(scope, locals)).toEqual('Alabama');
38+
});
39+
40+
it('should allow to specify custom model mapping that is used as a label as well', function () {
41+
42+
scope.states = [
43+
{code:'AL', name:'Alabama'},
44+
{code:'CA', name:'California'},
45+
{code:'DE', name:'Delaware'}
46+
];
47+
var result = typeaheadParser.parse("state.name for state in states | filter:$viewValue | orderBy:'name':true");
48+
49+
var itemName = result.itemName;
50+
expect(itemName).toEqual('state');
51+
expect(result.source(scope, {$viewValue:'al'})).toEqual([
52+
{code:'CA', name:'California'},
53+
{code:'AL', name:'Alabama'}
54+
]);
55+
56+
var locals = {$viewValue:'al'};
57+
locals[itemName] = {code:'AL', name:'Alabama'};
58+
expect(result.viewMapper(scope, locals)).toEqual('Alabama');
59+
expect(result.modelMapper(scope, locals)).toEqual('Alabama');
60+
});
61+
62+
it('should allow to specify custom view and model mappers', function () {
63+
64+
scope.states = [
65+
{code:'AL', name:'Alabama'},
66+
{code:'CA', name:'California'},
67+
{code:'DE', name:'Delaware'}
68+
];
69+
var result = typeaheadParser.parse("state.code as state.name + ' ('+state.code+')' for state in states | filter:$viewValue | orderBy:'name':true");
70+
71+
var itemName = result.itemName;
72+
expect(result.source(scope, {$viewValue:'al'})).toEqual([
73+
{code:'CA', name:'California'},
74+
{code:'AL', name:'Alabama'}
75+
]);
76+
77+
var locals = {$viewValue:'al'};
78+
locals[itemName] = {code:'AL', name:'Alabama'};
79+
expect(result.viewMapper(scope, locals)).toEqual('Alabama (AL)');
80+
expect(result.modelMapper(scope, locals)).toEqual('AL');
81+
});
82+
});
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
describe('typeaheadPopup - result rendering', function () {
2+
3+
var scope, $rootScope, $compile;
4+
5+
beforeEach(module('ui.bootstrap.typeahead'));
6+
beforeEach(module('template/typeahead/typeahead.html'));
7+
beforeEach(inject(function (_$rootScope_, _$compile_) {
8+
$rootScope = _$rootScope_;
9+
scope = $rootScope.$new();
10+
$compile = _$compile_;
11+
}));
12+
13+
it('should render initial results', function () {
14+
15+
scope.matches = ['foo', 'bar', 'baz'];
16+
scope.active = 1;
17+
18+
var el = $compile("<div><typeahead-popup matches='matches' active='active' select='select(activeIdx)'></typeahead-popup></div>")(scope);
19+
$rootScope.$digest();
20+
21+
var liElems = el.find('li');
22+
expect(liElems.length).toEqual(3);
23+
expect(liElems.eq(0)).not.toHaveClass('active');
24+
expect(liElems.eq(1)).toHaveClass('active');
25+
expect(liElems.eq(2)).not.toHaveClass('active');
26+
});
27+
28+
it('should change active item on mouseenter', function () {
29+
30+
scope.matches = ['foo', 'bar', 'baz'];
31+
scope.active = 1;
32+
33+
var el = $compile("<div><typeahead-popup matches='matches' active='active' select='select(activeIdx)'></typeahead-popup></div>")(scope);
34+
$rootScope.$digest();
35+
36+
var liElems = el.find('li');
37+
expect(liElems.eq(1)).toHaveClass('active');
38+
expect(liElems.eq(2)).not.toHaveClass('active');
39+
40+
liElems.eq(2).trigger('mouseenter');
41+
42+
expect(liElems.eq(1)).not.toHaveClass('active');
43+
expect(liElems.eq(2)).toHaveClass('active');
44+
});
45+
46+
it('should select an item on mouse click', function () {
47+
48+
scope.matches = ['foo', 'bar', 'baz'];
49+
scope.active = 1;
50+
$rootScope.select = angular.noop;
51+
spyOn($rootScope, 'select');
52+
53+
var el = $compile("<div><typeahead-popup matches='matches' active='active' select='select(activeIdx)'></typeahead-popup></div>")(scope);
54+
$rootScope.$digest();
55+
56+
var liElems = el.find('li');
57+
liElems.eq(2).find('a').trigger('click');
58+
expect($rootScope.select).toHaveBeenCalledWith(2);
59+
});
60+
});

0 commit comments

Comments
 (0)