forked from box/box-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnnotationAPI-test.js
99 lines (85 loc) · 2.99 KB
/
AnnotationAPI-test.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
98
99
/* eslint-disable no-unused-expressions */
import AnnotationAPI from '../AnnotationAPI';
import { ERROR_TYPE } from '../../constants';
const API_HOST = 'https://app.box.com/api';
const HTTP_POST = 'POST';
const HTTP_DELETE = 'DELETE';
let api;
const promise = new Promise(jest.fn());
describe('api/AnnotationAPI', () => {
const annotationData = {
id: '',
details: {
location: { page: 1 }
}
};
beforeEach(() => {
api = new AnnotationAPI({
apiHost: API_HOST,
fileId: 1,
token: 'someToken'
});
api.axios = jest.fn().mockReturnValue(promise);
api.errorHandler = jest.fn();
api.axiosSource = { token: '123abc' };
api.headers = {};
});
describe('getBaseUrl()', () => {
it('should return the create annotations URL', () => {
expect(api.getBaseUrl()).toEqual('https://app.box.com/api/2.0/annotations');
});
it('should return the delete annotations URL', () => {
expect(api.getBaseUrl(1)).toEqual('https://app.box.com/api/2.0/annotations/1');
});
});
describe('create()', () => {
it('post and create a new annotation', () => {
api.create({}).then(() => {
expect(api.axios).toBeCalledWith({
url: api.getBaseUrl(),
data: {},
method: HTTP_POST,
cancelToken: '123abc',
headers: {}
});
});
});
});
describe('delete()', () => {
it('delete the specified annotation', () => {
api.delete('123').then(() => {
expect(api.axios).toBeCalledWith({
url: api.getBaseUrl('123'),
method: HTTP_DELETE,
cancelToken: '123abc',
headers: {}
});
});
});
});
describe('createSuccessHandler()', () => {
it('should emit an error event', () => {
const error = new Error('Could not create annotation');
error.name = ERROR_TYPE.create;
api.createSuccessHandler({ type: 'error' });
expect(api.errorHandler).toBeCalledWith(error);
});
it('should return the created annotation', () => {
expect(api.createSuccessHandler(annotationData).permissions).toEqual({
can_delete: true,
can_edit: true
});
});
});
describe('deleteSuccessHandler()', () => {
it('should emit an error event', () => {
const error = new Error('Could not delete annotation with ID 123');
error.name === ERROR_TYPE.delete;
api.deleteSuccessHandler({ type: 'error' }, '123');
expect(api.errorHandler).toBeCalledWith(error);
});
it('should return the deleted annotation', () => {
expect(api.deleteSuccessHandler({})).toEqual({});
});
});
});