Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(entities): add http API for listing all entities
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { EntitiesController } from './entities.controller'; | ||
import { EntitiesService } from './entities.service'; | ||
import { Sensor } from './sensor'; | ||
import { Switch } from './switch'; | ||
import { Entity } from './entity'; | ||
import { NestEmitterModule } from 'nest-emitter'; | ||
import { ClusterModule } from '../cluster/cluster.module'; | ||
import { EventEmitter } from 'events'; | ||
import { ClusterService } from '../cluster/cluster.service'; | ||
|
||
describe('Entities Controller', () => { | ||
let controller: EntitiesController; | ||
let service: EntitiesService; | ||
const emitter: EventEmitter = new EventEmitter(); | ||
const clusterService = {}; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [NestEmitterModule.forRoot(emitter), ClusterModule], | ||
controllers: [EntitiesController], | ||
providers: [EntitiesService] | ||
}) | ||
.overrideProvider(ClusterService) | ||
.useValue(clusterService) | ||
.compile(); | ||
|
||
controller = module.get<EntitiesController>(EntitiesController); | ||
service = module.get<EntitiesService>(EntitiesService); | ||
}); | ||
|
||
it('should return all registered entities', () => { | ||
const entities: Entity[] = [ | ||
new Sensor('sensor', 'Test Sensor'), | ||
new Switch('switch', 'Test Switch') | ||
]; | ||
jest.spyOn(service, 'getAll').mockImplementation(() => { | ||
return entities; | ||
}); | ||
|
||
expect(controller.getAll()).toBe(entities); | ||
}); | ||
}); |
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,13 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Entity } from './entity'; | ||
import { EntitiesService } from './entities.service'; | ||
|
||
@Controller('entities') | ||
export class EntitiesController { | ||
constructor(private readonly entitiesService: EntitiesService) {} | ||
|
||
@Get() | ||
getAll(): Entity[] { | ||
return this.entitiesService.getAll(); | ||
} | ||
} |
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,10 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EntitiesService } from './entities.service'; | ||
import { ClusterModule } from '../cluster/cluster.module'; | ||
import { EntitiesController } from './entities.controller'; | ||
|
||
@Module({ | ||
imports: [ClusterModule], | ||
providers: [EntitiesService], | ||
controllers: [EntitiesController], | ||
exports: [EntitiesService] | ||
}) | ||
export class EntitiesModule {} |
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