-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
651 additions
and
10 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
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
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,99 @@ | ||
import { describe, it } from 'mocha'; | ||
import Delivery from '../dist/index.js'; | ||
|
||
describe('Delivery', () => { | ||
describe('register', () => { | ||
it('should successfully register a new key and return a promise', done => { | ||
const delivery = new Delivery(); | ||
const key = 'newKey'; | ||
|
||
const result = delivery.register(key); | ||
|
||
if ( | ||
result instanceof Promise && | ||
delivery['promises'][key] && | ||
delivery['promises'][key].promise === result | ||
) { | ||
done(); | ||
} else { | ||
done(new Error('Failed to register a new key and return a promise')); | ||
} | ||
}); | ||
|
||
it('should throw an error when trying to register a key that already exists', done => { | ||
const delivery = new Delivery(); | ||
const key = 'existingKey'; | ||
|
||
delivery.register(key); | ||
|
||
try { | ||
delivery.register(key); | ||
done(new Error('Expected error was not thrown')); | ||
} catch (error) { | ||
if ( | ||
error.message === `Promise with Key: ${key} is already registered` | ||
) { | ||
done(); | ||
} else { | ||
done(new Error('Unexpected error message')); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
describe('resolve', () => { | ||
it('should resolve the promise with the given key', done => { | ||
const delivery = new Delivery(); | ||
const key = 'resolveKey'; | ||
const value = 'resolvedValue'; | ||
|
||
const promise = delivery.register(key); | ||
delivery.resolve(key, value); | ||
|
||
promise | ||
.then(result => { | ||
if (result === value) { | ||
done(); | ||
} else { | ||
done(new Error('Promise was not resolved with the correct value')); | ||
} | ||
}) | ||
.catch(done); | ||
}); | ||
}); | ||
|
||
describe('reject', () => { | ||
it('should reject the promise with the given key', done => { | ||
const delivery = new Delivery(); | ||
const key = 'rejectKey'; | ||
const reason = 'rejectedReason'; | ||
|
||
const promise = delivery.register(key); | ||
delivery.reject(key, reason); | ||
|
||
promise.catch(error => { | ||
if (error === reason) { | ||
done(); | ||
} else { | ||
done(new Error('Promise was not rejected with the correct reason')); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getPromise', () => { | ||
it('should return the promise associated with the given key', done => { | ||
const delivery = new Delivery(); | ||
const key = 'getPromiseKey'; | ||
|
||
const registeredPromise = delivery.register(key); | ||
const retrievedPromise = delivery.getPromise(key); | ||
|
||
if (registeredPromise === retrievedPromise) { | ||
done(); | ||
} else { | ||
done(new Error('getPromise did not return the correct promise')); | ||
} | ||
}); | ||
}); | ||
}); |
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,7 +1,48 @@ | ||
declare class Delivery<T> { | ||
private promises; | ||
/** | ||
* Registers a new promise with the given key. | ||
* | ||
* This method creates a new promise and stores it along with its resolve and reject functions. | ||
* If a promise with the given key already exists, an error is thrown. | ||
* | ||
* @param key - The unique identifier for the promise to be registered. | ||
* @throws Will throw an error if a promise with the given key is already registered. | ||
* @returns A Promise<T> that can be used to handle the asynchronous operation. | ||
*/ | ||
register(key: string): Promise<T>; | ||
/** | ||
* Resolves the promise associated with the given key with the provided value. | ||
* | ||
* @param key - The unique identifier for the promise to be resolved. | ||
* @param value - The value to fulfill the promise with. | ||
* | ||
* @throws Will throw an error if a promise with the given key is not found. | ||
* | ||
* @returns {void} | ||
*/ | ||
resolve(key: string, value: T): void; | ||
/** | ||
* Rejects the promise associated with the given key with the provided reason. | ||
* | ||
* This method finds the promise with the specified key, rejects it with the given reason, | ||
* and then removes it from the internal promises storage. | ||
* | ||
* @param key - The unique identifier for the promise to be rejected. | ||
* @param reason - The reason for rejecting the promise. | ||
* @throws Will throw an error if a promise with the given key is not found. | ||
* @returns {void} | ||
*/ | ||
reject(key: string, reason: string): void; | ||
/** | ||
* Returns the promise associated with the given key. | ||
* | ||
* This method finds the promise with the specified key and returns it. | ||
* | ||
* @param key - The unique identifier for the promise to be retrieved. | ||
* @throws Will throw an error if a promise with the given key is not found. | ||
* @returns {Promise<T>} The promise associated with the given key. | ||
*/ | ||
getPromise(key: string): Promise<T>; | ||
} | ||
export default Delivery; |
Oops, something went wrong.