@contentful/field-editor-slug 1.3.0 → 1.3.1

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.
@@ -135,12 +135,13 @@ describe('SlugEditor', ()=>{
135
135
  baseSdk: sdk,
136
136
  isInitiallyDisabled: false
137
137
  }));
138
- await (0, _react1.wait)();
139
- expect(field.setValue).not.toHaveBeenCalled();
140
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
141
- expect(sdk.space.getEntries).not.toHaveBeenCalled();
142
- expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
143
- expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
138
+ await (0, _react1.waitFor)(()=>{
139
+ expect(field.setValue).not.toHaveBeenCalled();
140
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
141
+ expect(sdk.space.getEntries).not.toHaveBeenCalled();
142
+ expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
143
+ expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
144
+ });
144
145
  });
145
146
  it('when title and slug are the same field', async ()=>{
146
147
  const { field , titleField , sdk } = createMocks();
@@ -150,9 +151,10 @@ describe('SlugEditor', ()=>{
150
151
  baseSdk: sdk,
151
152
  isInitiallyDisabled: false
152
153
  }));
153
- await (0, _react1.wait)();
154
- expect(titleField.onValueChanged).not.toHaveBeenCalled();
155
- expect(field.setValue).not.toHaveBeenCalled();
154
+ await (0, _react1.waitFor)(()=>{
155
+ expect(titleField.onValueChanged).not.toHaveBeenCalled();
156
+ expect(field.setValue).not.toHaveBeenCalled();
157
+ });
156
158
  });
157
159
  it('when a saved slug is different from a title at the render', async ()=>{
158
160
  const { field , titleField , sdk } = createMocks({
@@ -164,9 +166,10 @@ describe('SlugEditor', ()=>{
164
166
  baseSdk: sdk,
165
167
  isInitiallyDisabled: false
166
168
  }));
167
- await (0, _react1.wait)();
168
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
169
- expect(field.setValue).not.toHaveBeenCalled();
169
+ await (0, _react1.waitFor)(()=>{
170
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
171
+ expect(field.setValue).not.toHaveBeenCalled();
172
+ });
170
173
  });
171
174
  });
172
175
  describe('should check for uniqueness', ()=>{
@@ -192,18 +195,19 @@ describe('SlugEditor', ()=>{
192
195
  baseSdk: sdk,
193
196
  isInitiallyDisabled: false
194
197
  }));
195
- await (0, _react1.wait)();
196
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
197
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
198
- content_type: 'content-type-id',
199
- 'fields.slug-id.en-US': 'slug-value',
200
- limit: 0,
201
- 'sys.id[ne]': 'entry-id',
202
- 'sys.publishedAt[exists]': true
203
- });
204
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
205
- expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
206
- expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
198
+ await (0, _react1.waitFor)(()=>{
199
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
200
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
201
+ content_type: 'content-type-id',
202
+ 'fields.slug-id.en-US': 'slug-value',
203
+ limit: 0,
204
+ 'sys.id[ne]': 'entry-id',
205
+ 'sys.publishedAt[exists]': true
206
+ });
207
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
208
+ expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
209
+ expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
210
+ });
207
211
  });
208
212
  it('if it is not published', async ()=>{
209
213
  const { field , titleField , sdk } = createMocks({
@@ -227,19 +231,20 @@ describe('SlugEditor', ()=>{
227
231
  baseSdk: sdk,
228
232
  isInitiallyDisabled: false
229
233
  }));
230
- await (0, _react1.wait)();
231
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
232
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
233
- content_type: 'content-type-id',
234
- 'fields.slug-id.en-US': 'slug-value',
235
- limit: 0,
236
- 'sys.id[ne]': 'entry-id',
237
- 'sys.publishedAt[exists]': true
238
- });
239
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
240
- expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
241
- expect(queryByText('This slug has already been published in another entry')).toBeInTheDocument();
242
- expect(getByTestId('cf-ui-text-input')).toHaveValue('slug-value');
234
+ await (0, _react1.waitFor)(()=>{
235
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
236
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
237
+ content_type: 'content-type-id',
238
+ 'fields.slug-id.en-US': 'slug-value',
239
+ limit: 0,
240
+ 'sys.id[ne]': 'entry-id',
241
+ 'sys.publishedAt[exists]': true
242
+ });
243
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
244
+ expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
245
+ expect(queryByText('This slug has already been published in another entry')).toBeInTheDocument();
246
+ expect(getByTestId('cf-ui-text-input')).toHaveValue('slug-value');
247
+ });
243
248
  sdk.space.getEntries.mockResolvedValue({
244
249
  total: 0
245
250
  });
@@ -248,18 +253,19 @@ describe('SlugEditor', ()=>{
248
253
  value: '123'
249
254
  }
250
255
  });
251
- await (0, _react1.wait)();
252
- expect(field.setValue).toHaveBeenCalledTimes(1);
253
- expect(field.setValue).toHaveBeenCalledWith('123');
254
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
255
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
256
- content_type: 'content-type-id',
257
- 'fields.slug-id.en-US': '123',
258
- limit: 0,
259
- 'sys.id[ne]': 'entry-id',
260
- 'sys.publishedAt[exists]': true
261
- });
262
- expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
256
+ await (0, _react1.waitFor)(()=>{
257
+ expect(field.setValue).toHaveBeenCalledTimes(1);
258
+ expect(field.setValue).toHaveBeenCalledWith('123');
259
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
260
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
261
+ content_type: 'content-type-id',
262
+ 'fields.slug-id.en-US': '123',
263
+ limit: 0,
264
+ 'sys.id[ne]': 'entry-id',
265
+ 'sys.publishedAt[exists]': true
266
+ });
267
+ expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
268
+ });
263
269
  });
264
270
  });
265
271
  describe('should react to title changes', ()=>{
@@ -273,12 +279,13 @@ describe('SlugEditor', ()=>{
273
279
  baseSdk: sdk,
274
280
  isInitiallyDisabled: true
275
281
  }));
276
- await (0, _react1.wait)();
277
- expect(field.setValue).toHaveBeenCalled();
278
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
279
- expect(sdk.space.getEntries).toHaveBeenCalled();
280
- expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
281
- expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
282
+ await (0, _react1.waitFor)(()=>{
283
+ expect(field.setValue).toHaveBeenCalled();
284
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
285
+ expect(sdk.space.getEntries).toHaveBeenCalled();
286
+ expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
287
+ expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
288
+ });
282
289
  });
283
290
  it('should generate unique value with date if title is empty', async ()=>{
284
291
  const { field , titleField , sdk } = createMocks({
@@ -290,20 +297,23 @@ describe('SlugEditor', ()=>{
290
297
  baseSdk: sdk,
291
298
  isInitiallyDisabled: false
292
299
  }));
293
- await (0, _react1.wait)();
294
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
295
- expect(field.setValue).toHaveBeenCalledTimes(1);
296
- expect(field.setValue).toHaveBeenLastCalledWith('untitled-entry-2020-01-24-at-15-33-47');
300
+ await (0, _react1.waitFor)(()=>{
301
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
302
+ expect(field.setValue).toHaveBeenCalledTimes(1);
303
+ expect(field.setValue).toHaveBeenLastCalledWith('untitled-entry-2020-01-24-at-15-33-47');
304
+ });
297
305
  await sdk.entry.fields['title-id'].setValue('Hello world!');
298
- await (0, _react1.wait)();
299
- expect(field.setValue).toHaveBeenCalledTimes(2);
300
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
301
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
306
+ await (0, _react1.waitFor)(()=>{
307
+ expect(field.setValue).toHaveBeenCalledTimes(2);
308
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
309
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
310
+ });
302
311
  await sdk.entry.fields['title-id'].setValue('фраза написанная по русски');
303
- await (0, _react1.wait)();
304
- expect(field.setValue).toHaveBeenCalledTimes(3);
305
- expect(field.setValue).toHaveBeenLastCalledWith('fraza-napisannaya-po-russki');
306
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(3);
312
+ await (0, _react1.waitFor)(()=>{
313
+ expect(field.setValue).toHaveBeenCalledTimes(3);
314
+ expect(field.setValue).toHaveBeenLastCalledWith('fraza-napisannaya-po-russki');
315
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(3);
316
+ });
307
317
  });
308
318
  it('should generate value from title if it is not empty', async ()=>{
309
319
  const { field , titleField , sdk } = createMocks({
@@ -315,15 +325,17 @@ describe('SlugEditor', ()=>{
315
325
  baseSdk: sdk,
316
326
  isInitiallyDisabled: false
317
327
  }));
318
- await (0, _react1.wait)();
319
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
320
- expect(field.setValue).toHaveBeenCalledTimes(1);
321
- expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-title-value');
328
+ await (0, _react1.waitFor)(()=>{
329
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
330
+ expect(field.setValue).toHaveBeenCalledTimes(1);
331
+ expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-title-value');
332
+ });
322
333
  await sdk.entry.fields['title-id'].setValue('Hello world!');
323
- await (0, _react1.wait)();
324
- expect(field.setValue).toHaveBeenCalledTimes(2);
325
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
326
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
334
+ await (0, _react1.waitFor)(()=>{
335
+ expect(field.setValue).toHaveBeenCalledTimes(2);
336
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
337
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
338
+ });
327
339
  });
328
340
  it('should stop tracking value after user intentionally changes slug value', async ()=>{
329
341
  const { field , titleField , sdk } = createMocks({
@@ -335,28 +347,33 @@ describe('SlugEditor', ()=>{
335
347
  baseSdk: sdk,
336
348
  isInitiallyDisabled: false
337
349
  }));
338
- await (0, _react1.wait)();
339
- await sdk.entry.fields['title-id'].setValue('Hello world!');
340
- await (0, _react1.wait)();
341
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
342
- expect(field.setValue).toHaveBeenCalledTimes(2);
343
- expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
344
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
345
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
350
+ await (0, _react1.waitFor)(async ()=>{
351
+ await sdk.entry.fields['title-id'].setValue('Hello world!');
352
+ });
353
+ await (0, _react1.waitFor)(()=>{
354
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
355
+ expect(field.setValue).toHaveBeenCalledTimes(2);
356
+ expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
357
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
358
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
359
+ });
346
360
  _react1.fireEvent.change(getByTestId('cf-ui-text-input'), {
347
361
  target: {
348
362
  value: 'new-custom-slug'
349
363
  }
350
364
  });
351
- await (0, _react1.wait)();
352
- expect(field.setValue).toHaveBeenCalledTimes(3);
353
- expect(field.setValue).toHaveBeenLastCalledWith('new-custom-slug');
365
+ await (0, _react1.waitFor)(()=>{
366
+ expect(field.setValue).toHaveBeenCalledTimes(3);
367
+ expect(field.setValue).toHaveBeenLastCalledWith('new-custom-slug');
368
+ });
354
369
  await sdk.entry.fields['title-id'].setValue('I decided to update my title');
355
- await (0, _react1.wait)();
356
- expect(field.setValue).toHaveBeenCalledTimes(3);
370
+ await (0, _react1.waitFor)(()=>{
371
+ expect(field.setValue).toHaveBeenCalledTimes(3);
372
+ });
357
373
  await sdk.entry.fields['title-id'].setValue('I decided to update my title again');
358
- await (0, _react1.wait)();
359
- expect(field.setValue).toHaveBeenCalledTimes(3);
374
+ await (0, _react1.waitFor)(()=>{
375
+ expect(field.setValue).toHaveBeenCalledTimes(3);
376
+ });
360
377
  });
361
378
  it('should start tracking again after potential slug equals real one', async ()=>{
362
379
  const { field , sdk } = createMocks({
@@ -368,26 +385,31 @@ describe('SlugEditor', ()=>{
368
385
  baseSdk: sdk,
369
386
  isInitiallyDisabled: false
370
387
  }));
371
- await (0, _react1.wait)();
372
- await sdk.entry.fields['title-id'].setValue('ABC DEF');
373
- await (0, _react1.wait)();
374
- expect(field.setValue).toHaveBeenLastCalledWith('abc-def');
375
- expect(field.setValue).toHaveBeenCalledTimes(2);
388
+ await (0, _react1.waitFor)(async ()=>{
389
+ await sdk.entry.fields['title-id'].setValue('ABC DEF');
390
+ });
391
+ await (0, _react1.waitFor)(()=>{
392
+ expect(field.setValue).toHaveBeenLastCalledWith('abc-def');
393
+ expect(field.setValue).toHaveBeenCalledTimes(2);
394
+ });
376
395
  _react1.fireEvent.change(getByTestId('cf-ui-text-input'), {
377
396
  target: {
378
397
  value: 'abc'
379
398
  }
380
399
  });
381
400
  await sdk.entry.fields['title-id'].setValue('ABC D');
382
- await (0, _react1.wait)();
383
- expect(field.setValue).toHaveBeenLastCalledWith('abc');
384
- expect(field.setValue).toHaveBeenCalledTimes(3);
385
- await sdk.entry.fields['title-id'].setValue('ABC');
386
- await sdk.entry.fields['title-id'].setValue('ABC ABC');
387
- await (0, _react1.wait)();
388
- expect(field.setValue).toHaveBeenLastCalledWith('abc-abc');
389
- expect(field.setValue).toHaveBeenCalledTimes(4);
390
- await (0, _react1.wait)();
401
+ await (0, _react1.waitFor)(()=>{
402
+ expect(field.setValue).toHaveBeenLastCalledWith('abc');
403
+ expect(field.setValue).toHaveBeenCalledTimes(3);
404
+ });
405
+ await (0, _react1.waitFor)(async ()=>{
406
+ await sdk.entry.fields['title-id'].setValue('ABC');
407
+ await sdk.entry.fields['title-id'].setValue('ABC ABC');
408
+ });
409
+ await (0, _react1.waitFor)(()=>{
410
+ expect(field.setValue).toHaveBeenLastCalledWith('abc-abc');
411
+ expect(field.setValue).toHaveBeenCalledTimes(4);
412
+ });
391
413
  });
392
414
  });
393
415
  describe('for non default locales', ()=>{
@@ -413,10 +435,11 @@ describe('SlugEditor', ()=>{
413
435
  baseSdk: sdk,
414
436
  isInitiallyDisabled: false
415
437
  }));
416
- await (0, _react1.wait)();
417
- expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
418
- expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
419
- expect(titleField.onValueChanged).toHaveBeenCalledWith('de-DE', expect.any(Function));
438
+ await (0, _react1.waitFor)(()=>{
439
+ expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
440
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
441
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('de-DE', expect.any(Function));
442
+ });
420
443
  });
421
444
  it('locale is optional and has a fallback then it should track only current locale changes', async ()=>{
422
445
  const { sdk , field , titleField } = createMocks();
@@ -440,10 +463,11 @@ describe('SlugEditor', ()=>{
440
463
  baseSdk: sdk,
441
464
  isInitiallyDisabled: false
442
465
  }));
443
- await (0, _react1.wait)();
444
- expect(field.setValue).not.toHaveBeenCalled();
445
- expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
446
- expect(titleField.onValueChanged).not.toHaveBeenCalledWith('de-DE', expect.any(Function));
466
+ await (0, _react1.waitFor)(()=>{
467
+ expect(field.setValue).not.toHaveBeenCalled();
468
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
469
+ expect(titleField.onValueChanged).not.toHaveBeenCalledWith('de-DE', expect.any(Function));
470
+ });
447
471
  });
448
472
  });
449
473
  it('slug suggestion is limited to 75 symbols', async ()=>{
@@ -456,11 +480,13 @@ describe('SlugEditor', ()=>{
456
480
  baseSdk: sdk,
457
481
  isInitiallyDisabled: false
458
482
  }));
459
- await (0, _react1.wait)();
460
- await sdk.entry.fields['title-id'].setValue('a'.repeat(80));
461
- await (0, _react1.wait)();
462
- const expectedSlug = 'a'.repeat(75);
463
- expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
483
+ await (0, _react1.waitFor)(async ()=>{
484
+ await sdk.entry.fields['title-id'].setValue('a'.repeat(80));
485
+ });
486
+ await (0, _react1.waitFor)(()=>{
487
+ const expectedSlug = 'a'.repeat(75);
488
+ expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
489
+ });
464
490
  });
465
491
  it('slug suggestion does not contain cut-off words', async ()=>{
466
492
  const { field , sdk } = createMocks({
@@ -472,11 +498,13 @@ describe('SlugEditor', ()=>{
472
498
  baseSdk: sdk,
473
499
  isInitiallyDisabled: false
474
500
  }));
475
- await (0, _react1.wait)();
476
- await sdk.entry.fields['title-id'].setValue(`one two three ${'a'.repeat(80)}`);
477
- await (0, _react1.wait)();
478
- const expectedSlug = 'one-two-three';
479
- expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
501
+ await (0, _react1.waitFor)(async ()=>{
502
+ await sdk.entry.fields['title-id'].setValue(`one two three ${'a'.repeat(80)}`);
503
+ });
504
+ await (0, _react1.waitFor)(()=>{
505
+ const expectedSlug = 'one-two-three';
506
+ expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
507
+ });
480
508
  });
481
509
  it('should subscribe for changes in custom field id', async ()=>{
482
510
  const { field , titleField , descriptionField , sdk } = createMocks({
@@ -494,15 +522,17 @@ describe('SlugEditor', ()=>{
494
522
  }
495
523
  }
496
524
  }));
497
- await (0, _react1.wait)();
498
- expect(titleField.onValueChanged).not.toHaveBeenCalled();
499
- expect(descriptionField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
500
- expect(field.setValue).toHaveBeenCalledTimes(1);
501
- expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-description-value');
525
+ await (0, _react1.waitFor)(()=>{
526
+ expect(titleField.onValueChanged).not.toHaveBeenCalled();
527
+ expect(descriptionField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
528
+ expect(field.setValue).toHaveBeenCalledTimes(1);
529
+ expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-description-value');
530
+ });
502
531
  await sdk.entry.fields['description-id'].setValue('Hello world!');
503
- await (0, _react1.wait)();
504
- expect(field.setValue).toHaveBeenCalledTimes(2);
505
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
506
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
532
+ await (0, _react1.waitFor)(()=>{
533
+ expect(field.setValue).toHaveBeenCalledTimes(2);
534
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
535
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
536
+ });
507
537
  });
508
538
  });
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { createFakeFieldAPI, createFakeLocalesAPI } from '@contentful/field-editor-test-utils';
3
- import { render, configure, cleanup, wait, fireEvent } from '@testing-library/react';
3
+ import { cleanup, configure, fireEvent, render, waitFor } from '@testing-library/react';
4
4
  import identity from 'lodash/identity';
5
5
  import '@testing-library/jest-dom/extend-expect';
6
6
  import { SlugEditor } from './SlugEditor';
@@ -87,12 +87,13 @@ describe('SlugEditor', ()=>{
87
87
  baseSdk: sdk,
88
88
  isInitiallyDisabled: false
89
89
  }));
90
- await wait();
91
- expect(field.setValue).not.toHaveBeenCalled();
92
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
93
- expect(sdk.space.getEntries).not.toHaveBeenCalled();
94
- expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
95
- expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
90
+ await waitFor(()=>{
91
+ expect(field.setValue).not.toHaveBeenCalled();
92
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
93
+ expect(sdk.space.getEntries).not.toHaveBeenCalled();
94
+ expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
95
+ expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
96
+ });
96
97
  });
97
98
  it('when title and slug are the same field', async ()=>{
98
99
  const { field , titleField , sdk } = createMocks();
@@ -102,9 +103,10 @@ describe('SlugEditor', ()=>{
102
103
  baseSdk: sdk,
103
104
  isInitiallyDisabled: false
104
105
  }));
105
- await wait();
106
- expect(titleField.onValueChanged).not.toHaveBeenCalled();
107
- expect(field.setValue).not.toHaveBeenCalled();
106
+ await waitFor(()=>{
107
+ expect(titleField.onValueChanged).not.toHaveBeenCalled();
108
+ expect(field.setValue).not.toHaveBeenCalled();
109
+ });
108
110
  });
109
111
  it('when a saved slug is different from a title at the render', async ()=>{
110
112
  const { field , titleField , sdk } = createMocks({
@@ -116,9 +118,10 @@ describe('SlugEditor', ()=>{
116
118
  baseSdk: sdk,
117
119
  isInitiallyDisabled: false
118
120
  }));
119
- await wait();
120
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
121
- expect(field.setValue).not.toHaveBeenCalled();
121
+ await waitFor(()=>{
122
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
123
+ expect(field.setValue).not.toHaveBeenCalled();
124
+ });
122
125
  });
123
126
  });
124
127
  describe('should check for uniqueness', ()=>{
@@ -144,18 +147,19 @@ describe('SlugEditor', ()=>{
144
147
  baseSdk: sdk,
145
148
  isInitiallyDisabled: false
146
149
  }));
147
- await wait();
148
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
149
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
150
- content_type: 'content-type-id',
151
- 'fields.slug-id.en-US': 'slug-value',
152
- limit: 0,
153
- 'sys.id[ne]': 'entry-id',
154
- 'sys.publishedAt[exists]': true
155
- });
156
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
157
- expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
158
- expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
150
+ await waitFor(()=>{
151
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
152
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
153
+ content_type: 'content-type-id',
154
+ 'fields.slug-id.en-US': 'slug-value',
155
+ limit: 0,
156
+ 'sys.id[ne]': 'entry-id',
157
+ 'sys.publishedAt[exists]': true
158
+ });
159
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
160
+ expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
161
+ expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
162
+ });
159
163
  });
160
164
  it('if it is not published', async ()=>{
161
165
  const { field , titleField , sdk } = createMocks({
@@ -179,19 +183,20 @@ describe('SlugEditor', ()=>{
179
183
  baseSdk: sdk,
180
184
  isInitiallyDisabled: false
181
185
  }));
182
- await wait();
183
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
184
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
185
- content_type: 'content-type-id',
186
- 'fields.slug-id.en-US': 'slug-value',
187
- limit: 0,
188
- 'sys.id[ne]': 'entry-id',
189
- 'sys.publishedAt[exists]': true
190
- });
191
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
192
- expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
193
- expect(queryByText('This slug has already been published in another entry')).toBeInTheDocument();
194
- expect(getByTestId('cf-ui-text-input')).toHaveValue('slug-value');
186
+ await waitFor(()=>{
187
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
188
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
189
+ content_type: 'content-type-id',
190
+ 'fields.slug-id.en-US': 'slug-value',
191
+ limit: 0,
192
+ 'sys.id[ne]': 'entry-id',
193
+ 'sys.publishedAt[exists]': true
194
+ });
195
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(1);
196
+ expect(queryByTestId('slug-editor-spinner')).not.toBeInTheDocument();
197
+ expect(queryByText('This slug has already been published in another entry')).toBeInTheDocument();
198
+ expect(getByTestId('cf-ui-text-input')).toHaveValue('slug-value');
199
+ });
195
200
  sdk.space.getEntries.mockResolvedValue({
196
201
  total: 0
197
202
  });
@@ -200,18 +205,19 @@ describe('SlugEditor', ()=>{
200
205
  value: '123'
201
206
  }
202
207
  });
203
- await wait();
204
- expect(field.setValue).toHaveBeenCalledTimes(1);
205
- expect(field.setValue).toHaveBeenCalledWith('123');
206
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
207
- expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
208
- content_type: 'content-type-id',
209
- 'fields.slug-id.en-US': '123',
210
- limit: 0,
211
- 'sys.id[ne]': 'entry-id',
212
- 'sys.publishedAt[exists]': true
213
- });
214
- expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
208
+ await waitFor(()=>{
209
+ expect(field.setValue).toHaveBeenCalledTimes(1);
210
+ expect(field.setValue).toHaveBeenCalledWith('123');
211
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
212
+ expect(sdk.space.getEntries).toHaveBeenLastCalledWith({
213
+ content_type: 'content-type-id',
214
+ 'fields.slug-id.en-US': '123',
215
+ limit: 0,
216
+ 'sys.id[ne]': 'entry-id',
217
+ 'sys.publishedAt[exists]': true
218
+ });
219
+ expect(queryByText('This slug has already been published in another entry')).not.toBeInTheDocument();
220
+ });
215
221
  });
216
222
  });
217
223
  describe('should react to title changes', ()=>{
@@ -225,12 +231,13 @@ describe('SlugEditor', ()=>{
225
231
  baseSdk: sdk,
226
232
  isInitiallyDisabled: true
227
233
  }));
228
- await wait();
229
- expect(field.setValue).toHaveBeenCalled();
230
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
231
- expect(sdk.space.getEntries).toHaveBeenCalled();
232
- expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
233
- expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
234
+ await waitFor(()=>{
235
+ expect(field.setValue).toHaveBeenCalled();
236
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
237
+ expect(sdk.space.getEntries).toHaveBeenCalled();
238
+ expect(sdk.entry.fields['title-id'].getValue).toHaveBeenCalledTimes(1);
239
+ expect(sdk.entry.getSys).toHaveBeenCalledTimes(2);
240
+ });
234
241
  });
235
242
  it('should generate unique value with date if title is empty', async ()=>{
236
243
  const { field , titleField , sdk } = createMocks({
@@ -242,20 +249,23 @@ describe('SlugEditor', ()=>{
242
249
  baseSdk: sdk,
243
250
  isInitiallyDisabled: false
244
251
  }));
245
- await wait();
246
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
247
- expect(field.setValue).toHaveBeenCalledTimes(1);
248
- expect(field.setValue).toHaveBeenLastCalledWith('untitled-entry-2020-01-24-at-15-33-47');
252
+ await waitFor(()=>{
253
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
254
+ expect(field.setValue).toHaveBeenCalledTimes(1);
255
+ expect(field.setValue).toHaveBeenLastCalledWith('untitled-entry-2020-01-24-at-15-33-47');
256
+ });
249
257
  await sdk.entry.fields['title-id'].setValue('Hello world!');
250
- await wait();
251
- expect(field.setValue).toHaveBeenCalledTimes(2);
252
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
253
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
258
+ await waitFor(()=>{
259
+ expect(field.setValue).toHaveBeenCalledTimes(2);
260
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
261
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
262
+ });
254
263
  await sdk.entry.fields['title-id'].setValue('фраза написанная по русски');
255
- await wait();
256
- expect(field.setValue).toHaveBeenCalledTimes(3);
257
- expect(field.setValue).toHaveBeenLastCalledWith('fraza-napisannaya-po-russki');
258
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(3);
264
+ await waitFor(()=>{
265
+ expect(field.setValue).toHaveBeenCalledTimes(3);
266
+ expect(field.setValue).toHaveBeenLastCalledWith('fraza-napisannaya-po-russki');
267
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(3);
268
+ });
259
269
  });
260
270
  it('should generate value from title if it is not empty', async ()=>{
261
271
  const { field , titleField , sdk } = createMocks({
@@ -267,15 +277,17 @@ describe('SlugEditor', ()=>{
267
277
  baseSdk: sdk,
268
278
  isInitiallyDisabled: false
269
279
  }));
270
- await wait();
271
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
272
- expect(field.setValue).toHaveBeenCalledTimes(1);
273
- expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-title-value');
280
+ await waitFor(()=>{
281
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
282
+ expect(field.setValue).toHaveBeenCalledTimes(1);
283
+ expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-title-value');
284
+ });
274
285
  await sdk.entry.fields['title-id'].setValue('Hello world!');
275
- await wait();
276
- expect(field.setValue).toHaveBeenCalledTimes(2);
277
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
278
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
286
+ await waitFor(()=>{
287
+ expect(field.setValue).toHaveBeenCalledTimes(2);
288
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
289
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
290
+ });
279
291
  });
280
292
  it('should stop tracking value after user intentionally changes slug value', async ()=>{
281
293
  const { field , titleField , sdk } = createMocks({
@@ -287,28 +299,33 @@ describe('SlugEditor', ()=>{
287
299
  baseSdk: sdk,
288
300
  isInitiallyDisabled: false
289
301
  }));
290
- await wait();
291
- await sdk.entry.fields['title-id'].setValue('Hello world!');
292
- await wait();
293
- expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
294
- expect(field.setValue).toHaveBeenCalledTimes(2);
295
- expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
296
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
297
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
302
+ await waitFor(async ()=>{
303
+ await sdk.entry.fields['title-id'].setValue('Hello world!');
304
+ });
305
+ await waitFor(()=>{
306
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
307
+ expect(field.setValue).toHaveBeenCalledTimes(2);
308
+ expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
309
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
310
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
311
+ });
298
312
  fireEvent.change(getByTestId('cf-ui-text-input'), {
299
313
  target: {
300
314
  value: 'new-custom-slug'
301
315
  }
302
316
  });
303
- await wait();
304
- expect(field.setValue).toHaveBeenCalledTimes(3);
305
- expect(field.setValue).toHaveBeenLastCalledWith('new-custom-slug');
317
+ await waitFor(()=>{
318
+ expect(field.setValue).toHaveBeenCalledTimes(3);
319
+ expect(field.setValue).toHaveBeenLastCalledWith('new-custom-slug');
320
+ });
306
321
  await sdk.entry.fields['title-id'].setValue('I decided to update my title');
307
- await wait();
308
- expect(field.setValue).toHaveBeenCalledTimes(3);
322
+ await waitFor(()=>{
323
+ expect(field.setValue).toHaveBeenCalledTimes(3);
324
+ });
309
325
  await sdk.entry.fields['title-id'].setValue('I decided to update my title again');
310
- await wait();
311
- expect(field.setValue).toHaveBeenCalledTimes(3);
326
+ await waitFor(()=>{
327
+ expect(field.setValue).toHaveBeenCalledTimes(3);
328
+ });
312
329
  });
313
330
  it('should start tracking again after potential slug equals real one', async ()=>{
314
331
  const { field , sdk } = createMocks({
@@ -320,26 +337,31 @@ describe('SlugEditor', ()=>{
320
337
  baseSdk: sdk,
321
338
  isInitiallyDisabled: false
322
339
  }));
323
- await wait();
324
- await sdk.entry.fields['title-id'].setValue('ABC DEF');
325
- await wait();
326
- expect(field.setValue).toHaveBeenLastCalledWith('abc-def');
327
- expect(field.setValue).toHaveBeenCalledTimes(2);
340
+ await waitFor(async ()=>{
341
+ await sdk.entry.fields['title-id'].setValue('ABC DEF');
342
+ });
343
+ await waitFor(()=>{
344
+ expect(field.setValue).toHaveBeenLastCalledWith('abc-def');
345
+ expect(field.setValue).toHaveBeenCalledTimes(2);
346
+ });
328
347
  fireEvent.change(getByTestId('cf-ui-text-input'), {
329
348
  target: {
330
349
  value: 'abc'
331
350
  }
332
351
  });
333
352
  await sdk.entry.fields['title-id'].setValue('ABC D');
334
- await wait();
335
- expect(field.setValue).toHaveBeenLastCalledWith('abc');
336
- expect(field.setValue).toHaveBeenCalledTimes(3);
337
- await sdk.entry.fields['title-id'].setValue('ABC');
338
- await sdk.entry.fields['title-id'].setValue('ABC ABC');
339
- await wait();
340
- expect(field.setValue).toHaveBeenLastCalledWith('abc-abc');
341
- expect(field.setValue).toHaveBeenCalledTimes(4);
342
- await wait();
353
+ await waitFor(()=>{
354
+ expect(field.setValue).toHaveBeenLastCalledWith('abc');
355
+ expect(field.setValue).toHaveBeenCalledTimes(3);
356
+ });
357
+ await waitFor(async ()=>{
358
+ await sdk.entry.fields['title-id'].setValue('ABC');
359
+ await sdk.entry.fields['title-id'].setValue('ABC ABC');
360
+ });
361
+ await waitFor(()=>{
362
+ expect(field.setValue).toHaveBeenLastCalledWith('abc-abc');
363
+ expect(field.setValue).toHaveBeenCalledTimes(4);
364
+ });
343
365
  });
344
366
  });
345
367
  describe('for non default locales', ()=>{
@@ -365,10 +387,11 @@ describe('SlugEditor', ()=>{
365
387
  baseSdk: sdk,
366
388
  isInitiallyDisabled: false
367
389
  }));
368
- await wait();
369
- expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
370
- expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
371
- expect(titleField.onValueChanged).toHaveBeenCalledWith('de-DE', expect.any(Function));
390
+ await waitFor(()=>{
391
+ expect(field.setValue).toHaveBeenCalledWith('untitled-entry-2020-01-24-at-15-33-47');
392
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
393
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('de-DE', expect.any(Function));
394
+ });
372
395
  });
373
396
  it('locale is optional and has a fallback then it should track only current locale changes', async ()=>{
374
397
  const { sdk , field , titleField } = createMocks();
@@ -392,10 +415,11 @@ describe('SlugEditor', ()=>{
392
415
  baseSdk: sdk,
393
416
  isInitiallyDisabled: false
394
417
  }));
395
- await wait();
396
- expect(field.setValue).not.toHaveBeenCalled();
397
- expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
398
- expect(titleField.onValueChanged).not.toHaveBeenCalledWith('de-DE', expect.any(Function));
418
+ await waitFor(()=>{
419
+ expect(field.setValue).not.toHaveBeenCalled();
420
+ expect(titleField.onValueChanged).toHaveBeenCalledWith('ru-RU', expect.any(Function));
421
+ expect(titleField.onValueChanged).not.toHaveBeenCalledWith('de-DE', expect.any(Function));
422
+ });
399
423
  });
400
424
  });
401
425
  it('slug suggestion is limited to 75 symbols', async ()=>{
@@ -408,11 +432,13 @@ describe('SlugEditor', ()=>{
408
432
  baseSdk: sdk,
409
433
  isInitiallyDisabled: false
410
434
  }));
411
- await wait();
412
- await sdk.entry.fields['title-id'].setValue('a'.repeat(80));
413
- await wait();
414
- const expectedSlug = 'a'.repeat(75);
415
- expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
435
+ await waitFor(async ()=>{
436
+ await sdk.entry.fields['title-id'].setValue('a'.repeat(80));
437
+ });
438
+ await waitFor(()=>{
439
+ const expectedSlug = 'a'.repeat(75);
440
+ expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
441
+ });
416
442
  });
417
443
  it('slug suggestion does not contain cut-off words', async ()=>{
418
444
  const { field , sdk } = createMocks({
@@ -424,11 +450,13 @@ describe('SlugEditor', ()=>{
424
450
  baseSdk: sdk,
425
451
  isInitiallyDisabled: false
426
452
  }));
427
- await wait();
428
- await sdk.entry.fields['title-id'].setValue(`one two three ${'a'.repeat(80)}`);
429
- await wait();
430
- const expectedSlug = 'one-two-three';
431
- expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
453
+ await waitFor(async ()=>{
454
+ await sdk.entry.fields['title-id'].setValue(`one two three ${'a'.repeat(80)}`);
455
+ });
456
+ await waitFor(()=>{
457
+ const expectedSlug = 'one-two-three';
458
+ expect(field.setValue).toHaveBeenLastCalledWith(expectedSlug);
459
+ });
432
460
  });
433
461
  it('should subscribe for changes in custom field id', async ()=>{
434
462
  const { field , titleField , descriptionField , sdk } = createMocks({
@@ -446,15 +474,17 @@ describe('SlugEditor', ()=>{
446
474
  }
447
475
  }
448
476
  }));
449
- await wait();
450
- expect(titleField.onValueChanged).not.toHaveBeenCalled();
451
- expect(descriptionField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
452
- expect(field.setValue).toHaveBeenCalledTimes(1);
453
- expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-description-value');
477
+ await waitFor(()=>{
478
+ expect(titleField.onValueChanged).not.toHaveBeenCalled();
479
+ expect(descriptionField.onValueChanged).toHaveBeenCalledWith('en-US', expect.any(Function));
480
+ expect(field.setValue).toHaveBeenCalledTimes(1);
481
+ expect(field.setValue).toHaveBeenLastCalledWith('this-is-initial-description-value');
482
+ });
454
483
  await sdk.entry.fields['description-id'].setValue('Hello world!');
455
- await wait();
456
- expect(field.setValue).toHaveBeenCalledTimes(2);
457
- expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
458
- expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
484
+ await waitFor(()=>{
485
+ expect(field.setValue).toHaveBeenCalledTimes(2);
486
+ expect(field.setValue).toHaveBeenLastCalledWith('hello-world');
487
+ expect(sdk.space.getEntries).toHaveBeenCalledTimes(2);
488
+ });
459
489
  });
460
490
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contentful/field-editor-slug",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -54,5 +54,5 @@
54
54
  "@contentful/app-sdk": "^4.2.0",
55
55
  "react": ">=16.8.0"
56
56
  },
57
- "gitHead": "543e02672a8dd4edc810f9f3568d6b69c454e1f9"
57
+ "gitHead": "83b651f1638ad6bab53565479e76b8d29b86af3a"
58
58
  }