Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
doublelam committed Dec 20, 2024
1 parent 8e7023c commit e647777
Show file tree
Hide file tree
Showing 6 changed files with 651 additions and 10 deletions.
48 changes: 47 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
class Delivery {
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) {
if (this.promises[key]) {
throw new Error(`Promise with Key: ${key} is already registered`);
Expand All @@ -16,19 +26,55 @@ class Delivery {
this.promises[key] = state;
return this.promises[key].promise;
}
/**
* 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, value) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].resolve(value);
delete this.promises[key];
}
/**
* 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, reason) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
this.promises[key].reject(reason);
delete this.promises[key];
}
/**
* 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) {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
return this.promises[key].promise;
}
}
export default Delivery;
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"description": "This package is used to manage multiple promises by giving a key, you can call resolve or reject function out of the promise parameter callback and manage them by a key.",
"version": "1.0.1",
"main": "./dist/index.js",
"scripts": {
"test": "mocha"
},
"type": "module",
"repository": "https://github.com/doublelam/promises-delivery",
"types": "./types/index.d.ts",
Expand All @@ -13,10 +16,12 @@
"@eslint/eslintrc": "3.2.0",
"@typescript-eslint/eslint-plugin": "8.3.0",
"@typescript-eslint/parser": "8.3.0",
"chai": "5.1.2",
"eslint": "9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "5.2.1",
"mocha": "11.0.1",
"prettier": "3.3.3",
"typescript": "5.5.4"
},
Expand Down
16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ class Delivery<T> {
this.promises[key].reject(reason);
delete this.promises[key];
}

/**
* 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.
*/
public getPromise(key: string): Promise<T> {
if (!this.promises[key]) {
throw new Error(`Promise with Key: ${key} is not found`);
}
return this.promises[key].promise;
}
}

export default Delivery;
99 changes: 99 additions & 0 deletions test/index.js
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'));
}
});
});
});
41 changes: 41 additions & 0 deletions types/index.d.ts
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;
Loading

0 comments on commit e647777

Please sign in to comment.