Last active
December 9, 2019 15:03
-
-
Save vschoener/5bda8b46fdbf40ca3bdb7f1e482f16c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Test, TestingModule } from '@nestjs/testing'; | |
import { INestApplication } from '@nestjs/common'; | |
import * as nock from 'nock'; | |
import { Collection } from 'mongodb'; | |
import * as request from 'supertest'; | |
import { AppModule } from '../../src/app.module'; | |
import { getModelToken } from 'src/core/mongodb/mongodb.utils'; | |
import { Snap } from 'src/snaps/model/schema/snaps.schema'; | |
import { DateManager } from 'src/core/utils/date-manager.service'; | |
import { SnapModel } from 'src/snaps/model/snaps.model'; | |
describe('SnapController (e2e)', () => { | |
let app: INestApplication; | |
let snapsCollection: Collection<Snap>; | |
let snapModel: SnapModel; | |
let dateManager: DateManager; | |
const date = new Date(); | |
const nockCompass = nock('http://localhost:3008').get('/api/routes/match'); | |
beforeEach(async () => { | |
const moduleFixture: TestingModule = await Test.createTestingModule({ | |
imports: [AppModule], | |
}).compile(); | |
app = moduleFixture.createNestApplication(); | |
await app.init(); | |
snapsCollection = app.get<Collection<Snap>>(getModelToken('snaps')); | |
snapModel = app.get<SnapModel>(SnapModel); | |
dateManager = app.get<DateManager>(DateManager); | |
jest.spyOn(dateManager, 'getDate').mockReturnValue(date); | |
await snapsCollection.remove({}); | |
await Promise.all([ | |
snapModel.createSnap({ | |
latitude: 1.896416, | |
longitude: 1.2973549, | |
accuracy: 1, | |
course: 0, | |
actor_id: '123456789012345678901234', | |
actor_type: 'driver', | |
}), | |
snapModel.createSnap({ | |
latitude: 2.896416, | |
longitude: 2.2973549, | |
accuracy: 2, | |
course: 0, | |
actor_id: '123456789012345678901234', | |
actor_type: 'driver', | |
}), | |
snapModel.createSnap({ | |
latitude: 3.896416, | |
longitude: 3.2973549, | |
accuracy: 3, | |
course: 0, | |
actor_id: '023456789012345678901234', | |
actor_type: 'driver', | |
}), | |
snapModel.createSnap({ | |
latitude: 4.896416, | |
longitude: 4.2973549, | |
accuracy: 4, | |
course: 0, | |
actor_id: '023456789012345678901234', | |
actor_type: 'driver', | |
}), | |
]); | |
}); | |
it('/snaps (POST) should succeed and return newly created the snap', async () => { | |
nockCompass.reply(200, { | |
providerName: 'osrm', | |
result: { | |
waypoints: [ | |
{ | |
lat: 10, | |
lng: 10, | |
accuracy: 1, | |
timestamp: 123456789, | |
}, | |
{ | |
lat: 20, | |
lng: 20, | |
accuracy: 2, | |
timestamp: 234567890, | |
}, | |
], | |
}, | |
}); | |
// I need to request db to get the last inserted snap and compare the data to the expect | |
return request(app.getHttpServer()) | |
.post('/snaps') | |
.send({ | |
latitude: 49.896416, | |
longitude: 2.2973549, | |
accuracy: 10, | |
course: 0, | |
actor_id: '123456789012345678901234', | |
actor_type: 'driver', | |
}) | |
.expect(200) | |
.expect({ | |
actor_id: '123456789012345678901234', | |
actor_type: 'driver', | |
gps_position: { | |
latitude: 49.896416, | |
longitude: 2.2973549, | |
accuracy: 10, | |
course: 0, | |
date: '2019-12-06T15:36:14.391Z', | |
}, | |
createdAt: '2019-12-06T15:36:14.391Z', | |
updatedAt: '2019-12-06T15:36:14.391Z', | |
_id: '5dea756e15994984ef115245', | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment