|  | 
| 1 | 1 | /* eslint-disable @typescript-eslint/no-var-requires */ | 
| 2 | 2 | import { ICommonProps } from '../common'; | 
| 3 |  | -import { wrapWithRetries } from './utils'; | 
|  | 3 | +import { wrapWithRetries, wrapWithRetryUntilPass } from './utils'; | 
| 4 | 4 | 
 | 
| 5 | 5 | jest.mock('../common'); | 
| 6 | 6 | 
 | 
| @@ -111,4 +111,95 @@ describe('utils', () => { | 
| 111 | 111 |       expect(sleep).toHaveBeenCalledWith(500); // default pollEvery | 
| 112 | 112 |     }); | 
| 113 | 113 |   }); | 
|  | 114 | + | 
|  | 115 | +  describe('wrapWithRetryUntilPass', () => { | 
|  | 116 | +    beforeEach(() => { | 
|  | 117 | +      jest.clearAllMocks(); | 
|  | 118 | +    }); | 
|  | 119 | + | 
|  | 120 | +    test('should retry once on pass === true', async () => { | 
|  | 121 | +      const toWrap = jest.fn(); | 
|  | 122 | +      const expectedResult = { pass: true, message: () => '' }; | 
|  | 123 | +      toWrap.mockReturnValue(Promise.resolve(expectedResult)); | 
|  | 124 | + | 
|  | 125 | +      const matcherUtils = {} as jest.MatcherUtils; | 
|  | 126 | + | 
|  | 127 | +      const props = { region: 'region' } as ICommonProps; | 
|  | 128 | +      const key = 'key'; | 
|  | 129 | + | 
|  | 130 | +      const wrapped = wrapWithRetryUntilPass(toWrap); | 
|  | 131 | +      const result = await wrapped.bind(matcherUtils)(props, key); | 
|  | 132 | + | 
|  | 133 | +      expect(toWrap).toHaveBeenCalledTimes(1); | 
|  | 134 | +      expect(toWrap).toHaveBeenCalledWith(props, key); | 
|  | 135 | +      expect(result).toBe(expectedResult); | 
|  | 136 | +    }); | 
|  | 137 | + | 
|  | 138 | +    test('should exhaust timeout on pass === false', async () => { | 
|  | 139 | +      const { sleep } = require('../common'); | 
|  | 140 | + | 
|  | 141 | +      const mockedNow = jest.fn(); | 
|  | 142 | +      Date.now = mockedNow; | 
|  | 143 | +      mockedNow.mockReturnValueOnce(0); | 
|  | 144 | +      mockedNow.mockReturnValueOnce(250); | 
|  | 145 | +      mockedNow.mockReturnValueOnce(500); | 
|  | 146 | +      mockedNow.mockReturnValueOnce(750); | 
|  | 147 | +      mockedNow.mockReturnValueOnce(1000); | 
|  | 148 | +      mockedNow.mockReturnValueOnce(1250); | 
|  | 149 | + | 
|  | 150 | +      const toWrap = jest.fn(); | 
|  | 151 | +      const expectedResult = { pass: false, message: () => '' }; | 
|  | 152 | +      toWrap.mockReturnValue(Promise.resolve(expectedResult)); | 
|  | 153 | + | 
|  | 154 | +      const matcherUtils = {} as jest.MatcherUtils; | 
|  | 155 | + | 
|  | 156 | +      const props = { timeout: 1001, pollEvery: 250 } as ICommonProps; | 
|  | 157 | +      const key = 'key'; | 
|  | 158 | + | 
|  | 159 | +      const wrapped = wrapWithRetryUntilPass(toWrap); | 
|  | 160 | +      const result = await wrapped.bind(matcherUtils)(props, key); | 
|  | 161 | + | 
|  | 162 | +      expect(toWrap).toHaveBeenCalledTimes(5); | 
|  | 163 | +      expect(toWrap).toHaveBeenCalledWith(props, key); | 
|  | 164 | +      expect(result).toBe(expectedResult); | 
|  | 165 | +      expect(sleep).toHaveBeenCalledTimes(4); | 
|  | 166 | +      expect(sleep).toHaveBeenCalledWith(props.pollEvery); | 
|  | 167 | +    }); | 
|  | 168 | + | 
|  | 169 | +    test('should retry twice, { pass: false, isNot: false } => { pass: true, isNot: false }', async () => { | 
|  | 170 | +      const { sleep } = require('../common'); | 
|  | 171 | + | 
|  | 172 | +      const mockedNow = jest.fn(); | 
|  | 173 | +      Date.now = mockedNow; | 
|  | 174 | +      mockedNow.mockReturnValueOnce(0); | 
|  | 175 | +      mockedNow.mockReturnValueOnce(250); | 
|  | 176 | +      mockedNow.mockReturnValueOnce(500); | 
|  | 177 | + | 
|  | 178 | +      const toWrap = jest.fn(); | 
|  | 179 | +      // first attempt returns pass === false | 
|  | 180 | +      toWrap.mockReturnValueOnce( | 
|  | 181 | +        Promise.resolve({ pass: false, message: () => '' }), | 
|  | 182 | +      ); | 
|  | 183 | + | 
|  | 184 | +      // second attempt returns pass === true | 
|  | 185 | +      const expectedResult = { pass: true, message: () => '' }; | 
|  | 186 | +      toWrap.mockReturnValueOnce(Promise.resolve(expectedResult)); | 
|  | 187 | + | 
|  | 188 | +      const matcherUtils = { | 
|  | 189 | +        isNot: false, | 
|  | 190 | +      } as jest.MatcherUtils; | 
|  | 191 | + | 
|  | 192 | +      const props = {} as ICommonProps; | 
|  | 193 | +      const key = 'key'; | 
|  | 194 | + | 
|  | 195 | +      const wrapped = wrapWithRetryUntilPass(toWrap); | 
|  | 196 | +      const result = await wrapped.bind(matcherUtils)(props, key); | 
|  | 197 | + | 
|  | 198 | +      expect(toWrap).toHaveBeenCalledTimes(2); | 
|  | 199 | +      expect(toWrap).toHaveBeenCalledWith(props, key); | 
|  | 200 | +      expect(result).toBe(expectedResult); | 
|  | 201 | +      expect(sleep).toHaveBeenCalledTimes(1); | 
|  | 202 | +      expect(sleep).toHaveBeenCalledWith(500); // default pollEvery | 
|  | 203 | +    }); | 
|  | 204 | +  }); | 
| 114 | 205 | }); | 
0 commit comments