-
Notifications
You must be signed in to change notification settings - Fork 1
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
2 changed files
with
74 additions
and
10 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
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,20 +1,85 @@ | ||
import { INestApplication, ValidationPipe } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { Course, Session } from '@prisma/client'; | ||
import { useContainer } from 'class-validator'; | ||
import * as request from 'supertest'; | ||
|
||
import { CourseService } from '../../src/course/course.service'; | ||
import { AppModule } from '../../src/app.module'; | ||
import { PrismaService } from '../../src/prisma/prisma.service'; | ||
|
||
describe('CourseService', () => { | ||
let service: CourseService; | ||
describe('CourseService (e2e)', () => { | ||
let app: INestApplication; | ||
let prisma: PrismaService; | ||
let course: Course; | ||
let session: Session; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [PrismaService, CourseService], | ||
const courseShape = expect.objectContaining({ | ||
id: expect.any(String), | ||
programId: expect.any(String), | ||
code: expect.any(String), | ||
title: expect.any(String), | ||
description: expect.any(String), | ||
credits: expect.any(Number), | ||
}); | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
service = module.get<CourseService>(CourseService); | ||
app = moduleFixture.createNestApplication(); | ||
prisma = app.get<PrismaService>(PrismaService); | ||
|
||
useContainer(app.select(AppModule), { fallbackOnErrors: true }); | ||
app.useGlobalPipes(new ValidationPipe({ whitelist: true })); | ||
|
||
await app.init(); | ||
|
||
session = await prisma.session.create({ | ||
data: { | ||
trimester: 'A', | ||
year: 2021, | ||
}, | ||
}); | ||
|
||
course = await prisma.course.create({ | ||
data: { | ||
id: '1', | ||
programId: '1', | ||
code: 'CS101', | ||
title: 'Introduction to Computer Science', | ||
description: 'A basic course on computer science', | ||
credits: 3, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
afterAll(async () => { | ||
await prisma.course.deleteMany(); | ||
await prisma.session.deleteMany(); | ||
await prisma.$disconnect(); | ||
await app.close(); | ||
}); | ||
|
||
describe('GET /courses', () => { | ||
it('returns a list of courses', async () => { | ||
const { status, body } = await request(app.getHttpServer()).get( | ||
'/courses', | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(body).toStrictEqual(expect.arrayContaining([courseShape])); | ||
}); | ||
}); | ||
|
||
describe('GET /courses/:id', () => { | ||
it('returns a course', async () => { | ||
const { status, body } = await request(app.getHttpServer()).get( | ||
`/courses/${course.id}`, | ||
); | ||
|
||
expect(status).toBe(200); | ||
expect(body).toStrictEqual(courseShape); | ||
}); | ||
}); | ||
}); |