Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created April 22, 2014 18:04
Show Gist options
  • Save clouddueling/11188718 to your computer and use it in GitHub Desktop.
Save clouddueling/11188718 to your computer and use it in GitHub Desktop.
Testing a service, controller, and filter using stub for service
describe('Profile | Clipboard | Finder |', function() {
beforeEach(module('profile.clipboard.finder'));
beforeEach(module('ui.router'));
describe('ClipboardFinderCtrl |', function() {
var ctrl, scope, httpBackend, finder;
beforeEach(inject(function($rootScope, $controller, Finder, $httpBackend) {
scope = $rootScope.$new();
scope.main = {};
finder = Finder;
httpBackend = $httpBackend;
ctrl = $controller('ClipboardFinderCtrl', {
$scope: scope,
Finder: finder
});
}));
it('should load initial objects and tags', function() {
httpBackend.expectGET('/api/internal/finder/objects?page=1&order_by=updated_at&direction=DESC&query=&').respond({
objects: [
{ id: 1, type: 'article' }
]
});
httpBackend.expectGET('/api/internal/tags?').respond({
tags: [
{ name: 'test', id: 1 }
]
});
httpBackend.flush();
});
});
describe('FinderFactory |', function() {
var finder, httpBackend, timeout, getValuesStub, state;
beforeEach(inject(function(Finder, $httpBackend, ObjectService, $timeout, $state) {
finder = new Finder();
httpBackend = $httpBackend;
timeout = $timeout;
state = $state;
getValuesStub = sinon.stub($state, 'go').returns(true);
}));
it('should should have filters and objects upon start', function() {
expect(finder.filters).to.be.an.object;
expect(finder.busy).to.be.false;
expect(finder.showTypes).to.be.false;
expect(finder.showTags).to.be.false;
expect(finder.showSeries).to.be.false;
expect(finder.page).to.equal(1);
expect(finder.viewType).to.equal('icons');
expect(finder.objects).to.be.an.array;
});
it('should set the view type', function() {
expect(finder.viewType).to.equal('icons');
finder.setViewType('list');
expect(finder.viewType).to.equal('list');
});
it('should return if is active view type or not', function() {
expect(finder.isActiveViewType('list')).to.be.false;
finder.isActiveViewType('icons');
expect(finder.isActiveViewType('icons')).to.be.true;
});
it('should reeturn all objects types', function() {
expect(finder.getTypes()).to.have.length(28);
});
it('should return the type name of an object based on its type', function() {
var object = {
type: 'article'
};
expect(finder.getObjectType(object)).to.equal('Text Document');
});
it('should return the url for a card type object', function() {
var object = {
type: 'systems',
id: 1
};
expect(finder.getObjectUrl(object)).to.equal('/profile/clipboard/card/' + object.id + '/preview');
});
it('should return a object is a card or not', function() {
var object = {
id: 2,
type: 'systems'
};
expect(finder.isObjectCard(object)).to.be.true;
object.type = 'article';
expect(finder.isObjectCard(object)).to.be.false;
});
it('should return the url for a upload type object', function() {
var object = {
type: 'article',
id: 2
};
expect(finder.getObjectUrl(object)).to.equal('/profile/clipboard/upload/' + object.id + '/preview');
});
it('should toggle a property in the filters array', function() {
finder.toggleFilter('tags', 'tag1');
expect(finder.isFilterActive('tags', 'tag1')).to.be.true;
expect(finder.filters.tags[0]).to.equal('tag1');
finder.toggleFilter('tags', 'tag1');
expect(finder.filters.tags[0]).to.be.undefined;
});
it('should clear general filters', function() {
finder.filters.query = 'test';
finder.filters.types = ['article'];
finder.filters.tags = ['tag'];
finder.filters.serieIds = [1, 2];
finder.page = 3;
finder.objects = [{}, {}];
finder.clearFilters();
expect(finder.filters.query).to.be.empty;
expect(finder.filters.types).to.have.length(0);
expect(finder.filters.tags).to.have.length(0);
expect(finder.filters.serieIds).to.have.length(0);
expect(finder.page).to.equal(1);
expect(finder.objects).to.have.length(0);
});
it('should return true if selectable filters are set', function() {
finder.filters.query = 'test';
expect(finder.hasFilters()).to.be.true;
finder.clearFilters();
expect(finder.hasFilters()).to.be.false;
});
it('should set the filters order by', function() {
finder.setFiltersOrderBy('name');
expect(finder.filters.orderBy).to.equal('name');
expect(finder.filters.direction).to.equal('DESC');
finder.setFiltersOrderBy('name');
expect(finder.filters.orderBy).to.equal('name');
expect(finder.filters.direction).to.equal('ASC');
finder.setFiltersOrderBy('name');
expect(finder.filters.orderBy).to.equal('name');
expect(finder.filters.direction).to.equal('DESC');
finder.setFiltersOrderBy();
expect(finder.filters.orderBy).to.equal('updated_at');
expect(finder.filters.direction).to.equal('DESC');
});
it('should return if this is the current orderby and direction', function() {
expect(finder.isFiltersOrderBy('created_at', 'DESC')).to.be.false;
expect(finder.isFiltersOrderBy('updated_at', 'DESC')).to.be.true;
});
it('should increment the page counter and load objects', function() {
httpBackend.expectGET('/api/internal/finder/objects?page=2&order_by=updated_at&direction=DESC&query=&').respond({
objects: [
{ id: 1, type: 'article' }
]
});
// Activate the busy bool
finder.nextPage();
finder.nextPage();
httpBackend.flush();
});
it('should do a delayed request for objects with a search query', function() {
httpBackend.expectGET('/api/internal/finder/objects?page=1&order_by=updated_at&direction=DESC&query=test&').respond({ objects: [ { id: 1 }] });
finder.filters.query = 'test'
finder.searchObjects();
timeout.flush();
httpBackend.flush();
expect(finder.objects).to.have.length(1);
});
it('should load objects', function() {
httpBackend.expectGET('/api/internal/finder/objects?page=1&order_by=updated_at&direction=DESC&query=&').respond({
objects: [{}]
});
finder.loadObjects();
httpBackend.flush();
expect(finder.objects).to.have.length(1);
});
it('should not append objects when loading if no objects are returned', function() {
httpBackend.expectGET('/api/internal/finder/objects?page=1&order_by=updated_at&direction=DESC&query=&').respond({});
finder.loadObjects();
httpBackend.flush();
});
it('should load tags', function() {
httpBackend.expectGET('/api/internal/tags?').respond({
tags: [{ name: 'tag', id: 1 }]
});
finder.loadTags();
httpBackend.flush();
expect(finder.tags).to.have.length(1);
});
it('should create a POST card request', function() {
httpBackend.expectPOST('/api/internal/card').respond({ card: { id: 1 } });
finder.createCard('systems');
httpBackend.flush();
});
});
describe('Truncate Middle Filter |', function() {
var truncateMiddleFilter;
beforeEach(inject(function(_truncateMiddleFilter_) {
truncateMiddleFilter = _truncateMiddleFilter_;
}));
it('should create an ellipsis in the middle of long text', function() {
var text = "I am a really long piece of text that should not be allowed to live!";
expect(truncateMiddleFilter(text)).to.equal("I am a really long p...d to live!");
});
it('should return the string if its short', function() {
var text = 'test';
expect(truncateMiddleFilter(text)).to.equal(text);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment