|
| 1 | +// This file exports an enketo mock for testing. A test can communicate with the |
| 2 | +// mock by getting or setting properties of global.enketo. |
| 3 | + |
1 | 4 | const appRoot = require('app-root-path'); |
2 | 5 | const { call } = require('ramda'); |
3 | | -// eslint-disable-next-line import/no-dynamic-require |
4 | 6 | const Problem = require(appRoot + '/lib/util/problem'); |
| 7 | +const { without } = require(appRoot + '/lib/util/util'); |
| 8 | + |
| 9 | +const defaults = { |
| 10 | + // Properties that each test can set |
| 11 | + |
| 12 | + // If `state` is set to 'error', the mock will pretend that Enketo has |
| 13 | + // misbehaved and will return a rejected promise for the next call. |
| 14 | + state: undefined, |
| 15 | + // Controls the timing of the Enketo response. |
| 16 | + wait: call, |
| 17 | + // The enketoId for the create() or createOnceToken() method to return. By |
| 18 | + // default, it is ::abcdefgh for create() and ::::abcdefgh for |
| 19 | + // createOnceToken(). |
| 20 | + token: undefined, |
| 21 | + |
| 22 | + // Properties that may be updated after the mock is called |
| 23 | + |
| 24 | + // The total number of times that the mock has been called during the test |
| 25 | + callCount: 0, |
| 26 | + // The OpenRosa URL that was passed to the create() or createOnceToken() |
| 27 | + // method |
| 28 | + receivedUrl: undefined, |
| 29 | + // An object with a property for each argument passed to the edit() method |
| 30 | + editData: undefined |
| 31 | +}; |
5 | 32 |
|
6 | | -const _create = (prefix) => (openRosaUrl) => new Promise((resolve, reject) => { |
7 | | - const state = global.enketoPreviewTest; |
8 | | - global.enketoPreviewTest = null; |
9 | | - const token = global.enketoToken || `${prefix}abcdefgh`; |
10 | | - global.enketoToken = null; |
11 | | - global.enketoReceivedUrl = null; |
12 | | - const wait = global.enketoWait || call; |
13 | | - global.enketoWait = null; |
14 | | - |
15 | | - wait(() => { |
16 | | - if (state === 'error') { |
17 | | - // pretend that Enketo has misbehaved |
18 | | - reject(Problem.internal.enketoUnexpectedResponse('wrong status code')); |
19 | | - } else { |
20 | | - global.enketoReceivedUrl = openRosaUrl; |
21 | | - resolve(token); |
22 | | - } |
| 33 | +let cancelToken = 0; |
| 34 | + |
| 35 | +const reset = () => { |
| 36 | + if (global.enketo === undefined) global.enketo = {}; |
| 37 | + Object.assign(global.enketo, defaults); |
| 38 | + cancelToken += 1; |
| 39 | +}; |
| 40 | + |
| 41 | +// Mocks a request to Enketo. |
| 42 | +const request = (f) => { |
| 43 | + global.enketo.callCount += 1; |
| 44 | + const options = { ...global.enketo }; |
| 45 | + Object.assign(global.enketo, without(['callCount'], defaults)); |
| 46 | + return new Promise((resolve, reject) => { |
| 47 | + const { wait } = options; |
| 48 | + const tokenBeforeWait = cancelToken; |
| 49 | + wait(() => { |
| 50 | + if (cancelToken !== tokenBeforeWait) |
| 51 | + reject(new Error('request was canceled')); |
| 52 | + else if (options.state === 'error') |
| 53 | + reject(Problem.internal.enketoUnexpectedResponse('wrong status code')); |
| 54 | + else |
| 55 | + resolve(f(options)); |
| 56 | + }); |
23 | 57 | }); |
24 | | -}); |
| 58 | +}; |
25 | 59 |
|
26 | | -const edit = (openRosaUrl, domain, form, logicalId, submissionDef, attachments, token) => new Promise((resolve) => { |
27 | | - global.enketoEditTest = null; |
28 | | - global.enketoEditData = { openRosaUrl, domain, form, logicalId, submissionDef, attachments, token }; |
29 | | - resolve('https://enketo/edit/url'); |
30 | | -}); |
| 60 | +const _create = (prefix) => (openRosaUrl) => |
| 61 | + request(({ token = `${prefix}abcdefgh` }) => { |
| 62 | + global.enketo.receivedUrl = openRosaUrl; |
| 63 | + return token; |
| 64 | + }); |
| 65 | + |
| 66 | +const edit = (openRosaUrl, domain, form, logicalId, submissionDef, attachments, token) => |
| 67 | + request(() => { |
| 68 | + global.enketo.editData = { openRosaUrl, domain, form, logicalId, submissionDef, attachments, token }; |
| 69 | + return 'https://enketo/edit/url'; |
| 70 | + }); |
31 | 71 |
|
32 | | -module.exports = { create: _create('::'), createOnceToken: _create('::::'), edit }; |
| 72 | +module.exports = { |
| 73 | + create: _create('::'), createOnceToken: _create('::::'), edit, |
| 74 | + reset |
| 75 | +}; |
33 | 76 |
|
0 commit comments