-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(javascript): provide
browseObjects
, browseRules
and `browseS…
…ynonyms` helper methods (#887)
- Loading branch information
Showing
17 changed files
with
612 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.11 | ||
0.0.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
clients/algoliasearch-client-javascript/packages/client-common/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
238 changes: 238 additions & 0 deletions
238
...ch-client-javascript/packages/client-common/src/__tests__/create-iterable-promise.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
import { createIterablePromise } from '../createIterablePromise'; | ||
|
||
describe('createIterablePromise', () => { | ||
describe('func', () => { | ||
it('provides the `previousResponse` parameter', async () => { | ||
const responses: Array<string | undefined> = []; | ||
const promise = createIterablePromise<string | undefined>({ | ||
func: (previousResponse) => { | ||
return new Promise((resolve) => { | ||
resolve(previousResponse === undefined ? 'yes' : 'no'); | ||
}); | ||
}, | ||
validate: () => responses.length === 3, | ||
aggregator: (response) => responses.push(response), | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('no'); | ||
expect(responses).toEqual(['yes', 'no', 'no']); | ||
}); | ||
}); | ||
|
||
describe('validate', () => { | ||
it('iterates on a `func` until `validate` is met', async () => { | ||
let calls = 0; | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
let calls = 0; | ||
const promise = createIterablePromise<number>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(calls); | ||
}); | ||
}, | ||
validate: (response) => response >= 3, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual(3); | ||
expect(calls).toBe(3); | ||
}); | ||
}); | ||
|
||
describe('aggregator', () => { | ||
it('is called before iterating', async () => { | ||
let calls = 0; | ||
let count = 0; | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
aggregator: () => (count += 3), | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
expect(count).toBe(3 * 3); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
let calls = 0; | ||
const responses: string[] = []; | ||
const promise = createIterablePromise<string>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 3, | ||
aggregator: (response) => { | ||
responses.push(response); | ||
}, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #3'); | ||
expect(calls).toBe(3); | ||
expect(responses).toEqual(['success #1', 'success #2', 'success #3']); | ||
}); | ||
}); | ||
|
||
describe('timeout', () => { | ||
it('defaults to no timeout (0)', async () => { | ||
let calls = 0; | ||
const before = Date.now(); | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 2, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #2'); | ||
|
||
expect(Date.now() - before).toBeGreaterThanOrEqual(0); | ||
expect(Date.now() - before).toBeLessThan(10); | ||
expect(calls).toBe(2); | ||
}); | ||
|
||
it('waits before calling the `func` again', async () => { | ||
let calls = 0; | ||
const before = Date.now(); | ||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(`success #${calls}`); | ||
}); | ||
}, | ||
validate: () => calls >= 2, | ||
timeout: () => 2000, | ||
}); | ||
|
||
await expect(promise).resolves.toEqual('success #2'); | ||
|
||
expect(Date.now() - before).toBeGreaterThanOrEqual(2000); | ||
expect(Date.now() - before).toBeLessThan(2010); | ||
expect(calls).toBe(2); | ||
}); | ||
}); | ||
|
||
describe('error', () => { | ||
it('gets the rejection of the given promise via reject', async () => { | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve, reject) => { | ||
calls += 1; | ||
if (calls <= 3) { | ||
resolve('okay'); | ||
} else { | ||
reject(new Error('nope')); | ||
} | ||
}); | ||
}, | ||
validate: () => false, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ message: 'nope' }) | ||
); | ||
}); | ||
|
||
it('gets the rejection of the given promise via throw', async () => { | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
if (calls <= 3) { | ||
resolve('okay'); | ||
} else { | ||
throw new Error('nope'); | ||
} | ||
}); | ||
}, | ||
validate: () => false, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ message: 'nope' }) | ||
); | ||
}); | ||
|
||
it('rejects with the given `message` when `validate` hits', async () => { | ||
const MAX_RETRIES = 3; | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve('okay'); | ||
}); | ||
}, | ||
validate: () => false, | ||
error: { | ||
validate: () => calls >= MAX_RETRIES, | ||
message: () => `Error is thrown: ${calls}/${MAX_RETRIES}`, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ | ||
message: 'Error is thrown: 3/3', | ||
}) | ||
); | ||
expect(calls).toBe(MAX_RETRIES); | ||
}); | ||
|
||
it('forward the response of the `func`', async () => { | ||
const MAX_RETRIES = 3; | ||
let calls = 0; | ||
|
||
const promise = createIterablePromise<number>({ | ||
func: () => { | ||
return new Promise((resolve) => { | ||
calls += 1; | ||
resolve(calls); | ||
}); | ||
}, | ||
validate: () => false, | ||
error: { | ||
validate: (response) => response >= MAX_RETRIES, | ||
message: (response) => `Error is thrown: ${response}/${MAX_RETRIES}`, | ||
}, | ||
}); | ||
|
||
await expect(promise).rejects.toEqual( | ||
expect.objectContaining({ | ||
message: 'Error is thrown: 3/3', | ||
}) | ||
); | ||
expect(calls).toBe(MAX_RETRIES); | ||
}); | ||
}); | ||
}); |
86 changes: 0 additions & 86 deletions
86
...h-client-javascript/packages/client-common/src/__tests__/create-retryable-promise.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.