diff --git a/.codeclimate.yml b/.codeclimate.yml index 097b0d5..d39e7f9 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -63,5 +63,6 @@ ratings: - "**.module" - "**.inc" exclude_paths: -- js/**/* +- js/build/**/* +- js/tests/**/* - lib/classes/**/* diff --git a/Gruntfile.js b/Gruntfile.js index 62eacdd..997c9ac 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -33,7 +33,7 @@ module.exports = function(grunt) { // There are basically three phases of building the production theme: // 0) Testing / linting - grunt.registerTask('test', ['jshint']); + grunt.registerTask('test', ['jshint', 'jasmine']); // 1) Javascript preparation (concatenating and uglifying scripts) grunt.registerTask('javascript', ['uglify']); // 2) Stylesheet preparation (SASS, autoprefixing, and minification) diff --git a/js/scrollPosts.js b/js/scrollPosts.js index 3e3b60e..26b5a52 100644 --- a/js/scrollPosts.js +++ b/js/scrollPosts.js @@ -12,22 +12,18 @@ $j(function(){ $j("#postContainer").load("/news/test/?offset="+offset); $j("#another").click(function(){ - + offset = offset+10; - + $j("#postContainer") .slideUp() .load("/news/test/?offset="+offset, function() { $j("#mitContainer").css("display", "none"); - $j(this).slideDown(); - $j( "#postContainer" ).scrollTop( 300 ); + $j(this).slideDown(); + $j( "#postContainer" ).scrollTop( 300 ); }); - + return false; }); }); - - - - diff --git a/js/mitlibnews-more.js b/js/src/mitlibnews-more.js similarity index 88% rename from js/mitlibnews-more.js rename to js/src/mitlibnews-more.js index 0546fc1..88b443b 100644 --- a/js/mitlibnews-more.js +++ b/js/src/mitlibnews-more.js @@ -6,7 +6,6 @@ * JSON objects into rendered HTML. * */ - var window, document, jQuery; window.mitlibnews = window.mitlibnews || {}; @@ -17,7 +16,6 @@ window.mitlibnews.loader = { * Properties */ container : '', - offset: 0, page: 1, postcontent: '', @@ -25,30 +23,24 @@ window.mitlibnews.loader = { * Initialize */ initialize : function() { - console.log('MIT Libraries News Loader initializing...'); // Identify container that will receive post cards. this.container = document.getElementById('mitlibnews-container'); // The type of query is identified by data attribute on the container. this.setPostcontent(); - console.log('Initialization complete.'); + return true; }, /** * Simple Post Loader */ loadPosts : function(posts_per_page) { - console.log('Loading ' + posts_per_page + ' posts'); - console.log('Query: '); var query = this.buildQuery(posts_per_page); - console.log(query); jQuery.ajax({ url: '/news/wp-json/posts', data: query, dataType: 'json', type: 'GET', success: function(data) { - console.log(posts_per_page + ' posts requested'); - console.log(data.length + ' posts received'); if ( data.length < posts_per_page) { window.mitlibnews.loader.hideMore(); } @@ -58,34 +50,6 @@ window.mitlibnews.loader = { jQuery(target).append( window.mitlibnews.loader.renderCard(value) ); }); window.mitlibnews.loader.setPage( window.mitlibnews.loader.getPage() + 1); - console.log('New page: '); - console.log(window.mitlibnews.loader.getPage()); - - }, - error: function() { - console.log("Error"); - } - }); - console.log(''); - }, - - /** - * Load More - Archive - */ - loadArchive : function() { - console.log('Loading Archive posts...'); - jQuery.ajax({ - url: '/news/wp-json/posts', - data: { - filter: { - 'posts_per_page': 9, - 'offset': 10, - } - }, - dataType: 'json', - type: 'GET', - success: function(data) { - console.log(data); }, error: function() { console.log("Error"); @@ -317,14 +281,6 @@ window.mitlibnews.loader = { return document.getElementById('mitlibnews-container'); }, - /** - * Get Offset - */ - getOffset : function() { - console.log("Retrieving offset value of " + this.offset); - return this.offset; - }, - /** * Get Page */ @@ -339,32 +295,21 @@ window.mitlibnews.loader = { return this.postcontent; }, - /** - * Set Offset - */ - setOffset : function(value) { - console.log("Replacing offset value of " + this.offset + " with " + value); - this.offset = value; - return this.getOffset(); - }, - /** * Set Page */ setPage : function(value) { - console.log("Replacing page value of " + this.page + " with " + value); this.page = value; }, /** * Set Post Content */ - setPostcontent : function(value) { + setPostcontent : function() { // The default value is 'all'. this.postcontent = 'all'; // If everything is not set, just use the default. if ( !this.container || !this.container.dataset || !this.container.dataset.postcontent ) { - console.log( 'Post content attribute not found! Using default value.' ); return true; } // If we're still here, use the real value. diff --git a/js/tests/mitlibnews-more.spec.js b/js/tests/mitlibnews-more.spec.js new file mode 100644 index 0000000..e6c5684 --- /dev/null +++ b/js/tests/mitlibnews-more.spec.js @@ -0,0 +1,38 @@ +var window; + +describe("Additional Posts suite", function() { + it("should initialize", function() { + var demo; + demo = window.mitlibnews.loader; + expect(demo.initialize()).toEqual(true); + // Default values + expect(demo.getPage()).toEqual(1); + expect(demo.getPostcontent()).toEqual('all'); + }); + + it("should be able to get and set pagination", function() { + var demo; + demo = window.mitlibnews.loader; + demo.setPage(2); + expect(demo.getPage()).toEqual(2); + }); + + it("should be able to build a query", function() { + var demo, reference; + demo = window.mitlibnews.loader; + reference = { + page: 2, + filter: { + posts_per_page: 12 + } + }; + expect(demo.buildQuery(12)).toEqual(reference); + reference = { + page: 2, + filter: { + posts_per_page: 9 + } + }; + expect(demo.buildQuery(9)).toEqual(reference); + }); +}); diff --git a/package.json b/package.json index 41b63a9..2878673 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "devDependencies": { "glob": "^7.0.5", "grunt": "^1.0.1", + "grunt-contrib-jasmine": "^1.0.3", "grunt-contrib-jshint": "^1.0.0", "grunt-contrib-uglify": "^2.0.0", "grunt-gitinfo": "^0.1.8", diff --git a/tasks/options/jasmine.js b/tasks/options/jasmine.js new file mode 100644 index 0000000..5c4f360 --- /dev/null +++ b/tasks/options/jasmine.js @@ -0,0 +1,6 @@ +module.exports = { + src: 'js/src/**/*.js', + options: { + specs: 'js/tests/**/*.js' + } +} diff --git a/tasks/options/jshint.js b/tasks/options/jshint.js index d098c68..014bbf1 100644 --- a/tasks/options/jshint.js +++ b/tasks/options/jshint.js @@ -2,7 +2,8 @@ module.exports = { all: [ 'Gruntfile.js', 'js/mitlibnews.js', - 'js/mitlibnews-more.js' + 'js/src/**/*.js', + 'js/tests/**/*.js' ], options: { jshintrc: '.jshintrc' diff --git a/tasks/options/uglify.js b/tasks/options/uglify.js index 9c432a1..3208efa 100644 --- a/tasks/options/uglify.js +++ b/tasks/options/uglify.js @@ -4,7 +4,7 @@ module.exports = { dest: 'js/build/mitlibnews.min.js' }, loadMore: { - src: 'js/mitlibnews-more.js', + src: 'js/src/mitlibnews-more.js', dest: 'js/build/mitlibnews-more.min.js' }, lazyLoad: {