Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed Jan 22, 2025
1 parent 04ee2d5 commit 9fc8ff9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions src/bidiMapper/modules/session/SubscriptionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ describe('SubscriptionManager', () => {
).to.equal(false);
});

it('should unsubscribe by id', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
subscriptionManager.unsubscribeById([id]);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(false);
});

it('should not unsubscribe on error', () => {
subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL);
expect(() =>
Expand Down Expand Up @@ -299,6 +311,18 @@ describe('SubscriptionManager', () => {
).to.equal(false);
});

it('should unsubscribe by id', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[SOME_CONTEXT],
SOME_CHANNEL,
);
subscriptionManager.unsubscribeById([id]);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(false);
});

it('should partially unsubscribe from a context', () => {
subscriptionManager.subscribe(
[SOME_EVENT],
Expand Down Expand Up @@ -430,6 +454,46 @@ describe('SubscriptionManager', () => {
});
});

describe('unsubscribeById', () => {
it('should keep subscription if one of the IDs is not known', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
expect(() => {
subscriptionManager.unsubscribeById([id, 'wrong']);
}).to.throw('No subscription found');
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
});

it('should throw an error if an ID is not know', () => {
expect(() => {
subscriptionManager.unsubscribeById(['wrong']);
}).to.throw('No subscription found');
});

it('should throw an error if a subscription is used multiple times', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
subscriptionManager.unsubscribeById([id]);
expect(() => {
subscriptionManager.unsubscribeById([id]);
}).to.throw('No subscription found');
});
});

describe('cartesian product', () => {
it('should return empty array for empty array', () => {
expect(cartesianProduct([], [])).to.deep.equal([]);
Expand Down
2 changes: 1 addition & 1 deletion src/bidiMapper/modules/session/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class SubscriptionManager {
throw new InvalidArgumentException('No subscription found');
}
this.#subscriptions = this.#subscriptions.filter((subscription) => {
return subscriptionIdsSet.has(subscription.id);
return !subscriptionIdsSet.has(subscription.id);
});
this.#knownSubscriptionIds = difference(
this.#knownSubscriptionIds,
Expand Down

0 comments on commit 9fc8ff9

Please sign in to comment.