cozy-harvest-lib 22.0.5 → 22.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [22.0.7](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@22.0.6...cozy-harvest-lib@22.0.7) (2024-01-17)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Do not disable submit buton when password is the same ([f431e7f](https://github.com/cozy/cozy-libs/commit/f431e7fda2e1d16618f687c5b16fbfbef83ab0e7))
12
+
13
+
14
+
15
+
16
+
17
+ ## [22.0.6](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@22.0.5...cozy-harvest-lib@22.0.6) (2024-01-08)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * Use konnectorFolder from cozy-client ([f81eed9](https://github.com/cozy/cozy-libs/commit/f81eed99e502f2c1378c6c2d0daa157047967f6e))
23
+
24
+
25
+
26
+
27
+
6
28
  ## [22.0.5](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@22.0.4...cozy-harvest-lib@22.0.5) (2024-01-08)
7
29
 
8
30
 
@@ -114,14 +114,12 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
114
114
  _createClass(AccountForm, [{
115
115
  key: "isSubmittable",
116
116
  value: function isSubmittable(_ref) {
117
- var dirty = _ref.dirty,
118
- error = _ref.error,
119
- initialValues = _ref.initialValues,
117
+ var error = _ref.error,
120
118
  valid = _ref.valid;
121
- var modified = !initialValues || dirty; // Here error is not a validation error but an instance of a
119
+ // Here error is not a validation error but an instance of a
122
120
  // KonnectorJobError, so submitting again is possible
123
-
124
- return modified && (error || valid);
121
+ var result = error || valid;
122
+ return result;
125
123
  }
126
124
  /**
127
125
  * Give focus to next input in the form.
@@ -161,10 +159,8 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
161
159
  * Handle key up and check if `ENTER` key has been pressed. If so, submit the
162
160
  * form if all parmater are ok.
163
161
  * @param {React.SyntheticEvent} event Key events
164
- * @param {Boolean} dirty Indicates if form is dirty, i.e. if values
165
162
  * have changed
166
163
  * @param {Object} form The form object injected by ReactFinalForm.
167
- * @param {Object} initialValues Initial data injected into AccountForm
168
164
  * @param {Boolean} valid Indicates if the form data is valid
169
165
  * @param {Object} values Actual form values data
170
166
  */
@@ -172,9 +168,7 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
172
168
  }, {
173
169
  key: "handleKeyUp",
174
170
  value: function handleKeyUp(event, _ref2) {
175
- var dirty = _ref2.dirty,
176
- form = _ref2.form,
177
- initialValues = _ref2.initialValues,
171
+ var form = _ref2.form,
178
172
  valid = _ref2.valid,
179
173
  values = _ref2.values;
180
174
 
@@ -182,8 +176,6 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
182
176
  var changedFocus = isMobile() && !!this.focusNext();
183
177
 
184
178
  if (!changedFocus && this.isSubmittable({
185
- dirty: dirty,
186
- initialValues: initialValues,
187
179
  valid: valid
188
180
  })) {
189
181
  this.handleSubmit(values, form);
@@ -303,7 +295,6 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
303
295
  vendor_link = konnector.vendor_link;
304
296
  var sanitizedFields = manifest.sanitizeFields(fields);
305
297
  fieldHelpers.addForceEncryptedPlaceholder(sanitizedFields, fieldOptions);
306
- var initialValues = account && account.auth;
307
298
  var values = manifest.getFieldsValues(konnector, account);
308
299
  var isReadOnlyIdentifier = Boolean(get(account, 'relationships.vaultCipher')) && this.props.readOnlyIdentifier;
309
300
 
@@ -321,16 +312,13 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
321
312
  onSubmit: onSubmit,
322
313
  validate: this.validate(sanitizedFields, values),
323
314
  render: function render(_ref3) {
324
- var dirty = _ref3.dirty,
325
- form = _ref3.form,
315
+ var form = _ref3.form,
326
316
  values = _ref3.values,
327
317
  valid = _ref3.valid;
328
318
  return /*#__PURE__*/React.createElement("div", {
329
319
  onKeyUp: function onKeyUp(event) {
330
320
  return _this3.handleKeyUp(event, {
331
- dirty: dirty,
332
321
  form: form,
333
- initialValues: initialValues,
334
322
  valid: valid,
335
323
  values: values
336
324
  });
@@ -365,9 +353,7 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
365
353
  buttonProps: _defineProperty({
366
354
  busy: submitting && !flag('harvest.inappconnectors.enabled'),
367
355
  disabled: submitting || !_this3.isSubmittable({
368
- dirty: dirty,
369
356
  error: error,
370
- initialValues: initialValues,
371
357
  valid: valid
372
358
  }),
373
359
  label: t('accountForm.clientSide.submit'),
@@ -379,9 +365,7 @@ export var AccountForm = /*#__PURE__*/function (_PureComponent) {
379
365
  busy: submitting && !flag('harvest.inappconnectors.enabled'),
380
366
  className: "u-mt-2 u-mb-1-half",
381
367
  disabled: submitting || !_this3.isSubmittable({
382
- dirty: dirty,
383
368
  error: error,
384
- initialValues: initialValues,
385
369
  valid: valid
386
370
  }),
387
371
  fullWidth: true,
@@ -260,52 +260,15 @@ describe('AccountForm', function () {
260
260
 
261
261
  assertButtonEnabled(wrapper);
262
262
  });
263
- it('should be disabled with initialValues', function () {
264
- var account = {
265
- auth: {
266
- username: 'foo',
267
- passphrase: 'bar'
268
- }
269
- };
270
-
271
- var _setup12 = setup({
272
- account: account
273
- }),
274
- wrapper = _setup12.wrapper;
275
-
276
- assertButtonDisabled(wrapper);
277
- });
278
- /**
279
- * This case corresponds to an account edition of a trigger already having
280
- * an error. We are not supposed to submit it without modifications.
281
- */
282
-
283
- it('should be disabled with initialValues AND error', function () {
284
- var account = {
285
- auth: {
286
- username: 'foo',
287
- passphrase: 'bar'
288
- }
289
- };
290
- var error = new Error('Existing trigger error');
291
-
292
- var _setup13 = setup({
293
- account: account,
294
- error: error
295
- }),
296
- wrapper = _setup13.wrapper;
297
-
298
- assertButtonDisabled(wrapper);
299
- });
300
263
  it('should be enabled when an error exists', function () {
301
264
  var account = {};
302
265
  var error = new Error('Test error');
303
266
 
304
- var _setup14 = setup({
267
+ var _setup12 = setup({
305
268
  account: account,
306
269
  error: error
307
270
  }),
308
- wrapper = _setup14.wrapper;
271
+ wrapper = _setup12.wrapper;
309
272
 
310
273
  assertButtonEnabled(wrapper);
311
274
  });
@@ -314,8 +277,8 @@ describe('AccountForm', function () {
314
277
  var loginInput = document.createElement('input');
315
278
  var passwordInput = document.createElement('input');
316
279
  it('should focus next input', function () {
317
- var _setup15 = setup(),
318
- wrapper = _setup15.wrapper;
280
+ var _setup13 = setup(),
281
+ wrapper = _setup13.wrapper;
319
282
 
320
283
  wrapper.instance().inputs = {
321
284
  login: loginInput,
@@ -334,8 +297,8 @@ describe('AccountForm', function () {
334
297
  it('should ignore other keys than ENTER', function () {
335
298
  isMobile.mockReturnValue(false);
336
299
 
337
- var _setup16 = setup(),
338
- wrapper = _setup16.wrapper;
300
+ var _setup14 = setup(),
301
+ wrapper = _setup14.wrapper;
339
302
 
340
303
  wrapper.instance().handleSubmit = jest.fn();
341
304
  wrapper.instance().handleKeyUp({
@@ -346,8 +309,8 @@ describe('AccountForm', function () {
346
309
  it('should submit form', function () {
347
310
  isMobile.mockReturnValue(false);
348
311
 
349
- var _setup17 = setup(),
350
- wrapper = _setup17.wrapper;
312
+ var _setup15 = setup(),
313
+ wrapper = _setup15.wrapper;
351
314
 
352
315
  wrapper.instance().handleSubmit = jest.fn();
353
316
  wrapper.instance().isSubmittable = jest.fn().mockReturnValue(true);
@@ -359,8 +322,8 @@ describe('AccountForm', function () {
359
322
  it('should not submit form', function () {
360
323
  isMobile.mockReturnValue(false);
361
324
 
362
- var _setup18 = setup({}),
363
- wrapper = _setup18.wrapper;
325
+ var _setup16 = setup({}),
326
+ wrapper = _setup16.wrapper;
364
327
 
365
328
  wrapper.instance().handleSubmit = jest.fn();
366
329
  wrapper.instance().isSubmittable = jest.fn().mockReturnValue(false);
@@ -372,8 +335,8 @@ describe('AccountForm', function () {
372
335
  it('should focus next input on mobile', function () {
373
336
  isMobile.mockReturnValue(true);
374
337
 
375
- var _setup19 = setup(),
376
- wrapper = _setup19.wrapper;
338
+ var _setup17 = setup(),
339
+ wrapper = _setup17.wrapper;
377
340
 
378
341
  wrapper.instance().focusNext = jest.fn().mockReturnValue(document.createElement('input'));
379
342
  wrapper.instance().handleKeyUp({
@@ -384,8 +347,8 @@ describe('AccountForm', function () {
384
347
  it('should submit form on mobile', function () {
385
348
  isMobile.mockReturnValue(true);
386
349
 
387
- var _setup20 = setup(),
388
- wrapper = _setup20.wrapper;
350
+ var _setup18 = setup(),
351
+ wrapper = _setup18.wrapper;
389
352
 
390
353
  wrapper.instance().focusNext = jest.fn().mockReturnValue(null);
391
354
  wrapper.instance().isSubmittable = jest.fn().mockReturnValue(true);
@@ -399,8 +362,8 @@ describe('AccountForm', function () {
399
362
  it('should not submit form on mobile', function () {
400
363
  isMobile.mockReturnValue(true);
401
364
 
402
- var _setup21 = setup(),
403
- wrapper = _setup21.wrapper;
365
+ var _setup19 = setup(),
366
+ wrapper = _setup19.wrapper;
404
367
 
405
368
  wrapper.instance().focusNext = jest.fn().mockReturnValue(null);
406
369
  wrapper.instance().isSubmittable = jest.fn().mockReturnValue(false);
@@ -1,15 +1,11 @@
1
- import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
1
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ import { ensureKonnectorFolder } from 'cozy-client/dist/models/konnectorFolder';
4
4
  import { triggers as triggersModel } from 'cozy-client/dist/models/trigger';
5
- import { CozyFolder as CozyFolderClass } from 'cozy-doctypes';
6
5
  import { fetchAccount, updateAccount } from './accounts';
7
- import { statDirectoryByPath, createDirectoryByPath } from './files';
8
6
  import * as accounts from '../helpers/accounts';
9
7
  import cron from '../helpers/cron';
10
8
  import * as konnectors from '../helpers/konnectors';
11
- var FILES_DOCTYPE = 'io.cozy.files';
12
- var PERMISSIONS_DOCTYPE = 'io.cozy.permissions';
13
9
  var TRIGGERS_DOCTYPE = 'io.cozy.triggers';
14
10
  /**
15
11
  * Creates a trigger with given attributes
@@ -174,96 +170,29 @@ export var triggersMutations = function triggersMutations(client) {
174
170
  launchTrigger: launchTrigger.bind(null, client)
175
171
  };
176
172
  };
177
-
178
- var ensureKonnectorFolder = /*#__PURE__*/function () {
173
+ export var ensureTrigger = /*#__PURE__*/function () {
179
174
  var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(client, _ref5) {
180
- var konnector, account, t, permissions, files, CozyFolder, _yield$Promise$all, _yield$Promise$all2, adminFolder, photosFolder, path, folder;
181
-
175
+ var trigger, account, konnector, t, folder;
182
176
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
183
177
  while (1) {
184
178
  switch (_context5.prev = _context5.next) {
185
179
  case 0:
186
- konnector = _ref5.konnector, account = _ref5.account, t = _ref5.t;
187
- permissions = client.collection(PERMISSIONS_DOCTYPE);
188
- files = client.collection(FILES_DOCTYPE);
189
- CozyFolder = CozyFolderClass.copyWithClient(client);
190
- _context5.next = 6;
191
- return Promise.all([CozyFolder.ensureMagicFolder(CozyFolder.magicFolders.ADMINISTRATIVE, "/".concat(t('folder.administrative'))), CozyFolder.ensureMagicFolder(CozyFolder.magicFolders.PHOTOS, "/".concat(t('folder.photos')))]);
192
-
193
- case 6:
194
- _yield$Promise$all = _context5.sent;
195
- _yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
196
- adminFolder = _yield$Promise$all2[0];
197
- photosFolder = _yield$Promise$all2[1];
198
- path = konnectors.buildFolderPath(konnector, account, {
199
- administrative: adminFolder.path,
200
- photos: photosFolder.path
201
- });
202
- _context5.next = 13;
203
- return statDirectoryByPath(client, path);
204
-
205
- case 13:
206
- _context5.t0 = _context5.sent;
207
-
208
- if (_context5.t0) {
209
- _context5.next = 18;
210
- break;
211
- }
212
-
213
- _context5.next = 17;
214
- return createDirectoryByPath(client, path);
215
-
216
- case 17:
217
- _context5.t0 = _context5.sent;
218
-
219
- case 18:
220
- folder = _context5.t0;
221
- _context5.next = 21;
222
- return permissions.add(konnector, konnectors.buildFolderPermission(folder));
223
-
224
- case 21:
225
- _context5.next = 23;
226
- return files.addReferencesTo(konnector, [folder]);
227
-
228
- case 23:
229
- return _context5.abrupt("return", folder);
230
-
231
- case 24:
232
- case "end":
233
- return _context5.stop();
234
- }
235
- }
236
- }, _callee5);
237
- }));
238
-
239
- return function ensureKonnectorFolder(_x9, _x10) {
240
- return _ref6.apply(this, arguments);
241
- };
242
- }();
243
-
244
- export var ensureTrigger = /*#__PURE__*/function () {
245
- var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(client, _ref7) {
246
- var trigger, account, konnector, t, folder;
247
- return _regeneratorRuntime.wrap(function _callee6$(_context6) {
248
- while (1) {
249
- switch (_context6.prev = _context6.next) {
250
- case 0:
251
- trigger = _ref7.trigger, account = _ref7.account, konnector = _ref7.konnector, t = _ref7.t;
180
+ trigger = _ref5.trigger, account = _ref5.account, konnector = _ref5.konnector, t = _ref5.t;
252
181
 
253
182
  if (!trigger) {
254
- _context6.next = 3;
183
+ _context5.next = 3;
255
184
  break;
256
185
  }
257
186
 
258
- return _context6.abrupt("return", trigger);
187
+ return _context5.abrupt("return", trigger);
259
188
 
260
189
  case 3:
261
190
  if (!konnectors.needsFolder(konnector)) {
262
- _context6.next = 7;
191
+ _context5.next = 7;
263
192
  break;
264
193
  }
265
194
 
266
- _context6.next = 6;
195
+ _context5.next = 6;
267
196
  return ensureKonnectorFolder(client, {
268
197
  konnector: konnector,
269
198
  account: account,
@@ -271,10 +200,10 @@ export var ensureTrigger = /*#__PURE__*/function () {
271
200
  });
272
201
 
273
202
  case 6:
274
- folder = _context6.sent;
203
+ folder = _context5.sent;
275
204
 
276
205
  case 7:
277
- _context6.next = 9;
206
+ _context5.next = 9;
278
207
  return createTrigger(client, triggersModel.buildTriggerAttributes({
279
208
  account: account,
280
209
  cron: cron.fromKonnector(konnector),
@@ -283,18 +212,18 @@ export var ensureTrigger = /*#__PURE__*/function () {
283
212
  }));
284
213
 
285
214
  case 9:
286
- return _context6.abrupt("return", _context6.sent);
215
+ return _context5.abrupt("return", _context5.sent);
287
216
 
288
217
  case 10:
289
218
  case "end":
290
- return _context6.stop();
219
+ return _context5.stop();
291
220
  }
292
221
  }
293
- }, _callee6);
222
+ }, _callee5);
294
223
  }));
295
224
 
296
- return function ensureTrigger(_x11, _x12) {
297
- return _ref8.apply(this, arguments);
225
+ return function ensureTrigger(_x9, _x10) {
226
+ return _ref6.apply(this, arguments);
298
227
  };
299
228
  }();
300
229
  export default triggersMutations;
@@ -2,39 +2,9 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
2
  import _regeneratorRuntime from "@babel/runtime/regenerator";
3
3
 
4
4
  /* eslint-env jest */
5
- import { createTrigger, launchTrigger, ensureTrigger } from 'connections/triggers';
6
- import Polyglot from 'node-polyglot';
5
+ import { createTrigger, launchTrigger } from 'connections/triggers';
7
6
  import CozyClient from 'cozy-client';
8
- import { CozyFolder } from 'cozy-doctypes';
9
- import { statDirectoryByPath, createDirectoryByPath } from '../../src/connections/files';
10
- import en from '../../src/locales/en.json';
11
7
  import fixtures from '../../test/fixtures';
12
- jest.mock('../../src/connections/files', function () {
13
- return {
14
- statDirectoryByPath: jest.fn(),
15
- createDirectoryByPath: jest.fn()
16
- };
17
- });
18
- afterEach(function () {
19
- jest.restoreAllMocks();
20
- statDirectoryByPath.mockClear();
21
- createDirectoryByPath.mockClear();
22
- });
23
-
24
- CozyFolder.copyWithClient = function () {
25
- return {
26
- magicFolders: CozyFolder.magicFolders,
27
- ensureMagicFolder: function ensureMagicFolder(id, path) {
28
- return {
29
- path: path
30
- };
31
- }
32
- };
33
- };
34
-
35
- var polyglot = new Polyglot();
36
- polyglot.extend(en);
37
- var fakeT = polyglot.t.bind(polyglot);
38
8
 
39
9
  var setup = function setup() {
40
10
  var client = new CozyClient({});
@@ -46,26 +16,12 @@ var setup = function setup() {
46
16
  data: fixtures.launchedJob
47
17
  })
48
18
  };
49
- var permissionCollection = {
50
- add: jest.fn().mockResolvedValue({})
51
- };
52
- var fileCollection = {
53
- addReferencesTo: jest.fn().mockResolvedValue({})
54
- };
55
- jest.spyOn(CozyClient.prototype, 'collection').mockImplementation(function (doctype) {
56
- if (doctype == 'io.cozy.triggers') {
57
- return triggerCollection;
58
- } else if (doctype == 'io.cozy.permissions') {
59
- return permissionCollection;
60
- } else if (doctype == 'io.cozy.files') {
61
- return fileCollection;
62
- }
19
+ jest.spyOn(CozyClient.prototype, 'collection').mockImplementation(function () {
20
+ return triggerCollection;
63
21
  });
64
22
  return {
65
23
  client: client,
66
- triggerCollection: triggerCollection,
67
- permissionCollection: permissionCollection,
68
- fileCollection: fileCollection
24
+ triggerCollection: triggerCollection
69
25
  };
70
26
  };
71
27
 
@@ -120,73 +76,4 @@ describe('Trigger mutations', function () {
120
76
  }, _callee2);
121
77
  })));
122
78
  });
123
- });
124
- describe('when konnector needs folder', function () {
125
- it('should create folder if it does not exist', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
126
- var _setup3, client, permissionCollection, fileCollection, addReferencesTo;
127
-
128
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
129
- while (1) {
130
- switch (_context3.prev = _context3.next) {
131
- case 0:
132
- _setup3 = setup(), client = _setup3.client, permissionCollection = _setup3.permissionCollection, fileCollection = _setup3.fileCollection;
133
- statDirectoryByPath.mockResolvedValue(null);
134
- createDirectoryByPath.mockReturnValue(fixtures.folder);
135
- _context3.next = 5;
136
- return ensureTrigger(client, {
137
- konnector: fixtures.konnectorWithFolder,
138
- account: fixtures.account,
139
- t: fakeT
140
- });
141
-
142
- case 5:
143
- expect(statDirectoryByPath).toHaveBeenCalledTimes(1);
144
- expect(createDirectoryByPath).toHaveBeenCalledTimes(1);
145
- expect(createDirectoryByPath).toHaveBeenCalledWith(client, fixtures.folderPath);
146
- expect(permissionCollection.add).toHaveBeenCalledTimes(1);
147
- expect(permissionCollection.add).toHaveBeenCalledWith(fixtures.konnectorWithFolder, fixtures.folderPermission);
148
- addReferencesTo = fileCollection.addReferencesTo;
149
- expect(addReferencesTo).toHaveBeenCalledTimes(1);
150
- expect(addReferencesTo).toHaveBeenCalledWith(fixtures.konnectorWithFolder, [fixtures.folder]);
151
-
152
- case 13:
153
- case "end":
154
- return _context3.stop();
155
- }
156
- }
157
- }, _callee3);
158
- })));
159
- it('should not create folder if it exists', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
160
- var _setup4, client, permissionCollection, fileCollection, addPermission, addReferencesTo;
161
-
162
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
163
- while (1) {
164
- switch (_context4.prev = _context4.next) {
165
- case 0:
166
- _setup4 = setup(), client = _setup4.client, permissionCollection = _setup4.permissionCollection, fileCollection = _setup4.fileCollection;
167
- statDirectoryByPath.mockResolvedValue(fixtures.folder);
168
- _context4.next = 4;
169
- return ensureTrigger(client, {
170
- account: fixtures.account,
171
- konnector: fixtures.konnectorWithFolder,
172
- t: fakeT
173
- });
174
-
175
- case 4:
176
- expect(statDirectoryByPath).toHaveBeenCalledTimes(1);
177
- expect(createDirectoryByPath).toHaveBeenCalledTimes(0);
178
- addPermission = permissionCollection.add;
179
- expect(addPermission).toHaveBeenCalledTimes(1);
180
- expect(addPermission).toHaveBeenCalledWith(fixtures.konnectorWithFolder, fixtures.folderPermission);
181
- addReferencesTo = fileCollection.addReferencesTo;
182
- expect(addReferencesTo).toHaveBeenCalledTimes(1);
183
- expect(addReferencesTo).toHaveBeenCalledWith(fixtures.konnectorWithFolder, [fixtures.folder]);
184
-
185
- case 12:
186
- case "end":
187
- return _context4.stop();
188
- }
189
- }
190
- }, _callee4);
191
- })));
192
79
  });
@@ -37,12 +37,6 @@ jest.mock('../sentry', function () {
37
37
  captureException: jest.fn()
38
38
  };
39
39
  });
40
- jest.mock('../connections/files', function () {
41
- return {
42
- statDirectoryByPath: jest.fn(),
43
- createDirectoryByPath: jest.fn()
44
- };
45
- });
46
40
  KonnectorJobWatcher.prototype.watch = jest.fn();
47
41
  jest.mock('date-fns');
48
42
  var realtimeMock = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-harvest-lib",
3
- "version": "22.0.5",
3
+ "version": "22.0.7",
4
4
  "description": "Provides logic, modules and components for Cozy's harvest applications.",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy",
@@ -106,5 +106,5 @@
106
106
  "react-router-dom": ">=4.3.1"
107
107
  },
108
108
  "sideEffects": false,
109
- "gitHead": "1d769fb5515fbfa5344e69c089536d8b20c27fee"
109
+ "gitHead": "e96e3f59e027dc5c55614bbe1877b0b7a527b076"
110
110
  }
@@ -71,11 +71,11 @@ export class AccountForm extends PureComponent {
71
71
  * @param {Object} formState See https://github.com/final-form/final-form#formstate
72
72
  * @return {Boolean}
73
73
  */
74
- isSubmittable({ dirty, error, initialValues, valid }) {
75
- const modified = !initialValues || dirty
74
+ isSubmittable({ error, valid }) {
76
75
  // Here error is not a validation error but an instance of a
77
76
  // KonnectorJobError, so submitting again is possible
78
- return modified && (error || valid)
77
+ const result = error || valid
78
+ return result
79
79
  }
80
80
 
81
81
  /**
@@ -115,20 +115,15 @@ export class AccountForm extends PureComponent {
115
115
  * Handle key up and check if `ENTER` key has been pressed. If so, submit the
116
116
  * form if all parmater are ok.
117
117
  * @param {React.SyntheticEvent} event Key events
118
- * @param {Boolean} dirty Indicates if form is dirty, i.e. if values
119
118
  * have changed
120
119
  * @param {Object} form The form object injected by ReactFinalForm.
121
- * @param {Object} initialValues Initial data injected into AccountForm
122
120
  * @param {Boolean} valid Indicates if the form data is valid
123
121
  * @param {Object} values Actual form values data
124
122
  */
125
- handleKeyUp(event, { dirty, form, initialValues, valid, values }) {
123
+ handleKeyUp(event, { form, valid, values }) {
126
124
  if (event.key === 'Enter') {
127
125
  const changedFocus = isMobile() && !!this.focusNext()
128
- if (
129
- !changedFocus &&
130
- this.isSubmittable({ dirty, initialValues, valid })
131
- ) {
126
+ if (!changedFocus && this.isSubmittable({ valid })) {
132
127
  this.handleSubmit(values, form)
133
128
  }
134
129
  }
@@ -242,7 +237,6 @@ export class AccountForm extends PureComponent {
242
237
  const sanitizedFields = manifest.sanitizeFields(fields)
243
238
  fieldHelpers.addForceEncryptedPlaceholder(sanitizedFields, fieldOptions)
244
239
 
245
- const initialValues = account && account.auth
246
240
  const values = manifest.getFieldsValues(konnector, account)
247
241
 
248
242
  const isReadOnlyIdentifier =
@@ -263,13 +257,11 @@ export class AccountForm extends PureComponent {
263
257
  initialValues={values}
264
258
  onSubmit={onSubmit}
265
259
  validate={this.validate(sanitizedFields, values)}
266
- render={({ dirty, form, values, valid }) => (
260
+ render={({ form, values, valid }) => (
267
261
  <div
268
262
  onKeyUp={event =>
269
263
  this.handleKeyUp(event, {
270
- dirty,
271
264
  form,
272
- initialValues,
273
265
  valid,
274
266
  values
275
267
  })
@@ -327,9 +319,7 @@ export class AccountForm extends PureComponent {
327
319
  disabled:
328
320
  submitting ||
329
321
  !this.isSubmittable({
330
- dirty,
331
322
  error,
332
- initialValues,
333
323
  valid
334
324
  }),
335
325
  label: t('accountForm.clientSide.submit'),
@@ -346,9 +336,7 @@ export class AccountForm extends PureComponent {
346
336
  disabled={
347
337
  submitting ||
348
338
  !this.isSubmittable({
349
- dirty,
350
339
  error,
351
- initialValues,
352
340
  valid
353
341
  })
354
342
  }
@@ -230,40 +230,6 @@ describe('AccountForm', () => {
230
230
  assertButtonEnabled(wrapper)
231
231
  })
232
232
 
233
- it('should be disabled with initialValues', () => {
234
- const account = {
235
- auth: {
236
- username: 'foo',
237
- passphrase: 'bar'
238
- }
239
- }
240
- const { wrapper } = setup({
241
- account
242
- })
243
-
244
- assertButtonDisabled(wrapper)
245
- })
246
-
247
- /**
248
- * This case corresponds to an account edition of a trigger already having
249
- * an error. We are not supposed to submit it without modifications.
250
- */
251
- it('should be disabled with initialValues AND error', () => {
252
- const account = {
253
- auth: {
254
- username: 'foo',
255
- passphrase: 'bar'
256
- }
257
- }
258
- const error = new Error('Existing trigger error')
259
- const { wrapper } = setup({
260
- account,
261
- error
262
- })
263
-
264
- assertButtonDisabled(wrapper)
265
- })
266
-
267
233
  it('should be enabled when an error exists', () => {
268
234
  const account = {}
269
235
  const error = new Error('Test error')
@@ -1,14 +1,11 @@
1
+ import { ensureKonnectorFolder } from 'cozy-client/dist/models/konnectorFolder'
1
2
  import { triggers as triggersModel } from 'cozy-client/dist/models/trigger'
2
- import { CozyFolder as CozyFolderClass } from 'cozy-doctypes'
3
3
 
4
4
  import { fetchAccount, updateAccount } from './accounts'
5
- import { statDirectoryByPath, createDirectoryByPath } from './files'
6
5
  import * as accounts from '../helpers/accounts'
7
6
  import cron from '../helpers/cron'
8
7
  import * as konnectors from '../helpers/konnectors'
9
8
 
10
- const FILES_DOCTYPE = 'io.cozy.files'
11
- const PERMISSIONS_DOCTYPE = 'io.cozy.permissions'
12
9
  const TRIGGERS_DOCTYPE = 'io.cozy.triggers'
13
10
 
14
11
  /**
@@ -71,34 +68,6 @@ export const triggersMutations = client => {
71
68
  }
72
69
  }
73
70
 
74
- const ensureKonnectorFolder = async (client, { konnector, account, t }) => {
75
- const permissions = client.collection(PERMISSIONS_DOCTYPE)
76
- const files = client.collection(FILES_DOCTYPE)
77
- const CozyFolder = CozyFolderClass.copyWithClient(client)
78
- const [adminFolder, photosFolder] = await Promise.all([
79
- CozyFolder.ensureMagicFolder(
80
- CozyFolder.magicFolders.ADMINISTRATIVE,
81
- `/${t('folder.administrative')}`
82
- ),
83
- CozyFolder.ensureMagicFolder(
84
- CozyFolder.magicFolders.PHOTOS,
85
- `/${t('folder.photos')}`
86
- )
87
- ])
88
- const path = konnectors.buildFolderPath(konnector, account, {
89
- administrative: adminFolder.path,
90
- photos: photosFolder.path
91
- })
92
- const folder =
93
- (await statDirectoryByPath(client, path)) ||
94
- (await createDirectoryByPath(client, path))
95
-
96
- await permissions.add(konnector, konnectors.buildFolderPermission(folder))
97
- await files.addReferencesTo(konnector, [folder])
98
-
99
- return folder
100
- }
101
-
102
71
  export const ensureTrigger = async (
103
72
  client,
104
73
  { trigger, account, konnector, t }
@@ -110,7 +79,11 @@ export const ensureTrigger = async (
110
79
  let folder
111
80
 
112
81
  if (konnectors.needsFolder(konnector)) {
113
- folder = await ensureKonnectorFolder(client, { konnector, account, t })
82
+ folder = await ensureKonnectorFolder(client, {
83
+ konnector,
84
+ account,
85
+ t
86
+ })
114
87
  }
115
88
 
116
89
  return await createTrigger(
@@ -1,43 +1,10 @@
1
1
  /* eslint-env jest */
2
- import {
3
- createTrigger,
4
- launchTrigger,
5
- ensureTrigger
6
- } from 'connections/triggers'
7
- import Polyglot from 'node-polyglot'
2
+ import { createTrigger, launchTrigger } from 'connections/triggers'
8
3
 
9
4
  import CozyClient from 'cozy-client'
10
- import { CozyFolder } from 'cozy-doctypes'
11
5
 
12
- import {
13
- statDirectoryByPath,
14
- createDirectoryByPath
15
- } from '../../src/connections/files'
16
- import en from '../../src/locales/en.json'
17
6
  import fixtures from '../../test/fixtures'
18
7
 
19
- jest.mock('../../src/connections/files', () => ({
20
- statDirectoryByPath: jest.fn(),
21
- createDirectoryByPath: jest.fn()
22
- }))
23
-
24
- afterEach(() => {
25
- jest.restoreAllMocks()
26
- statDirectoryByPath.mockClear()
27
- createDirectoryByPath.mockClear()
28
- })
29
-
30
- CozyFolder.copyWithClient = () => ({
31
- magicFolders: CozyFolder.magicFolders,
32
- ensureMagicFolder: (id, path) => ({
33
- path
34
- })
35
- })
36
-
37
- const polyglot = new Polyglot()
38
- polyglot.extend(en)
39
- const fakeT = polyglot.t.bind(polyglot)
40
-
41
8
  const setup = () => {
42
9
  const client = new CozyClient({})
43
10
  const triggerCollection = {
@@ -49,29 +16,13 @@ const setup = () => {
49
16
  })
50
17
  }
51
18
 
52
- const permissionCollection = {
53
- add: jest.fn().mockResolvedValue({})
54
- }
55
-
56
- const fileCollection = {
57
- addReferencesTo: jest.fn().mockResolvedValue({})
58
- }
59
-
60
- jest.spyOn(CozyClient.prototype, 'collection').mockImplementation(doctype => {
61
- if (doctype == 'io.cozy.triggers') {
62
- return triggerCollection
63
- } else if (doctype == 'io.cozy.permissions') {
64
- return permissionCollection
65
- } else if (doctype == 'io.cozy.files') {
66
- return fileCollection
67
- }
19
+ jest.spyOn(CozyClient.prototype, 'collection').mockImplementation(() => {
20
+ return triggerCollection
68
21
  })
69
22
 
70
23
  return {
71
24
  client,
72
- triggerCollection,
73
- permissionCollection,
74
- fileCollection
25
+ triggerCollection
75
26
  }
76
27
  }
77
28
 
@@ -94,63 +45,3 @@ describe('Trigger mutations', () => {
94
45
  })
95
46
  })
96
47
  })
97
-
98
- describe('when konnector needs folder', () => {
99
- it('should create folder if it does not exist', async () => {
100
- const { client, permissionCollection, fileCollection } = setup()
101
- statDirectoryByPath.mockResolvedValue(null)
102
- createDirectoryByPath.mockReturnValue(fixtures.folder)
103
-
104
- await ensureTrigger(client, {
105
- konnector: fixtures.konnectorWithFolder,
106
- account: fixtures.account,
107
- t: fakeT
108
- })
109
-
110
- expect(statDirectoryByPath).toHaveBeenCalledTimes(1)
111
- expect(createDirectoryByPath).toHaveBeenCalledTimes(1)
112
- expect(createDirectoryByPath).toHaveBeenCalledWith(
113
- client,
114
- fixtures.folderPath
115
- )
116
-
117
- expect(permissionCollection.add).toHaveBeenCalledTimes(1)
118
- expect(permissionCollection.add).toHaveBeenCalledWith(
119
- fixtures.konnectorWithFolder,
120
- fixtures.folderPermission
121
- )
122
-
123
- const addReferencesTo = fileCollection.addReferencesTo
124
- expect(addReferencesTo).toHaveBeenCalledTimes(1)
125
- expect(addReferencesTo).toHaveBeenCalledWith(fixtures.konnectorWithFolder, [
126
- fixtures.folder
127
- ])
128
- })
129
-
130
- it('should not create folder if it exists', async () => {
131
- const { client, permissionCollection, fileCollection } = setup()
132
- statDirectoryByPath.mockResolvedValue(fixtures.folder)
133
-
134
- await ensureTrigger(client, {
135
- account: fixtures.account,
136
- konnector: fixtures.konnectorWithFolder,
137
- t: fakeT
138
- })
139
-
140
- expect(statDirectoryByPath).toHaveBeenCalledTimes(1)
141
- expect(createDirectoryByPath).toHaveBeenCalledTimes(0)
142
-
143
- const addPermission = permissionCollection.add
144
- expect(addPermission).toHaveBeenCalledTimes(1)
145
- expect(addPermission).toHaveBeenCalledWith(
146
- fixtures.konnectorWithFolder,
147
- fixtures.folderPermission
148
- )
149
-
150
- const addReferencesTo = fileCollection.addReferencesTo
151
- expect(addReferencesTo).toHaveBeenCalledTimes(1)
152
- expect(addReferencesTo).toHaveBeenCalledWith(fixtures.konnectorWithFolder, [
153
- fixtures.folder
154
- ])
155
- })
156
- })
@@ -32,11 +32,6 @@ jest.mock('../sentry', () => {
32
32
  }
33
33
  })
34
34
 
35
- jest.mock('../connections/files', () => ({
36
- statDirectoryByPath: jest.fn(),
37
- createDirectoryByPath: jest.fn()
38
- }))
39
-
40
35
  KonnectorJobWatcher.prototype.watch = jest.fn()
41
36
 
42
37
  jest.mock('date-fns')
@@ -1,86 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- import _regeneratorRuntime from "@babel/runtime/regenerator";
3
- var FILES_DOCTYPE = 'io.cozy.files';
4
- /**
5
- * Creates a directory from a given path
6
- * @param {Object} client CozyClient
7
- * @param {string} path Directory path
8
- * @return {Object} Directory attributes
9
- */
10
-
11
- export var createDirectoryByPath = /*#__PURE__*/function () {
12
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(client, path) {
13
- var _yield$client$collect, data;
14
-
15
- return _regeneratorRuntime.wrap(function _callee$(_context) {
16
- while (1) {
17
- switch (_context.prev = _context.next) {
18
- case 0:
19
- _context.next = 2;
20
- return client.collection(FILES_DOCTYPE).createDirectoryByPath(path);
21
-
22
- case 2:
23
- _yield$client$collect = _context.sent;
24
- data = _yield$client$collect.data;
25
- return _context.abrupt("return", data);
26
-
27
- case 5:
28
- case "end":
29
- return _context.stop();
30
- }
31
- }
32
- }, _callee);
33
- }));
34
-
35
- return function createDirectoryByPath(_x, _x2) {
36
- return _ref.apply(this, arguments);
37
- };
38
- }();
39
- /**
40
- * Retrieves a directory from its path
41
- * @param {Object} client CozyClient
42
- * @param {string} path Directory path
43
- * @return {Object} Created io.cozy.files document
44
- */
45
-
46
- export var statDirectoryByPath = /*#__PURE__*/function () {
47
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(client, path) {
48
- var response;
49
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
50
- while (1) {
51
- switch (_context2.prev = _context2.next) {
52
- case 0:
53
- _context2.prev = 0;
54
- _context2.next = 3;
55
- return client.collection(FILES_DOCTYPE).statByPath(path);
56
-
57
- case 3:
58
- response = _context2.sent;
59
- return _context2.abrupt("return", response.data);
60
-
61
- case 7:
62
- _context2.prev = 7;
63
- _context2.t0 = _context2["catch"](0);
64
-
65
- if (!(_context2.t0 && _context2.t0.status === 404)) {
66
- _context2.next = 11;
67
- break;
68
- }
69
-
70
- return _context2.abrupt("return", null);
71
-
72
- case 11:
73
- throw new Error(_context2.t0.message);
74
-
75
- case 12:
76
- case "end":
77
- return _context2.stop();
78
- }
79
- }
80
- }, _callee2, null, [[0, 7]]);
81
- }));
82
-
83
- return function statDirectoryByPath(_x3, _x4) {
84
- return _ref2.apply(this, arguments);
85
- };
86
- }();
@@ -1,141 +0,0 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
- import _regeneratorRuntime from "@babel/runtime/regenerator";
3
-
4
- /* eslint-env jest */
5
- import { createDirectoryByPath, statDirectoryByPath } from 'connections/files';
6
- import client from 'cozy-client';
7
- jest.mock('cozy-client', function () {
8
- return {
9
- collection: jest.fn().mockReturnValue({
10
- addReferencesTo: jest.fn(),
11
- createDirectoryByPath: jest.fn(),
12
- statByPath: jest.fn()
13
- })
14
- };
15
- });
16
- var fixtures = {
17
- path: '/Administrative/KonnectTest/84595fcbc15242f2a69ac483b37ae999',
18
- directory: {
19
- _id: '326267e55ff0511c7f7b9ba56e04b334',
20
- _rev: '2-510e5a2b3c84aba208c480a91fd39abf',
21
- type: 'directory',
22
- name: '84595fcbc15242f2a69ac483b37ae999',
23
- dir_id: '326267e55ff0511c7f7b9ba56e00ca07',
24
- created_at: '2018-12-18T11:22:41.065070118+01:00',
25
- updated_at: '2018-12-18T11:22:41.065070118+01:00',
26
- tags: [],
27
- path: '/Administrative/KonnectTest/84595fcbc15242f2a69ac483b37ae999'
28
- },
29
- references: {
30
- data: [{
31
- type: 'io.cozy.files',
32
- id: '326267e55ff0511c7f7b9ba56e04b334'
33
- }]
34
- }
35
- };
36
- describe('Files mutations', function () {
37
- beforeAll(function () {
38
- client.collection().addReferencesTo.mockResolvedValue({
39
- data: fixtures.references
40
- });
41
- client.collection().createDirectoryByPath.mockResolvedValue({
42
- data: fixtures.directory
43
- });
44
- client.collection().statByPath.mockResolvedValue({
45
- data: fixtures.directory
46
- });
47
- });
48
- afterEach(function () {
49
- jest.clearAllMocks();
50
- });
51
- afterAll(function () {
52
- jest.restoreAllMocks();
53
- });
54
- describe('createDirectoryByPath', function () {
55
- it('calls Cozy Client and return io.cozy.files document', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
56
- var directory;
57
- return _regeneratorRuntime.wrap(function _callee$(_context) {
58
- while (1) {
59
- switch (_context.prev = _context.next) {
60
- case 0:
61
- _context.next = 2;
62
- return createDirectoryByPath(client, fixtures.path);
63
-
64
- case 2:
65
- directory = _context.sent;
66
- expect(client.collection().createDirectoryByPath).toHaveBeenCalledWith(fixtures.path);
67
- expect(directory).toEqual(fixtures.directory);
68
-
69
- case 5:
70
- case "end":
71
- return _context.stop();
72
- }
73
- }
74
- }, _callee);
75
- })));
76
- });
77
- describe('statDirectoryByPath', function () {
78
- it('calls Cozy Client and return io.cozy.files data', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
79
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
80
- while (1) {
81
- switch (_context2.prev = _context2.next) {
82
- case 0:
83
- _context2.t0 = expect;
84
- _context2.next = 3;
85
- return statDirectoryByPath(client, fixtures.path);
86
-
87
- case 3:
88
- _context2.t1 = _context2.sent;
89
- (0, _context2.t0)(_context2.t1).toEqual(fixtures.directory);
90
-
91
- case 5:
92
- case "end":
93
- return _context2.stop();
94
- }
95
- }
96
- }, _callee2);
97
- })));
98
- it('returns null if Cozy Client returns a 404 error', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
99
- return _regeneratorRuntime.wrap(function _callee3$(_context3) {
100
- while (1) {
101
- switch (_context3.prev = _context3.next) {
102
- case 0:
103
- client.collection().statByPath.mockRejectedValue({
104
- status: 404
105
- });
106
- _context3.t0 = expect;
107
- _context3.next = 4;
108
- return statDirectoryByPath(client, fixtures.path);
109
-
110
- case 4:
111
- _context3.t1 = _context3.sent;
112
- (0, _context3.t0)(_context3.t1).toBeNull();
113
-
114
- case 6:
115
- case "end":
116
- return _context3.stop();
117
- }
118
- }
119
- }, _callee3);
120
- })));
121
- it('throw error if Cozy Client returns any other error', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
122
- return _regeneratorRuntime.wrap(function _callee4$(_context4) {
123
- while (1) {
124
- switch (_context4.prev = _context4.next) {
125
- case 0:
126
- client.collection().statByPath.mockRejectedValue({
127
- status: 403,
128
- message: 'Test error'
129
- });
130
- _context4.next = 3;
131
- return expect(statDirectoryByPath(client, fixtures.path)).rejects.toThrow('Test error');
132
-
133
- case 3:
134
- case "end":
135
- return _context4.stop();
136
- }
137
- }
138
- }, _callee4);
139
- })));
140
- });
141
- });
@@ -1,30 +0,0 @@
1
- const FILES_DOCTYPE = 'io.cozy.files'
2
-
3
- /**
4
- * Creates a directory from a given path
5
- * @param {Object} client CozyClient
6
- * @param {string} path Directory path
7
- * @return {Object} Directory attributes
8
- */
9
- export const createDirectoryByPath = async (client, path) => {
10
- const { data } = await client
11
- .collection(FILES_DOCTYPE)
12
- .createDirectoryByPath(path)
13
- return data
14
- }
15
-
16
- /**
17
- * Retrieves a directory from its path
18
- * @param {Object} client CozyClient
19
- * @param {string} path Directory path
20
- * @return {Object} Created io.cozy.files document
21
- */
22
- export const statDirectoryByPath = async (client, path) => {
23
- try {
24
- const response = await client.collection(FILES_DOCTYPE).statByPath(path)
25
- return response.data
26
- } catch (error) {
27
- if (error && error.status === 404) return null
28
- throw new Error(error.message)
29
- }
30
- }
@@ -1,94 +0,0 @@
1
- /* eslint-env jest */
2
-
3
- import { createDirectoryByPath, statDirectoryByPath } from 'connections/files'
4
-
5
- import client from 'cozy-client'
6
-
7
- jest.mock('cozy-client', () => ({
8
- collection: jest.fn().mockReturnValue({
9
- addReferencesTo: jest.fn(),
10
- createDirectoryByPath: jest.fn(),
11
- statByPath: jest.fn()
12
- })
13
- }))
14
-
15
- const fixtures = {
16
- path: '/Administrative/KonnectTest/84595fcbc15242f2a69ac483b37ae999',
17
- directory: {
18
- _id: '326267e55ff0511c7f7b9ba56e04b334',
19
- _rev: '2-510e5a2b3c84aba208c480a91fd39abf',
20
- type: 'directory',
21
- name: '84595fcbc15242f2a69ac483b37ae999',
22
- dir_id: '326267e55ff0511c7f7b9ba56e00ca07',
23
- created_at: '2018-12-18T11:22:41.065070118+01:00',
24
- updated_at: '2018-12-18T11:22:41.065070118+01:00',
25
- tags: [],
26
- path: '/Administrative/KonnectTest/84595fcbc15242f2a69ac483b37ae999'
27
- },
28
- references: {
29
- data: [
30
- {
31
- type: 'io.cozy.files',
32
- id: '326267e55ff0511c7f7b9ba56e04b334'
33
- }
34
- ]
35
- }
36
- }
37
-
38
- describe('Files mutations', () => {
39
- beforeAll(() => {
40
- client
41
- .collection()
42
- .addReferencesTo.mockResolvedValue({ data: fixtures.references })
43
-
44
- client
45
- .collection()
46
- .createDirectoryByPath.mockResolvedValue({ data: fixtures.directory })
47
-
48
- client
49
- .collection()
50
- .statByPath.mockResolvedValue({ data: fixtures.directory })
51
- })
52
-
53
- afterEach(() => {
54
- jest.clearAllMocks()
55
- })
56
-
57
- afterAll(() => {
58
- jest.restoreAllMocks()
59
- })
60
-
61
- describe('createDirectoryByPath', () => {
62
- it('calls Cozy Client and return io.cozy.files document', async () => {
63
- const directory = await createDirectoryByPath(client, fixtures.path)
64
-
65
- expect(client.collection().createDirectoryByPath).toHaveBeenCalledWith(
66
- fixtures.path
67
- )
68
- expect(directory).toEqual(fixtures.directory)
69
- })
70
- })
71
-
72
- describe('statDirectoryByPath', () => {
73
- it('calls Cozy Client and return io.cozy.files data', async () => {
74
- expect(await statDirectoryByPath(client, fixtures.path)).toEqual(
75
- fixtures.directory
76
- )
77
- })
78
-
79
- it('returns null if Cozy Client returns a 404 error', async () => {
80
- client.collection().statByPath.mockRejectedValue({ status: 404 })
81
- expect(await statDirectoryByPath(client, fixtures.path)).toBeNull()
82
- })
83
-
84
- it('throw error if Cozy Client returns any other error', async () => {
85
- client
86
- .collection()
87
- .statByPath.mockRejectedValue({ status: 403, message: 'Test error' })
88
-
89
- await expect(statDirectoryByPath(client, fixtures.path)).rejects.toThrow(
90
- 'Test error'
91
- )
92
- })
93
- })
94
- })