1 | describe('fs-xhr',function() {
|
2 |
|
3 | function createJQPromise(success) {
|
4 | return {
|
5 | done: function() {
|
6 | var p = Q.when(success);
|
7 | return p.then.apply(p,arguments);
|
8 | }
|
9 | };
|
10 | }
|
11 |
|
12 | var fakeJQuery = {
|
13 | get: jasmine.createSpy('jqget').and.returnValue(createJQPromise('get')),
|
14 | post: jasmine.createSpy('jqpost').and.returnValue(createJQPromise('post')),
|
15 | ajax: jasmine.createSpy('jqajax').and.returnValue(createJQPromise('ajax'))
|
16 | };
|
17 |
|
18 | var fs = factory('services/fs-xhr',{
|
19 | 'q':Q,
|
20 | 'jquery': fakeJQuery
|
21 | });
|
22 |
|
23 | describe('fs signature',function() {
|
24 | it('should have a read method',function() {
|
25 | expect(fs.read).not.toBeUndefined();
|
26 | });
|
27 | it('should have a write method',function() {
|
28 | expect(fs.write).not.toBeUndefined();
|
29 | });
|
30 | it('should have a remove method',function() {
|
31 | expect(fs.remove).not.toBeUndefined();
|
32 | });
|
33 | });
|
34 |
|
35 | describe('reading',function() {
|
36 | it('should read a test file',function() {
|
37 | return fs.read('foo.txt').then(function(data) {
|
38 | expect(data).toBe('get');
|
39 | expect(fakeJQuery.get).toHaveBeenCalledWith('fs/foo.txt');
|
40 | });
|
41 | });
|
42 | it('should fail on xhr exception',function() {
|
43 | fakeJQuery.get.and.returnValue(createJQPromise(Q.reject(new Error('foo'))));
|
44 | return fs.read('foo.txt').catch(function(err) {
|
45 | expect(fakeJQuery.get).toHaveBeenCalledWith('fs/foo.txt');
|
46 | expect(err.message).toBe('foo');
|
47 | });
|
48 | });
|
49 | });
|
50 |
|
51 | describe('writing',function() {
|
52 | it('should write a test file',function() {
|
53 | return fs.write('foo.txt','bar').then(function(data) {
|
54 | expect(fakeJQuery.post).toHaveBeenCalledWith('fs/foo.txt','bar');
|
55 | expect(data).toBe('post');
|
56 | });
|
57 | });
|
58 | it('should fail on xhr exception',function() {
|
59 | fakeJQuery.post.and.returnValue(createJQPromise(Q.reject(new Error('foo'))));
|
60 | return fs.write('foo.txt','bar').catch(function(err) {
|
61 | expect(fakeJQuery.post).toHaveBeenCalledWith('fs/foo.txt','bar');
|
62 | expect(err.message).toBe('foo');
|
63 | });
|
64 | });
|
65 | });
|
66 |
|
67 | describe('removing',function() {
|
68 | it('should remove a test file',function() {
|
69 | return fs.remove('foo.txt').then(function(data) {
|
70 | expect(fakeJQuery.ajax).toHaveBeenCalledWith('fs/foo.txt',{
|
71 | type: 'DELETE'
|
72 | });
|
73 | expect(data).toBe('ajax');
|
74 | });
|
75 | });
|
76 | it('should fail on xhr exception',function() {
|
77 | fakeJQuery.ajax.and.returnValue(createJQPromise(Q.reject(new Error('foo'))));
|
78 | return fs.remove('foo.txt','bar').catch(function(err) {
|
79 | expect(fakeJQuery.ajax).toHaveBeenCalledWith('fs/foo.txt',{
|
80 | type: 'DELETE'
|
81 | });
|
82 | expect(err.message).toBe('foo');
|
83 | });
|
84 | });
|
85 | });
|
86 |
|
87 |
|
88 | });
|