refactor: Cleanup (#21)

* refactor(routes): remove unused sitemap route

* style(comments): enforce consistent comment style

* style(exports): enforce consistent export style

* chore: Add jsDoc for contentful service

* chore: Add jsDoc for contentful service \n \n closes #23

* refactor(app): move query parameter comment to right position and mention comment route middleware

* test(npm): add temporary test script

* refactor(middlewares): split up bootstrap middleware - fixes #23759

* refactor(cookies): use constances to give context the maxAge cookie setting

* refactor(variables): use more descriptive names for variables

* space became spaceId when it was just the id not the full space instance
* all (access) token variable name variants became [api-type]Token
* all clients are now called deliveryClient or previewClient
* cpa and cda only remain when they are used as actual API id
* env variables names adjusted

* perf(helpers): only run marked when content is not empty

* refactor(comments): fix typos

* refactor(comments): add hint why error is logged to console in settings

* chore: Add comments to routes and services

* refactor(requires): order and group requires

* fix(settings): add validation for wrong preview token

* chore: Add comments to routes

* chore: fix typo

* chore: console.error -> throw

* chore: move cookie name to a constant

* chore: Fix app.js comment

* typo: removing t

* chore: typos

* chore: removed unnecessary comment line sign

* chore: newline for readabillity

* chore: remove dangling `t`

* chore: remove `t`, add `l`

* chore: typos

* Fleshed out title

* build(npm): remove unused dependencies

* build(npm): upgrade to latest stable contentful sdk

* chore: Addressing David feedbak

* fix(credentials): update to match the new space

* chore: Addressing code review comments

* chore: Addressing code review comments

* chore: res -> response, req-> request

* chore: include exactly what we need

* chore: Address JPs comments

* chore: Address JPs comments

* chore: Address Fredericks comments

* chore: Address Fredericks comments

* fixup! chore: Address Fredericks comments

* fixup! fixup! chore: Address Fredericks comments

* fixup! fixup! fixup! chore: Address Fredericks comments

* fixup! fixup! fixup! fixup! chore: Address Fredericks comments

* fixup! fixup! fixup! fixup! fixup! chore: Address Fredericks comments

* fixup! fixup! fixup! fixup! fixup! fixup! chore: Address Fredericks comments
This commit is contained in:
Benedikt Rötsch
2017-11-03 10:58:42 +01:00
parent 730ce24cc9
commit 7ad2c1fa4f
25 changed files with 507 additions and 340 deletions

6
test/.eslintrc.js Normal file
View File

@@ -0,0 +1,6 @@
module.exports = {
'env': {
'node': true,
'jest': true
}
}

View File

@@ -1,7 +1,7 @@
/* global describe, test, expect */
const app = require('../../app')
const request = require('supertest')
const app = require('../../app')
describe('courses', () => {
test('it should render a list of courses', () => {
return request(app).get('/courses')

View File

@@ -1,7 +1,7 @@
/* global describe, test */
const app = require('../../app')
const request = require('supertest')
const app = require('../../app')
describe('Home page', () => {
test('it should render the landing page', () => {
return request(app).get('/').expect(200)

View File

@@ -1,12 +1,13 @@
const app = require('../../app')
const request = require('supertest')
const cheerio = require('cheerio')
const cookie = require('cookie')
const cookieParser = require('cookie-parser')
const request = require('supertest')
function getSettingsCookie (res) {
const app = require('../../app')
function getSettingsCookie (response) {
try {
const cookies = res.headers['set-cookie']
const cookies = response.headers['set-cookie']
const settingsCookie = cookies.find((cookie) => cookie.startsWith('theExampleAppSettings='))
const parsedCookie = cookie.parse(settingsCookie)
return cookieParser.JSONCookie(parsedCookie.theExampleAppSettings)
@@ -38,15 +39,15 @@ describe('settings', () => {
expect(inputCpa.val()).toBe(process.env.CF_PREVIEW_ACCESS_TOKEN)
const inputEditorialFeatures = $('#input-editorial-features')
expect(inputEditorialFeatures.prop('checked')).toBeFalsy()
expect(inputEditorialFeaturesponse.prop('checked')).toBeFalsy()
})
})
test('should have the editorial features enabled when query parameter is set and set cookie for it', () => {
return request(app).get('/settings?enable_editorial_features')
.expect(200)
.expect((res) => {
const cookie = getSettingsCookie(res)
.expect((response) => {
const cookie = getSettingsCookie(response)
if (!cookie.editorialFeatures) {
throw new Error('Did not set cookie value for editorial features')
}
@@ -67,7 +68,7 @@ describe('settings', () => {
const $ = cheerio.load(response.text)
const inputEditorialFeatures = $('#input-editorial-features')
expect(inputEditorialFeatures.prop('checked')).toBeTruthy()
expect(inputEditorialFeaturesponse.prop('checked')).toBeTruthy()
})
})
})

View File

@@ -6,8 +6,8 @@ const { mockCourse, mockCategory } = require('./mocks/index')
jest.mock('../../services/contentful')
const contentful = require('../../services/contentful')
const req = {}
const res = {
const request = {}
const response = {
locals: {
currentLocale: {
code: 'en-US'
@@ -25,58 +25,58 @@ beforeAll(() => {
contentful.getCategories.mockImplementation(() => [mockCategory])
contentful.getCoursesByCategory.mockImplementation(() => [])
res.render = jest.fn()
res.cookie = jest.fn()
req.cookies = { visitedLessons: [] }
response.render = jest.fn()
response.cookie = jest.fn()
request.cookies = { visitedLessons: [] }
})
afterEach(() => {
res.render.mockClear()
res.render.mockReset()
response.render.mockClear()
response.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)
await getCourses(request, response)
expect(response.render.mock.calls[0][0]).toBe('courses')
expect(response.render.mock.calls[0][1].title).toBe('All Courses (1)')
expect(response.render.mock.calls[0][1].courses.length).toBe(1)
expect(response.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)
request.params = {slug: 'slug', lslug: 'lessonSlug'}
await getCourse(request, response)
expect(response.render.mock.calls[0][0]).toBe('course')
expect(response.render.mock.calls[0][1].title).toBe(mockCourse.fields.title)
expect(response.render.mock.calls[0][1].course.sys.id).toBe(mockCourse.sys.id)
expect(response.render.mock.calls[0][1].lesson.sys.id).toBe(mockCourse.fields.lessons[0].sys.id)
expect(response.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)
request.params = {slug: 'slug', lslug: 'lslug', category: 'categorySlug'}
await getCoursesByCategory(request, response)
expect(response.render.mock.calls[0][0]).toBe('courses')
expect(response.render.mock.calls[0][1].title).toBe(`${mockCategory.fields.title} (0)`)
expect(response.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)
request.params = { cslug: 'courseSlug', lslug: 'lessonSlug' }
await getLesson(request, response)
expect(response.render.mock.calls[0][0]).toBe('course')
expect(response.render.mock.calls[0][1].title).toBe('Course title | Lesson title')
expect(response.render.mock.calls[0][1].course.sys.id).toBe(mockCourse.sys.id)
expect(response.render.mock.calls[0][1].lesson.sys.id).toBe(mockCourse.fields.lessons[0].sys.id)
expect(response.render.mock.calls.length).toBe(1)
})
})
describe('Settings', () => {
test('It should render settings', async () => {
res.locals = {
response.locals = {
settings: {
space: 'spaceId',
cda: 'cda',
@@ -84,9 +84,9 @@ describe('Settings', () => {
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)
await getSettings(request, response)
expect(response.render.mock.calls[0][0]).toBe('settings')
expect(response.render.mock.calls[0][1].title).toBe('Settings')
expect(response.render.mock.calls[0][1].settings).toBe(response.locals.settings)
})
})