forked from formio/formio.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIMock.js
94 lines (94 loc) · 3.09 KB
/
APIMock.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
import _ from 'lodash';
import Formio from '../src/Formio';
import fetchMock from 'fetch-mock/es5/client';
import Chance from 'chance';
import esc from 'escape-string-regexp';
const chance = Chance();
export const APIMock = {
submission: function(url, form) {
const submissions = {};
const domain = url.match(/http[s]?:\/\/[^/]+/)[0];
const requests = {
formLoad: new RegExp(esc(`${url}?live=1`)),
formSubmissionIndex: new RegExp(`${domain}\\/form\\/(.*)\\/submission\\?(.*)`),
formSubmission: new RegExp(`${domain}\\/form\\/(.*)\\/submission\\/(.*)`),
submissionCreate: new RegExp(esc(`${url}/submission`)),
submissionGet: new RegExp(`${esc(`${url}/submission/`)}(.*)`),
submissionUpdate: new RegExp(`${esc(`${url}/submission/`)}(.*)`),
submissionIndex: new RegExp(`${esc(`${url}/submission?`)}(.*)`)
};
fetchMock
.get(requests.formLoad, () => {
return form;
})
// Create a new submission.
.post(requests.submissionCreate, (url, req) => {
const owner = Formio.getUser();
req.body = JSON.parse(req.body);
if (!req.body._id) {
req.body._id = chance.string({ length: 21, pool: 'ABCDEFG1234567890' });
}
if (owner) {
req.body.owner = owner._id;
}
submissions[req.body._id] = req.body;
return req.body;
})
// Update a submission.
.put(requests.submissionUpdate, (url, req) => {
req.body = JSON.parse(req.body);
const matches = url.match(requests.submissionUpdate);
const id = matches[1];
if (!id || !submissions[id]) {
return 401;
}
submissions[id] = req.body;
return submissions[id];
})
.get(requests.formSubmission, () => {
const matches = url.match(requests.formSubmission);
if (!matches || !matches[3]) {
return 401;
}
const id = matches[3];
if (!id || !submissions[id]) {
return 401;
}
return submissions[id];
})
// Get a submission
.get(requests.submissionGet, (url) => {
const matches = url.match(requests.submissionUpdate);
const id = matches[1];
if (!id || !submissions[id]) {
return 401;
}
return submissions[id];
})
.get(requests.formSubmissionIndex, (url) => {
const matches = url.match(requests.formSubmissionIndex);
if (!matches[2]) {
return [];
}
return _.filter(submissions, _.chain(matches[2])
.split('&')
.map(_.partial(_.split, _, '=', 2))
.fromPairs()
.omit(['limit', 'skip'])
.value());
})
// Mock the submission index.
.get(requests.submissionIndex, (url) => {
const matches = url.match(requests.submissionIndex);
if (!matches[1]) {
return [];
}
return _.filter(submissions, _.chain(matches[1])
.split('&')
.map(_.partial(_.split, _, '=', 2))
.fromPairs()
.omit(['limit', 'skip'])
.value());
});
}
};