test: Add unit test (#20)
This commit is contained in:
committed by
Benedikt Rötsch
parent
c2297d2e99
commit
b6ae3f9f07
@@ -8,7 +8,9 @@
|
|||||||
"lint": "eslint ./app.js routes",
|
"lint": "eslint ./app.js routes",
|
||||||
"format": "eslint --fix . bin --ignore public node_modules",
|
"format": "eslint --fix . bin --ignore public node_modules",
|
||||||
"test:integration": "jest test/integration",
|
"test:integration": "jest test/integration",
|
||||||
"test:integration:watch": "jest test/integration --watch"
|
"test:integration:watch": "jest test/integration --watch",
|
||||||
|
"test:unit": "jest test/unit",
|
||||||
|
"test:unit:watch": "jest test/unit --watch"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=8.4.0"
|
"node": ">=8.4.0"
|
||||||
|
|||||||
92
test/unit/index.test.js
Normal file
92
test/unit/index.test.js
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
/* global describe, test, beforeAll, afterEach, jest, expect */
|
||||||
|
const { getCourses, getCourse, getCoursesByCategory, getLesson } = require('../../routes/courses')
|
||||||
|
const { getSettings } = require('../../routes/settings')
|
||||||
|
const { mockCourse, mockCategory } = require('./mocks/index')
|
||||||
|
|
||||||
|
jest.mock('../../services/contentful')
|
||||||
|
const contentful = require('../../services/contentful')
|
||||||
|
|
||||||
|
const req = {}
|
||||||
|
const res = {
|
||||||
|
locals: {
|
||||||
|
currentLocale: {
|
||||||
|
code: 'en-US'
|
||||||
|
},
|
||||||
|
currentApi: {
|
||||||
|
id: 'cda'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeAll(() => {
|
||||||
|
contentful.getCourses.mockImplementation(() => [mockCourse])
|
||||||
|
contentful.getCourse.mockImplementation(() => mockCourse)
|
||||||
|
|
||||||
|
contentful.getCategories.mockImplementation(() => [mockCategory])
|
||||||
|
|
||||||
|
contentful.getCoursesByCategory.mockImplementation(() => [])
|
||||||
|
res.render = jest.fn()
|
||||||
|
res.cookie = jest.fn()
|
||||||
|
req.cookies = { visitedLessons: [] }
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
res.render.mockClear()
|
||||||
|
res.render.mockReset()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Courses', () => {
|
||||||
|
test('it should courses list once', async () => {
|
||||||
|
await getCourses(req, res)
|
||||||
|
expect(res.render.mock.calls[0][0]).toBe('courses')
|
||||||
|
expect(res.render.mock.calls[0][1].title).toBe('All Courses (1)')
|
||||||
|
expect(res.render.mock.calls[0][1].courses.length).toBe(1)
|
||||||
|
expect(res.render.mock.calls.length).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('it should render single course once', async () => {
|
||||||
|
req.params = {slug: 'slug', lslug: 'lessonSlug'}
|
||||||
|
await getCourse(req, res)
|
||||||
|
expect(res.render.mock.calls[0][0]).toBe('course')
|
||||||
|
expect(res.render.mock.calls[0][1].title).toBe(mockCourse.fields.title)
|
||||||
|
expect(res.render.mock.calls[0][1].course.sys.id).toBe(mockCourse.sys.id)
|
||||||
|
expect(res.render.mock.calls[0][1].lesson.sys.id).toBe(mockCourse.fields.lessons[0].sys.id)
|
||||||
|
expect(res.render.mock.calls.length).toBe(1)
|
||||||
|
})
|
||||||
|
test('it should render list of courses by categories', async () => {
|
||||||
|
req.params = {slug: 'slug', lslug: 'lslug', category: 'categorySlug'}
|
||||||
|
await getCoursesByCategory(req, res)
|
||||||
|
expect(res.render.mock.calls[0][0]).toBe('courses')
|
||||||
|
expect(res.render.mock.calls[0][1].title).toBe(`${mockCategory.fields.title} (0)`)
|
||||||
|
expect(res.render.mock.calls.length).toBe(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Lessons', () => {
|
||||||
|
test('it should render a lesson', async () => {
|
||||||
|
req.params = { cslug: 'courseSlug', lslug: 'lessonSlug' }
|
||||||
|
await getLesson(req, res)
|
||||||
|
expect(res.render.mock.calls[0][0]).toBe('course')
|
||||||
|
expect(res.render.mock.calls[0][1].title).toBe('Course title | Lesson title')
|
||||||
|
expect(res.render.mock.calls[0][1].course.sys.id).toBe(mockCourse.sys.id)
|
||||||
|
expect(res.render.mock.calls[0][1].lesson.sys.id).toBe(mockCourse.fields.lessons[0].sys.id)
|
||||||
|
expect(res.render.mock.calls.length).toBe(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Settings', () => {
|
||||||
|
test('It should render settings', async () => {
|
||||||
|
res.locals = {
|
||||||
|
settings: {
|
||||||
|
space: 'spaceId',
|
||||||
|
cda: 'cda',
|
||||||
|
cpa: 'cpa',
|
||||||
|
editorialFeatures: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await getSettings(req, res)
|
||||||
|
expect(res.render.mock.calls[0][0]).toBe('settings')
|
||||||
|
expect(res.render.mock.calls[0][1].title).toBe('Settings')
|
||||||
|
expect(res.render.mock.calls[0][1].settings).toBe(res.locals.settings)
|
||||||
|
})
|
||||||
|
})
|
||||||
24
test/unit/mocks/index.js
Normal file
24
test/unit/mocks/index.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
const mockCourse = {
|
||||||
|
sys: { id: 'courseId' },
|
||||||
|
fields: {
|
||||||
|
title: 'Course title',
|
||||||
|
lessons: [
|
||||||
|
{ sys: {id: 'lessonId'}, fields: { slug: 'lessonSlug', title: 'Lesson title' } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mockCategory = {
|
||||||
|
sys: {
|
||||||
|
id: 'categoryId'
|
||||||
|
},
|
||||||
|
fields: {
|
||||||
|
slug: 'categorySlug',
|
||||||
|
title: 'categoryTitle'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
mockCourse,
|
||||||
|
mockCategory
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user