-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit test and integration test #34
base: master
Are you sure you want to change the base?
Changes from all commits
7067b6b
fd190a8
836c7b1
f135c5a
48069b3
89d2d14
426f26b
41b4852
c33d743
d623e18
e771faf
8aada0a
2c3ce0e
5eca78f
45ef97f
1d46cbe
33dc103
99ead7d
81c4929
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true | ||
max_line_length = 80 | ||
trim_trailing_whitespace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
node_modules | ||
coverage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
baseUrl: 'http://localhost:3000', | ||
gridUrl: 'http://0.0.0.0:4444/wd/hub', | ||
compositeImage: true, | ||
|
||
browsers: { | ||
chrome: { | ||
desiredCapabilities: { | ||
browserName: 'chrome' | ||
} | ||
}, | ||
firefox: { | ||
desiredCapabilities: { | ||
browserName: 'firefox' | ||
} | ||
} | ||
}, | ||
plugins: { | ||
'html-reporter/hermione': { | ||
path: 'hermione/hermione-html-report' | ||
}, | ||
[path.resolve(__dirname, './hermione/custom-commands/index.js')]: true | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. круто, что получилось разобраться с библиотекой sinon.js |
||
|
||
const controller = require('../contentController'); | ||
|
||
describe('Контроллер содержимого файла коммита', () => { | ||
|
||
const request = { params: { hash: '', '0': '' } }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чтобы создавать заглушки для запроса и ответа express, удобно использовать готовые библиотеки вроде https://www.npmjs.com/package/sinon-express-mock |
||
const response = { render: sinon.spy() }; | ||
const stubs = {}; | ||
stubs.gitFileTree = sinon.stub(); | ||
stubs.gitFileTree.returns(Promise.resolve([ | ||
{ type: '', hash: '', path: '' } | ||
])); | ||
stubs.gitFileContent = sinon.stub(); | ||
stubs.buildBreadcrumbs = sinon.stub(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. эти переменные - общие для всех тестов на контроллер |
||
|
||
it('Используется представление содержимого файла коммита', async () => { | ||
await controller(request, response, () => {}, stubs); | ||
|
||
const view = response.render.getCall(0).args[0]; | ||
|
||
expect(view).to.be.equal('content'); | ||
}); | ||
|
||
it('В представление прокидывается набор данных, состоящий из заголовка, хлебных крошек и содержимого файла коммита коммита', async () => { | ||
await controller(request, response, () => {}, stubs); | ||
|
||
const params = response.render.getCall(1).args[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. если запустить этот тест отдельно от первого, то он упадет |
||
|
||
expect(params).to.have.all.keys( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не проверяется контент полученного объекта |
||
'title', | ||
'breadcrumbs', | ||
'content', | ||
); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const controller = require('../filesController'); | ||
|
||
describe('Контроллер файловой системы коммита', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь те же ошибки, что и в предыдущем файле |
||
|
||
const request = { params: { hash: '' } }; | ||
const response = { render: sinon.spy() }; | ||
const stubs = {}; | ||
stubs.gitFileTree = sinon.stub(); | ||
stubs.gitFileTree.returns(Promise.resolve([ | ||
{ type: '', hash: '', path: '' } | ||
])); | ||
stubs.buildObjectUrl = sinon.stub(); | ||
stubs.buildBreadcrumbs = sinon.stub(); | ||
|
||
it('Подготовка файловой системы коммита перед отправкой в представление', async () => { | ||
await controller(request, response, () => {}, stubs); | ||
|
||
const files = response.render.getCall(0).args[1].files; | ||
|
||
expect(files[0]).to.have.any.keys('href', 'name'); | ||
}); | ||
|
||
it('Используется представление содержимого файла коммита', async () => { | ||
await controller(request, response, () => {}, stubs); | ||
|
||
const view = response.render.getCall(1).args[0]; | ||
|
||
expect(view).to.be.equal('files'); | ||
}); | ||
|
||
it('В представление прокидывается набор данных, состоящий из заголовка, хлебных крошек и файловой системы коммита', async () => { | ||
await controller(request, response, () => {}, stubs); | ||
|
||
const params = response.render.getCall(2).args[1]; | ||
|
||
expect(params).to.have.all.keys( | ||
'title', | ||
'breadcrumbs', | ||
'files', | ||
); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const controller = require('../indexController'); | ||
|
||
describe('Контроллер истории коммитов', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь те же ошибки, что и в предыдущем файле |
||
|
||
const response = { render: sinon.spy() }; | ||
const stubs = {}; | ||
stubs.gitHistory = sinon.stub(); | ||
stubs.gitHistory.returns(Promise.resolve([ | ||
{ hash: '', author: '', timestamp: '', msg: '' } | ||
])); | ||
stubs.buildFolderUrl = sinon.stub(); | ||
stubs.buildBreadcrumbs = sinon.stub(); | ||
|
||
it('Подготовка истории коммитов перед отправкой в представление', async () => { | ||
await controller(null, response, () => {}, stubs); | ||
|
||
const list = response.render.getCall(0).args[1].list; | ||
|
||
expect(list[0]).to.have.any.keys('href'); | ||
}); | ||
|
||
it('Используется представление истории коммитов', async () => { | ||
await controller(null, response, () => {}, stubs); | ||
|
||
const view = response.render.getCall(1).args[0]; | ||
|
||
expect(view).to.be.equal('index'); | ||
}); | ||
|
||
it('В представление прокидывается набор данных, состоящий из заголовка, хлебных крошек и списка истории коммитов', async () => { | ||
await controller(null, response, () => {}, stubs); | ||
|
||
const params = response.render.getCall(2).args[1]; | ||
|
||
expect(params).to.have.all.keys( | ||
'title', | ||
'breadcrumbs', | ||
'list', | ||
); | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
const { gitFileContent, gitFileTree } = require('../utils/git'); | ||
const { buildFolderUrl, buildBreadcrumbs } = require('../utils/navigation'); | ||
const { buildBreadcrumbs } = require('../utils/navigation'); | ||
|
||
module.exports = async function(req, res, next, ...stubs) { | ||
const _stubs = (stubs && stubs[0]) || {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. некрасивый костыль - заглушки передаются как массив, хотя это один объект |
||
const _gitFileTree = _stubs.gitFileTree || gitFileTree; | ||
const _gitFileContent = _stubs.gitFileContent || gitFileContent; | ||
const _buildBreadcrumbs = _stubs.buildBreadcrumbs || buildBreadcrumbs; | ||
|
||
module.exports = function(req, res, next) { | ||
const { hash } = req.params; | ||
const path = req.params[0].split('/').filter(Boolean); | ||
const path = req.params[0].split('/').filter(Boolean).join('/'); | ||
|
||
const content = await _gitFileTree(hash, path).then( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь смешиваются промисы и async/await There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. еще, кажется, если при получении контента произойдет 500 ошибка, то все равно вызовется There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. кстати, на ошибку при получении контента из git тоже можно было написать тест |
||
([file]) => _gitFileContent(file.hash), | ||
/* istanbul ignore next */ | ||
err => next(err) | ||
); | ||
|
||
gitFileTree(hash, path.join('/')) | ||
.then(function([file]) { | ||
if (file && file.type === 'blob') { | ||
return gitFileContent(file.hash); | ||
} | ||
}) | ||
.then( | ||
content => { | ||
if (content) { | ||
res.render('content', { | ||
title: 'content', | ||
breadcrumbs: buildBreadcrumbs(hash, path.join('/')), | ||
content | ||
}); | ||
} else { | ||
next(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. кажется, во время рефакторинга сломалась 404 ошибка, если запросить несуществующий путь |
||
} | ||
}, | ||
err => next(err) | ||
); | ||
res.render('content', { | ||
title: 'content', | ||
breadcrumbs: _buildBreadcrumbs(hash, path), | ||
content | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ const { | |
buildBreadcrumbs | ||
} = require('../utils/navigation'); | ||
|
||
/* istanbul ignore next */ | ||
function buildObjectUrl(parentHash, { path, type }) { | ||
switch (type) { | ||
case 'tree': | ||
|
@@ -16,26 +17,32 @@ function buildObjectUrl(parentHash, { path, type }) { | |
} | ||
} | ||
|
||
module.exports = function(req, res, next) { | ||
module.exports = async function(req, res, next, ...stubs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. те же ошибки, что и в других контроллерах |
||
const _stubs = (stubs && stubs[0]) || {}; | ||
const _gitFileTree = _stubs.gitFileTree || gitFileTree; | ||
const _buildObjectUrl = _stubs.buildObjectUrl || buildObjectUrl; | ||
const _buildBreadcrumbs = _stubs.buildBreadcrumbs || buildBreadcrumbs; | ||
|
||
const { hash } = req.params; | ||
const pathParam = (req.params[0] || '').split('/').filter(Boolean); | ||
|
||
const path = pathParam.length ? pathParam.join('/') + '/' : ''; | ||
|
||
return gitFileTree(hash, path).then( | ||
const files = await _gitFileTree(hash, path).then( | ||
list => { | ||
const files = list.map(item => ({ | ||
return list.map(item => ({ | ||
...item, | ||
href: buildObjectUrl(hash, item), | ||
href: _buildObjectUrl(hash, item), | ||
name: item.path.split('/').pop() | ||
})); | ||
|
||
res.render('files', { | ||
title: 'files', | ||
breadcrumbs: buildBreadcrumbs(hash, pathParam.join('/')), | ||
files | ||
}); | ||
}, | ||
/* istanbul ignore next */ | ||
err => next(err) | ||
); | ||
|
||
res.render('files', { | ||
title: 'files', | ||
breadcrumbs: _buildBreadcrumbs(hash, pathParam.join('/')), | ||
files | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
const { gitHistory } = require('../utils/git'); | ||
const { buildFolderUrl, buildBreadcrumbs } = require('../utils/navigation'); | ||
|
||
module.exports = function(req, res) { | ||
gitHistory(1, 20).then( | ||
module.exports = async function(req, res, next, ...stubs) { | ||
const _stubs = (stubs && stubs[0]) || {}; | ||
const _gitHistory = _stubs.gitHistory || gitHistory; | ||
const _buildFolderUrl = _stubs.buildFolderUrl || buildFolderUrl; | ||
const _buildBreadcrumbs = _stubs.buildBreadcrumbs || buildBreadcrumbs; | ||
|
||
const list = await _gitHistory(1, 20).then( | ||
history => { | ||
const list = history.map(item => ({ | ||
return history.map(item => ({ | ||
...item, | ||
href: buildFolderUrl(item.hash, '') | ||
href: _buildFolderUrl(item.hash, '') | ||
})); | ||
|
||
res.render('index', { | ||
title: 'history', | ||
breadcrumbs: buildBreadcrumbs(), | ||
list | ||
}); | ||
}, | ||
/* istanbul ignore next */ | ||
err => next(err) | ||
); | ||
|
||
res.render('index', { | ||
title: 'history', | ||
breadcrumbs: _buildBreadcrumbs(), | ||
list | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hermione-html-report |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const assert = require('assert'); | ||
|
||
module.exports = (hermione, opts) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. круто, что получилось написать плагин для гермионы |
||
hermione.on(hermione.events.NEW_BROWSER, browser => { | ||
browser.addCommand('assertNavigation', (selector, expectSelector) => { | ||
return browser | ||
.isExisting(selector) | ||
.then(exists => { | ||
assert.ok(exists, 'Ссылка для перехода не найдена'); | ||
}) | ||
.click(selector) | ||
.isExisting(expectSelector) | ||
.then(exists => { | ||
assert.ok(exists, 'Переход по ссылке происходит некорректно'); | ||
}); | ||
}); | ||
}); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
удобнее было настроить в package.json обращение к собственному плагину как к пакету npm
https://medium.com/@the1mills/how-to-test-your-npm-module-without-publishing-it-every-5-minutes-1c4cb4b369be