forked from box/box-annotations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileVersionAPI-test.js
109 lines (96 loc) · 3.85 KB
/
FileVersionAPI-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
100
101
102
103
104
105
106
107
108
109
/* eslint-disable no-unused-expressions */
import FileVersionAPI from '../FileVersionAPI';
import { ERROR_TYPE } from '../../constants';
const API_HOST = 'https://app.box.com/api';
let api;
const promise = new Promise(jest.fn());
describe('api/FileVersionAPI', () => {
beforeEach(() => {
api = new FileVersionAPI({
apiHost: API_HOST,
fileId: 1,
token: 'someToken',
permissions: {}
});
api.axios = {
get: jest.fn().mockReturnValue(promise)
};
api.axiosSource = { token: '123abc' };
api.headers = {};
});
describe('getBaseUrl()', () => {
it('should return the read annotations URL', () => {
expect(api.getBaseUrl()).toEqual('https://app.box.com/api/2.0/files/1/annotations');
});
});
describe('fetchVersionAnnotations()', () => {
it('should fetch and organize the annotaitons for the specified file version id', () => {
api.createAnnotationMap = jest.fn();
api.fetchFromMarker = jest.fn().mockReturnValue(promise);
api.fetchVersionAnnotations('123').then(() => {
expect(api.fileVersionId).toEqual('123');
expect(api.fetchFromMarker).toBeCalled();
expect(api.createAnnotationMap).toBeCalled();
});
});
});
describe('fetchFromMarker()', () => {
it('should fetch all annotations from the specified file version id', () => {
api.fetchFromMarker('params').then(() => {
expect(api.axios.get).toBeCalledWith(api.getBaseUrl(), {
cancelToken: '123abc',
headers: {},
params: 'params'
});
});
});
});
describe('successHandler()', () => {
beforeEach(() => {
api.fetchFromMarker = jest.fn().mockReturnValue(Promise.resolve());
api.emit = jest.fn();
});
it('should call fetch the remaining annotations if the version has more to fetch', () => {
api.data = { entries: [{}] };
api.successHandler({ entries: [{}, {}], next_marker: 'marker', limit: 1 }).then(() => {
expect(api.data.entries.length).toEqual(3);
expect(api.fetchFromMarker).toBeCalled();
});
});
it('should not call fetch if no more annotations need to be fetched', () => {
api.data = { entries: [{}] };
api.successHandler({ entries: [{}, {}], limit: 1 }).then(() => {
expect(api.data.entries.length).toEqual(3);
expect(api.fetchFromMarker).not.toBeCalled();
});
});
it('should emit an error if the success response is of type error', () => {
api.fileVersionId = 123;
api.successHandler({ type: 'error' }).catch(() => {
const error = new Error(`Could not read annotations from file version with ID ${api.fileVersionId}`);
error.name = ERROR_TYPE.read;
expect(api.data.entries.length).toEqual(1);
expect(api.fetchFromMarker).not.toBeCalled();
});
});
});
describe('createAnnotationMap()', () => {
it('should create a map of thread IDs to annotations', () => {
const annotationData = {
threadID: 1,
item: {},
permissions: {},
details: {
location: { page: 1 },
type: 'highlight',
threadID: '123'
}
};
api.data = {
entries: [{ id: 1, ...annotationData }, { id: 2, ...annotationData }]
};
const threadMap = api.createAnnotationMap();
expect(Object.keys(threadMap).length).toEqual(1);
});
});
});