'
}
}, www);
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('header');
});
it('should render footer when suggestions are rendered', function() {
this.dataset = new Dataset({
source: this.source,
node: $('
'),
templates: {
footer: function(c) { return '
' + c.query + '
'; }
}
}, www);
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('woah');
});
it('should not render header/footer if there is no content', function() {
this.dataset = new Dataset({
source: this.source,
node: $('
'),
templates: {
header: '
header
',
footer: 'footer
'
}
}, www);
this.source.andCallFake(syncEmptySuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).not.toContainText('header');
expect(this.dataset.$el).not.toContainText('footer');
});
it('should not render stale suggestions', function() {
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('nelly');
waits(100);
runs(function() {
expect(this.dataset.$el).toContainText('one');
expect(this.dataset.$el).toContainText('two');
expect(this.dataset.$el).toContainText('html');
expect(this.dataset.$el).not.toContainText('four');
expect(this.dataset.$el).not.toContainText('five');
});
});
it('should not render async suggestions if update was canceled', function() {
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.dataset.cancel();
waits(100);
runs(function() {
var rendered = this.dataset.$el.find('.tt-suggestion');
expect(rendered).toHaveLength(3);
});
});
it('should trigger rendered after suggestions are rendered', function() {
var spy;
this.dataset.onSync('rendered', spy = jasmine.createSpy());
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
waitsFor(function() { return spy.callCount; });
});
});
describe('#clear', function() {
it('should clear suggestions', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
this.dataset.clear();
expect(this.dataset.$el).toBeEmpty();
});
it('should cancel pending updates', function() {
var spy;
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
spy = spyOn(this.dataset, 'cancel');
this.dataset.clear();
expect(spy).toHaveBeenCalled();
});
it('should trigger cleared', function() {
var spy;
this.dataset.onSync('cleared', spy = jasmine.createSpy());
this.dataset.clear();
expect(spy).toHaveBeenCalled();
});
});
describe('#isEmpty', function() {
it('should return true when empty', function() {
expect(this.dataset.isEmpty()).toBe(true);
});
it('should return false when not empty', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.isEmpty()).toBe(false);
});
});
describe('#destroy', function() {
it('should set dataset element to dummy element', function() {
var $prevEl = this.dataset.$el;
this.dataset.destroy();
expect(this.dataset.$el).not.toBe($prevEl);
});
});
// helper functions
// ----------------
function syncEmptySuggestions(q, sync, async) {
sync([]);
}
function syncMockSuggestions(q, sync, async) {
sync(mockSuggestions);
}
function syncMockSuggestionsDisplayFn(q, sync, async) {
sync(mockSuggestionsDisplayFn);
}
function asyncSync(q, sync, async) {
setTimeout(function() { sync(mockSuggestions); }, 0);
}
function multipleSync(q, sync, async) {
sync(mockSuggestions);
sync(mockSuggestions);
}
function fakeGetWithAsyncSuggestions(query, sync, async) {
sync(mockSuggestions);
setTimeout(function() {
async([
{ value: 'four', raw: { value: 'four' } },
{ value: 'five', raw: { value: 'five' } },
{ value: 'six', raw: { value: 'six' } },
{ value: 'seven', raw: { value: 'seven' } },
{ value: 'eight', raw: { value: 'eight' } },
]);
}, 0);
}
});