Skip to content
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

Unit test cases #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions javaScriptTraining/homeWork6(YouTube)/assets/js/HtmlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var HtmlHelper = (function () {

function HtmlHelper() {}
var apiHandler = new ApiHandler(),
paginationHelper = new PaginationHelper();
pagenationHelper = new PagenationHelper();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wthiss


HtmlHelper.prototype.initializePage = function () {
var _this = this,
Expand All @@ -17,24 +17,26 @@ var HtmlHelper = (function () {
event.preventDefault();
if (event.keyCode === 13) {
apiHandler.searchForVideos($searchBox.value).then(function (videos) {
paginationHelper.setCurrentPage(1);
pagenationHelper.setCurrentPage(1);
_this.renderGrid(videos);
_this.renderPagenation(videos);

});
}
});
$search.appendChild($searchBox);
document.body.appendChild($search);
window.addEventListener('resize', function () {
var videos = apiHandler.getVideos();
if (videos.length) {
paginationHelper.setCurrentPage(1);
if (videos && videos.length > 0) {
_this.renderGrid(videos);
_this.renderPagenation(videos)
}
});
}

HtmlHelper.prototype.renderPagenation = function (items) {
this.renderPageNumbers(paginationHelper.getPageCount(items, apiHandler));
this.renderPageNumbers(pagenationHelper.getPageCount(items, apiHandler));
}

HtmlHelper.prototype.clearGrid = function () {
Expand All @@ -58,13 +60,12 @@ var HtmlHelper = (function () {

HtmlHelper.prototype.renderPageNumbers = function (numberOfpages) {
var _this = this,
$paginationFragment = document.createElement('div'),
$anchor;
$paginationFragment = document.createElement('div');
$paginationFragment.setAttribute('id', 'pagination');
$paginationFragment.setAttribute('class', 'pagination-controls');
_this.clearPagination();
for (var i = 0; i < numberOfpages; i++) {
$anchor = document.createElement('a');
var $anchor = document.createElement('a');
$anchor.appendChild(document.createTextNode(i + 1));
$anchor.setAttribute('id', 'page' + (i + 1));
$anchor.setAttribute('href', '#');
Expand All @@ -83,49 +84,46 @@ var HtmlHelper = (function () {
}

function paginationClick(event) {
if (event.target.tagName.toLowerCase() === 'a') {
paginationHelper.setCurrentPage(event.target.text);
if (event.target.tagName.toLocaleLowerCase() === 'a') {
pagenationHelper.setCurrentPage(event.target.text);
HtmlHelper.prototype.renderGrid(apiHandler.getVideos());
HtmlHelper.prototype.setSelectedPageCss();
}
}

HtmlHelper.prototype.setSelectedPageCss = function () {
var $paginationElement = document.querySelector('#pagination'),
currentPage = paginationHelper.getCurrentPage(),
$selectedPagination = $paginationElement.querySelector('#page' + currentPage),
$previousActivePage;
currentPage = pagenationHelper.getCurrentPage(),
$selectedPagination = $paginationElement.querySelector('#page' + currentPage);
if (!$selectedPagination) {
currentPage = 1;
paginationHelper.setCurrentPage(currentPage);
pagenationHelper.setCurrentPage(currentPage);
$selectedPagination = $paginationElement.querySelector('#page' + currentPage);
}
$previousActivePage = $paginationElement.querySelector('.active');
var $previousActivePage = $paginationElement.querySelector('.active');
if ($previousActivePage) {
$previousActivePage.classList.remove('active');
}
if ($selectedPagination) {
$selectedPagination.classList.add('active');
}
$selectedPagination.classList.add('active');
}

HtmlHelper.prototype.renderGrid = function (videos) {
var _this = this,
$videosSection = document.createElement('div'),
itemCount = apiHandler.getVideosPerPage(),
startIndex = paginationHelper.getStartIndex(itemCount),
startIndex = itemCount + pagenationHelper.getStartIndex(itemCount),
range = startIndex + itemCount,
numberOfpages, $node;
numberOfpages;
$videosSection.setAttribute('id', 'youtube-container');
_this.clearGrid();
for (var i = startIndex; i < range; i++) {
if (videos[i]) {
$node = this.getVideoItem(videos[i], i);
var $node = this.getVideoItem(videos[i], i);
$videosSection.appendChild($node);
}
}
document.body.appendChild($videosSection);
numberOfpages = paginationHelper.getPageCount(videos, apiHandler.getVideosPerPage());
numberOfpages = pagenationHelper.getPageCount(videos, apiHandler);
_this.renderPageNumbers(numberOfpages);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var PagenationHelper = (function () {
'use strict';

function PagenationHelper() {};

PagenationHelper.prototype.getPageCount = function (items, apiHandler) {
var videosPerPage = apiHandler.getVideosPerPage(),
totalVideos = items.length,
pageCount = Math.floor(totalVideos / videosPerPage);
pageCount = pageCount + (totalVideos % videosPerPage === 0 ? 0 : 1);
return pageCount;
}

PagenationHelper.prototype.getCurrentPage = function () {
return this.currentPage || 1;
}

PagenationHelper.prototype.setCurrentPage = function (pageNumber) {
this.currentPage = pageNumber;
}

PagenationHelper.prototype.getStartIndex = function (noOfVideos) {
return (this.getCurrentPage() * noOfVideos) - noOfVideos
}
return PagenationHelper;
})();

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
describe('ApiHandler Module', function () {
describe('ApiHandler', function () {
var apiHandler;
beforeAll(function () {
apiHandler = new ApiHandler();
});
it('Test setter and getter for api handler expected behavior', function () {
it('first', function () {
apiHandler.setVideos([]);
expect(apiHandler.getVideos().length).toBe(0);
});
it('Test setter and getter for api handler un equality behavior', function () {
apiHandler.setVideos([]);
expect(apiHandler.getVideos().length).not.toBe(1);
});
it('Test url for API call', function () {
var queryParams = {
key: "token",
part: "part",
type: "type",
maxResults: "max",
q: "text"
},
url = apiHandler.getApiUrl(queryParams);
expect(url).toBe("https://www.googleapis.com/youtube/v3/search?key=token&part=part&type=type&maxResults=max&q=text");
});
})
})

This file was deleted.

2 changes: 1 addition & 1 deletion javaScriptTraining/homeWork6(YouTube)/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<p class="description"></p>
</div>
</template>
<script src="assets/js/PaginationHelper.js" type="text/javascript"></script>
<script src="assets/js/PagenationHelper.js" type="text/javascript"></script>
<script src="assets/js/ApiHandler.js" type="text/javascript"></script>
<script src="assets/js/HtmlHelper.js" type="text/javascript"></script>
<script src="assets/js/index.js" type="text/javascript"></script>
Expand Down
2 changes: 1 addition & 1 deletion javaScriptTraining/homeWork6(YouTube)/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function (config) {
files: [
"js/config.js",
"js/ApiHandler.js",
"js/PaginationHelper.js",
"js/PagenationHelper.js",
"js/HtmlHelper.js",
"js/index.js",
"specs/*.spec.js"],
Expand Down