retryableImport.spec.jsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import retryableImport from 'app/utils/retryableImport';
  2. describe('retryableImport', function() {
  3. it('can dynamically import successfully on first try', async function() {
  4. const importMock = jest.fn();
  5. importMock.mockReturnValue(
  6. new Promise(resolve =>
  7. resolve({
  8. default: {
  9. foo: 'bar',
  10. },
  11. })
  12. )
  13. );
  14. const result = await retryableImport(() => importMock());
  15. expect(result).toEqual({
  16. foo: 'bar',
  17. });
  18. expect(importMock).toHaveBeenCalledTimes(1);
  19. });
  20. it('can fail 2 dynamic imports and succeed on 3rd try', async function() {
  21. const importMock = jest.fn();
  22. importMock
  23. .mockReturnValueOnce(
  24. new Promise((resolve, reject) => reject(new Error('Unable to import')))
  25. )
  26. .mockReturnValueOnce(
  27. new Promise((resolve, reject) => reject(new Error('Unable to import')))
  28. )
  29. .mockReturnValue(
  30. new Promise(resolve =>
  31. resolve({
  32. default: {
  33. foo: 'bar',
  34. },
  35. })
  36. )
  37. );
  38. const result = await retryableImport(() => importMock());
  39. expect(result).toEqual({
  40. foo: 'bar',
  41. });
  42. expect(importMock).toHaveBeenCalledTimes(3);
  43. });
  44. it('only retries 3 times', async function() {
  45. const importMock = jest.fn(
  46. () => new Promise((resolve, reject) => reject(new Error('Unable to import')))
  47. );
  48. await expect(retryableImport(() => importMock())).rejects.toThrow('Unable to import');
  49. expect(importMock).toHaveBeenCalledTimes(3);
  50. });
  51. });