Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(testing-handlers): create real context
- Loading branch information
Showing
10 changed files
with
383 additions
and
262 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export interface TestingHandler<TOutput = any> { | ||
run(): TOutput; | ||
run(): Promise<TOutput>; | ||
} |
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
135 changes: 135 additions & 0 deletions
135
packages/testing/test/handlers/http-testing-handler.spec.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,135 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { | ||
Body, | ||
Controller, | ||
createParamDecorator, | ||
ExecutionContext, | ||
Headers, | ||
Param, | ||
Put, | ||
Get, | ||
Delete, | ||
} from '../../../common'; | ||
import { Test } from '../../test'; | ||
import { TestingModule } from '../../testing-module'; | ||
|
||
describe('Http Testing Handlers', () => { | ||
let testingModule: TestingModule; | ||
|
||
describe('With a simple HTTP controller...', () => { | ||
@Controller('dogs') | ||
class DogsController { | ||
@Get() | ||
fetchAllDogs() { | ||
return ['Dogo', 'Doge', 'Dogg']; | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
controllers: [DogsController], | ||
}).compile(); | ||
}); | ||
|
||
afterEach(() => testingModule.close()); | ||
|
||
it('creates an executable handler for the given HTTP controller class and method', async () => { | ||
const request = { params: { name: 'Dogo' }, body: { age: 10 } }; | ||
const handler = testingModule | ||
.createHttpHandler({ | ||
class: DogsController, | ||
methodName: 'fetchAllDogs', | ||
}) | ||
.setRequest(request); | ||
|
||
const result = await handler.run(); | ||
|
||
expect(result).to.eql(['Dogo', 'Doge', 'Dogg']); | ||
}); | ||
}); | ||
|
||
describe('With a custom Request...', () => { | ||
@Controller('dogs') | ||
class DogsController { | ||
@Put(':name') | ||
update( | ||
@Param('name') name: string, | ||
@Body('age') age: number, | ||
@Headers() headers: Record<string, string>, | ||
) { | ||
if (headers && headers.Authorization) { | ||
return { name, age, chipId: '1234567' }; | ||
} | ||
|
||
return { name, age }; | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
controllers: [DogsController], | ||
}).compile(); | ||
}); | ||
|
||
afterEach(() => testingModule.close()); | ||
|
||
it('allows configuring request', async () => { | ||
const request = { | ||
params: { name: 'Doggo' }, | ||
body: { age: 9 }, | ||
headers: { Authorization: 'AccessKey K0tL3t' }, | ||
}; | ||
const handler = testingModule.createHttpHandler({ | ||
class: DogsController, | ||
methodName: 'update', | ||
}); | ||
|
||
const result = await handler.setRequest(request).run(); | ||
|
||
expect(result).to.eql({ name: 'Doggo', age: 9, chipId: '1234567' }); | ||
}); | ||
}); | ||
|
||
describe('With custom Param Decorators', () => { | ||
const CurrentUser = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
return req.user; | ||
}, | ||
); | ||
@Controller('tasks') | ||
class TasksController { | ||
@Delete() | ||
deleteTasks(@CurrentUser() user: { userId: string }) { | ||
return { userId: user.userId, deleteCount: 16 }; | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
controllers: [TasksController], | ||
}).compile(); | ||
}); | ||
|
||
afterEach(() => testingModule.close()); | ||
|
||
it('handles custom param decorators', async () => { | ||
const request = { | ||
params: { name: 'Doggy' }, | ||
user: { userId: '2d2301c6-5e4d-4257-97e0-ec59005c77c8' }, | ||
}; | ||
const handler = testingModule.createHttpHandler({ | ||
class: TasksController, | ||
methodName: 'deleteTasks', | ||
}); | ||
|
||
const result = await handler.setRequest(request).run(); | ||
|
||
expect(result).to.eql({ | ||
userId: '2d2301c6-5e4d-4257-97e0-ec59005c77c8', | ||
deleteCount: 16, | ||
}); | ||
}); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
packages/testing/test/handlers/rpc-testing-handler.spec.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,69 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { Controller } from '../../../common'; | ||
import { MessagePattern, Payload, Ctx } from '../../../microservices'; | ||
import { Test } from '../../test'; | ||
import { TestingModule } from '../../testing-module'; | ||
|
||
describe('RPC Testing Handlers', () => { | ||
let testingModule: TestingModule; | ||
|
||
describe('With a simple RPC controller', () => { | ||
@Controller() | ||
class MathController { | ||
@MessagePattern({ cmd: 'sum' }) | ||
accumulate(data: number[]): number { | ||
return (data || []).reduce((a, b) => a + b, 0); | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
controllers: [MathController], | ||
}).compile(); | ||
}); | ||
|
||
afterEach(() => testingModule.close()); | ||
|
||
it('creates an executable handler for the given RPC controller class and method', async () => { | ||
const handler = testingModule.createRpcHandler({ | ||
class: MathController, | ||
methodName: 'accumulate', | ||
}); | ||
|
||
const result = await handler.run(); | ||
|
||
expect(result).to.equal(0); | ||
}); | ||
}); | ||
|
||
describe('With a custom context', () => { | ||
@Controller() | ||
class MathController { | ||
@MessagePattern({ cmd: 'sum' }) | ||
accumulate(data: number[], context: any): number { | ||
return (data || []).reduce((a, b) => a + b, context.offset); | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
testingModule = await Test.createTestingModule({ | ||
controllers: [MathController], | ||
}).compile(); | ||
}); | ||
|
||
afterEach(() => testingModule.close()); | ||
|
||
it('allows configuring the data and context passed to the handler', async () => { | ||
const handler = testingModule.createRpcHandler({ | ||
class: MathController, | ||
methodName: 'accumulate', | ||
}); | ||
const data = [1, 2, 3]; | ||
const context = { offset: 10 }; | ||
const result = await handler.setData(data).setContext(context).run(); | ||
|
||
expect(result).to.equal(16); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.