-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
2 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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { ShipController } from './controllers/ShipController'; | ||
import ShipRepository from './repositories/ShipRepository'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
controllers: [AppController, ShipController], | ||
providers: [AppService, PrismaClient, ShipRepository], | ||
}) | ||
export class AppModule {} |
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,60 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { Response } from 'express'; | ||
|
||
import { ShipController } from './ShipController'; | ||
import ShipRepository from '../repositories/ShipRepository'; | ||
import Ship from '../models/Ship'; | ||
import Position from '../models/Position'; | ||
import Location from '../models/Location'; | ||
|
||
describe('ShipController', () => { | ||
let shipController: ShipController; | ||
let shipRepository: ShipRepository; | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
controllers: [ShipController], | ||
providers: [PrismaClient, ShipRepository], | ||
}).compile(); | ||
|
||
shipController = app.get<ShipController>(ShipController); | ||
shipRepository = app.get<ShipRepository>(ShipRepository); | ||
}); | ||
|
||
describe('index', () => { | ||
it('it returns a list of ship', async () => { | ||
// given | ||
const response = { | ||
json: jest.fn(), | ||
} as unknown as Response; | ||
const destination = new Location('europa', 'Europa', new Position(5, 6)); | ||
const ships = [ | ||
new Ship(1, 'Discovery One', new Position(1, 2), null), | ||
new Ship(2, 'Europa Report', new Position(3, 4), destination), | ||
]; | ||
jest | ||
.spyOn(shipRepository, 'getAll') | ||
.mockImplementation(async () => ships); | ||
|
||
// when | ||
await shipController.index(response); | ||
|
||
// then | ||
expect(response.json).toHaveBeenCalledWith([ | ||
{ | ||
id: 1, | ||
name: 'Discovery One', | ||
position: { x: 1, y: 2 }, | ||
destination: null, | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Europa Report', | ||
position: { x: 3, y: 4 }, | ||
destination: { name: 'Europa' }, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { Controller, Get, Res } from '@nestjs/common'; | ||
import { Response } from 'express'; | ||
import ShipRepository from '../repositories/ShipRepository'; | ||
|
||
@Controller() | ||
export class ShipController { | ||
constructor(private readonly shipRepository: ShipRepository) {} | ||
|
||
@Get('ships') | ||
async index(@Res() res: Response): Promise<void> { | ||
const ships = await this.shipRepository.getAll(); | ||
const shipsResponse = ships.map((ship) => { | ||
return { | ||
id: ship.id, | ||
name: ship.name, | ||
position: { x: ship.currentPosition.x, y: ship.currentPosition.y }, | ||
destination: ship.isStationary ? null : { name: ship.destination.name }, | ||
}; | ||
}); | ||
res.json(shipsResponse); | ||
} | ||
} |
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