-
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.
Courses organize related sentence lists together Signed-off-by: Abhishek Kumar <[email protected]>
- Loading branch information
1 parent
ff26c9c
commit 28294c1
Showing
4 changed files
with
108 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,15 @@ | ||
import type { Request, Response } from 'express'; | ||
|
||
import { Router } from 'express'; | ||
import Course from '../course'; | ||
|
||
const router = Router(); | ||
|
||
export async function getCourses(req: Request, res: Response) { | ||
const courses = await Course.find({}); | ||
res.json(courses); | ||
} | ||
|
||
router.get('/', getCourses); | ||
|
||
export default router; |
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,11 @@ | ||
import { InferSchemaType, Schema, model } from 'mongoose'; | ||
import { sentenceListSchema } from '../sentence-list'; | ||
|
||
export const courseSchema = new Schema({ | ||
title: { type: String, required: true }, | ||
sentenceLists: { type: [sentenceListSchema], default: [] }, | ||
}); | ||
|
||
export type CourseType = InferSchemaType<typeof courseSchema>; | ||
|
||
export default model('Course', courseSchema); |
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
afterAll, | ||
afterEach, | ||
beforeAll, | ||
describe, | ||
expect, | ||
it, | ||
} from '@jest/globals'; | ||
|
||
import { createRequest, createResponse } from 'node-mocks-http'; | ||
|
||
import mongoose, { HydratedDocument } from 'mongoose'; | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
|
||
import SentenceList from '../../../sentence-list'; | ||
import { UserProfile, UserProfileType } from '../../../user-profile'; | ||
import { getCourses } from '../../../api/courses'; | ||
import Course from '../../../course'; | ||
|
||
describe('GET courses', () => { | ||
let mongoDB: MongoMemoryServer; | ||
let alice: HydratedDocument<UserProfileType>; | ||
|
||
beforeAll(async () => { | ||
mongoDB = await MongoMemoryServer.create(); | ||
const uri = mongoDB.getUri(); | ||
await mongoose.connect(uri); | ||
|
||
alice = await UserProfile.create({ | ||
userId: 'abc', | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.disconnect(); | ||
await mongoDB.stop(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await Promise.all([SentenceList.deleteMany(), Course.deleteMany()]); | ||
}); | ||
|
||
it('should return empty array if there are not courses', async () => { | ||
const req = createRequest(); | ||
const res = createResponse(); | ||
|
||
await getCourses(req, res); | ||
const data = res._getJSONData(); | ||
|
||
expect(data.length).toBe(0); | ||
}); | ||
|
||
it('should return all courses along with all lists', async () => { | ||
const testLists = await SentenceList.insertMany([ | ||
{ | ||
title: 'Korean 1', | ||
owner: alice, | ||
}, | ||
{ | ||
title: 'Korean 2', | ||
owner: alice, | ||
}, | ||
]); | ||
|
||
const testCourse = await Course.create({ | ||
title: 'Korean', | ||
sentenceLists: testLists, | ||
}); | ||
|
||
const req = createRequest(); | ||
const res = createResponse(); | ||
|
||
await getCourses(req, res); | ||
const data = res._getJSONData(); | ||
|
||
expect(data.length).toBe(1); | ||
expect(data[0].title).toBe(testCourse.title); | ||
expect(data[0].sentenceLists.length).toBe(2); | ||
}); | ||
}); |