-
Notifications
You must be signed in to change notification settings - Fork 482
/
clientApiTest.js
97 lines (85 loc) · 3.16 KB
/
clientApiTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sinon from 'sinon';
import {expect, assert} from '../util/configuredChai';
const project = require('@cdo/apps/code-studio/initApp/project');
var clientApi = require('@cdo/apps/clientApi');
describe('clientApi module', () => {
var xhr, requests;
beforeEach(() => {
sinon.stub(project, 'getCurrentId').returns('some-project');
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = function(xhr) {
requests.push(xhr);
};
});
afterEach(() => {
project.getCurrentId.restore();
xhr.restore();
});
describe('the withProjectId function', () => {
it('returns a copy of the clientApi with the projectId set', () => {
var newApi = clientApi.assets.withProjectId('some-other-project');
expect(clientApi.assets.projectId).to.be.undefined;
expect(newApi.projectId).to.equal('some-other-project');
expect(newApi).to.be.an.instanceof(clientApi.assets.constructor);
});
});
describe('the ajax function', done => {
it('will call the error handler with the xhr object when response status is >= 400', done => {
clientApi.assets.ajax(
'GET',
'does-not-exist',
() => assert.fail('success called', 'error called'),
xhr => {
expect(xhr.status).to.equal(404);
done();
}
);
expect(requests).to.have.length(1);
requests[0].respond(404, {}, 'Not Found');
});
it('will fail silently if no error handler is passed in', () => {
clientApi.assets.ajax('GET', 'does-not-exist', () =>
assert.fail('success called', 'error called')
);
requests[0].respond(404, {}, 'Not Found');
});
it('will call the success handler with the xhr object when response status is < 400', done => {
clientApi.assets.ajax(
'GET',
'exists.png',
xhr => {
expect(xhr.status).to.equal(200);
done();
},
() => assert.fail('error called', 'success called')
);
expect(requests).to.have.length(1);
expect(requests[0].method).to.equal('GET');
expect(requests[0].url).to.equal('/v3/assets/some-project/exists.png');
requests[0].respond(200, {}, 'this is an asset!');
});
it('will use the specified projectId for the url when set', () => {
clientApi.assets.withProjectId('some-other-project').ajax();
expect(requests[0].url).to.equal('/v3/assets/some-other-project');
});
});
describe('assets api', () => {
it('should have a copyAssets method', () => {
clientApi.assets.copyAssets('some-source-project', ['some-file.png']);
expect(requests).to.have.length(1);
expect(requests[0].method).to.equal('POST');
expect(requests[0].url).to.equal(
'/v3/copy-assets/some-project?src_channel=some-source-project&src_files=%5B%22some-file.png%22%5D'
);
});
it('puts an audio asset to the correct url', () => {
clientApi.assets.putAsset('some-source-project', ['some-audio.mp3']);
expect(requests).to.have.length(1);
expect(requests[0].method).to.equal('PUT');
expect(requests[0].url).to.equal(
'/v3/assets/some-project/some-source-project'
);
});
});
});