Skip to content

Commit

Permalink
test: add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Sep 19, 2022
1 parent eb5c2d2 commit 4a43505
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
21 changes: 19 additions & 2 deletions integration/scopes/e2e/durable-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ describe('Durable providers', () => {
});

describe('when service is durable', () => {
const performHttpCall = (tenantId: number, end: (err?: any) => void) =>
const performHttpCall = (
tenantId: number,
end: (err?: any) => void,
endpoint = '/durable',
) =>
request(server)
.get('/durable')
.get(endpoint)
.set({ ['x-tenant-id']: tenantId })
.end((err, res) => {
if (err) return end(err);
Expand Down Expand Up @@ -67,6 +71,19 @@ describe('Durable providers', () => {
);
expect(result.text).equal('Hello world! Counter: 1');
});

it(`should register a custom per-tenant request payload`, async () => {
let result: request.Response;
result = await new Promise<request.Response>(resolve =>
performHttpCall(1, resolve, '/durable/echo'),
);
expect(result.body).deep.equal({ tenantId: '1' });

result = await new Promise<request.Response>(resolve =>
performHttpCall(3, resolve, '/durable/echo'),
);
expect(result.body).deep.equal({ tenantId: '3' });
});
});

after(async () => {
Expand Down
7 changes: 5 additions & 2 deletions integration/scopes/src/durable/durable-context-id.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class DurableContextIdStrategy implements ContextIdStrategy {
tenantSubTreeId = { id: +tenantId } as ContextId;
tenants.set(tenantId, tenantSubTreeId);
}
return (info: HostComponentInfo) =>
info.isTreeDurable ? tenantSubTreeId : contextId;
return {
resolve: (info: HostComponentInfo) =>
info.isTreeDurable ? tenantSubTreeId : contextId,
payload: { tenantId },
};
}
}
5 changes: 5 additions & 0 deletions integration/scopes/src/durable/durable.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ export class DurableController {
greeting(): string {
return this.durableService.greeting();
}

@Get('echo')
echo() {
return this.durableService.requestPayload;
}
}
5 changes: 4 additions & 1 deletion integration/scopes/src/durable/durable.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Injectable, Scope } from '@nestjs/common';
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';

@Injectable({ scope: Scope.REQUEST, durable: true })
export class DurableService {
public instanceCounter = 0;

constructor(@Inject(REQUEST) public readonly requestPayload: unknown) {}

greeting() {
++this.instanceCounter;
return `Hello world! Counter: ${this.instanceCounter}`;
Expand Down

0 comments on commit 4a43505

Please sign in to comment.