Skip to content

Instantly share code, notes, and snippets.

@witoldsz
Created March 10, 2012 23:25
Show Gist options
  • Save witoldsz/2013912 to your computer and use it in GitHub Desktop.
Save witoldsz/2013912 to your computer and use it in GitHub Desktop.
Unit test for table model.
describe('table model', function() {
var hud, tableModel;
beforeEach(function(){
hud = jasmine.createSpyObj('hud', ['info']);
module('your-module-name', function($provide) {
$provide.value('hud', hud);
});
inject(function(tableModelFactory) {
tableModel = tableModelFactory('some/url');
});
});
function expectThat(expectations) {
expect(tableModel.data.length).toEqual(expectations.dataCount);
expect(tableModel.isEmpty()).toEqual(expectations.empty);
expect(tableModel.hasMore).toEqual(expectations.hasMore);
expect(tableModel.noMoreAvailable).toEqual(expectations.noMore);
}
it('should initialize correctly', function() {
expect(tableModel.data).toEqual([]);
expectThat({dataCount:0, empty:true, hasMore: false, noMore: false});
});
it('should get page by page where name="smith" and then sort by "age"', inject(function($httpBackend) {
$httpBackend.expectGET('some/url?name=smith').respond(200, [{id:1},{id:2}]);
tableModel.search({name: 'smith'});
$httpBackend.flush();
expectThat({dataCount:2, empty:false, hasMore: true, noMore: false});
$httpBackend.expectGET('some/url?name=smith&page=1').respond(200, [{id:3},{id:4}]);
tableModel.getMore();
$httpBackend.flush();
expectThat({dataCount:4, empty:false, hasMore: true, noMore: false});
$httpBackend.expectGET('some/url?name=smith&page=2').respond(200, []);
tableModel.getMore();
$httpBackend.flush();
expectThat({dataCount:4, empty:false, hasMore: false, noMore: true});
//Now let's test 'orderBy', do not care what exactly comes from the server.
$httpBackend.expectGET('some/url?name=smith&orderBy=age:asc').respond(200, [{id:1},{id:2},{id:3}]);
tableModel.orderBy('age:asc');
$httpBackend.flush();
expectThat({dataCount:3, empty:false, hasMore: true, noMore: false});
$httpBackend.expectGET('some/url?name=smith&orderBy=age:asc&page=1').respond(200, []);
tableModel.getMore();
$httpBackend.flush();
expectThat({dataCount:3, empty:false, hasMore: false, noMore: true});
}));
it('should display message when no data matching criteria', inject(function($httpBackend) {
$httpBackend.expectGET('some/url').respond(200, []);
tableModel.search({});
$httpBackend.flush();
expectThat({dataCount:0, empty:true, hasMore: false, noMore: true});
expect(hud.info).toHaveBeenCalled();
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment