Skip to content

Instantly share code, notes, and snippets.

@Problematic
Created March 17, 2014 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Problematic/9606843 to your computer and use it in GitHub Desktop.
Save Problematic/9606843 to your computer and use it in GitHub Desktop.
describe('$resource', function () {
var $httpBackend,
$rootScope,
Tasks;
beforeEach(module('ngResource'));
beforeEach(inject(function (_$httpBackend_, _$rootScope_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
var tasks = [{
ID: 1,
name: 'foo task'
}, {
ID: 2,
name: 'bar task'
}, {
ID: 3,
name: 'baz task'
}];
$httpBackend.when('GET', '/tasks').respond(tasks);
for (var i = 0; i < tasks.length; i++) {
$httpBackend.when('GET', '/tasks/' + tasks[i].ID).respond(tasks[i]);
}
}));
beforeEach(inject(function($resource) {
Tasks = $resource('/tasks/:id', {
id: '@ID'
});
}));
it('data layer', inject(function ($rootScope) {
$rootScope.$watch('tasks.watchValue', function (value) {
console.log(value);
// render chart
});
$rootScope.tasks = Tasks.query();
$rootScope.tasks.watchValue = 0;
$httpBackend.flush();
expect($rootScope.tasks.length).toBe(3);
$httpBackend.expectPOST('/tasks/1').respond(400);
var task = $rootScope.tasks[0];
task.name = 'grog';
task.$save().then(null, function () {
task.$get().then(function () {
$rootScope.tasks.watchValue++;
});
});
$httpBackend.flush();
expect($rootScope.tasks[0].name).toBe('foo task');
$httpBackend.expectPOST('/tasks/2').respond({
ID: 2,
name: 'grug'
});
var task2 = $rootScope.tasks[1];
task2.name = 'grug';
task2.$save().then(function () {
$rootScope.tasks.watchValue++;
});
$httpBackend.flush();
expect($rootScope.tasks[1].name).toBe('grug');
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment