define(['jquery', 'underscore', 'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers', 'URI', 'js/models/xblock_info', 'js/views/paged_container', 'js/views/paging_header', 'common/js/components/views/paging_footer', 'js/views/xblock'], function($, _, AjaxHelpers, URI, XBlockInfo, PagedContainer, PagingHeader, PagingFooter, XBlockView) { var htmlResponseTpl = _.template('' + '
Showing 1-3' + ' out of 4 total, ' + 'sorted by Date added descending
'); }); }); describe('Children count label', function() { it('should show correct count on first page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingHeader.$('.count-current-shown')).toHaveHtml('1-3'); }); it('should show correct count on second page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); expect(pagingContainer.pagingHeader.$('.count-current-shown')).toHaveHtml('4-4'); }); it('should show correct count for an empty collection', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingHeader.$('.count-current-shown')).toHaveHtml('0-0'); }); }); describe('Children total label', function() { it('should show correct total on the first page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingHeader.$('.count-total')).toHaveText('4 total'); }); it('should show correct total on the second page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); expect(pagingContainer.pagingHeader.$('.count-total')).toHaveText('4 total'); }); it('should show zero total for an empty collection', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingHeader.$('.count-total')).toHaveText('0 total'); }); }); }); describe('PagingFooter', function() { describe('Next page button', function() { beforeEach(function() { // Render the page and header so that they can react to events pagingContainer.render(); }); it('does not move forward if a server error occurs', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.next-page-link').click(); requests[1].respond(500); expect(pagingContainer.collection.currentPage).toBe(0); }); it('can move to the next page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.next-page-link').click(); respondWithMockPage(requests); expect(pagingContainer.collection.currentPage).toBe(1); }); it('should be enabled when there is at least one more page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.next-page-link')).not.toHaveClass('is-disabled'); }); it('should be disabled on the final page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.next-page-link')).toHaveClass('is-disabled'); }); it('should be disabled on an empty page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingFooter.$('.next-page-link')).toHaveClass('is-disabled'); }); }); describe('Previous page button', function() { beforeEach(function() { // Render the page and header so that they can react to events pagingContainer.render(); }); it('does not move back if a server error occurs', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.previous-page-link').click(); requests[1].respond(500); expect(pagingContainer.collection.currentPage).toBe(1); }); it('can go back a page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.previous-page-link').click(); respondWithMockPage(requests); expect(pagingContainer.collection.currentPage).toBe(0); }); it('should be disabled on the first page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.previous-page-link')).toHaveClass('is-disabled'); }); it('should be enabled on the second page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.previous-page-link')).not.toHaveClass('is-disabled'); }); it('should be disabled for an empty page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingFooter.$('.previous-page-link')).toHaveClass('is-disabled'); }); }); describe('Current page label', function() { it('should show 1 on the first page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.current-page')).toHaveText('1'); }); it('should show 2 on the second page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(1); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.current-page')).toHaveText('2'); }); it('should show 1 for an empty collection', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingFooter.$('.current-page')).toHaveText('1'); }); }); describe('Page total label', function() { it('should show the correct value with more than one page', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.total-pages')).toHaveText('2'); }); it('should show page 1 when there are no assets', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); AjaxHelpers.respondWithJson(requests, mockEmptyPage); expect(pagingContainer.pagingFooter.$('.total-pages')).toHaveText('1'); }); }); describe('Page input field', function() { var input; it('should initially have a blank page input', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); expect(pagingContainer.pagingFooter.$('.page-number-input')).toHaveValue(''); }); it('should handle invalid page requests', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.page-number-input').val('abc'); pagingContainer.pagingFooter.$('.page-number-input').trigger('change'); expect(pagingContainer.collection.currentPage).toBe(0); expect(pagingContainer.pagingFooter.$('.page-number-input')).toHaveValue(''); }); it('should switch pages via the input field', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.page-number-input').val('2'); pagingContainer.pagingFooter.$('.page-number-input').trigger('change'); AjaxHelpers.respondWithJson(requests, mockSecondPage); expect(pagingContainer.collection.currentPage).toBe(1); expect(pagingContainer.pagingFooter.$('.page-number-input')).toHaveValue(''); }); it('should handle AJAX failures when switching pages via the input field', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); pagingContainer.pagingFooter.$('.page-number-input').val('2'); pagingContainer.pagingFooter.$('.page-number-input').trigger('change'); requests[1].respond(500); expect(pagingContainer.collection.currentPage).toBe(0); expect(pagingContainer.pagingFooter.$('.page-number-input')).toHaveValue(''); }); }); }); describe('Previews', function() { describe('Toggle Previews', function() { var testSendsAjax, defaultUrl = '/preview/xblock/handler/trigger_previews'; testSendsAjax = function(show_previews) { it('should send ' + (!show_previews) + ' when showChildrenPreviews was ' + show_previews, function() { var requests = AjaxHelpers.requests(this); pagingContainer.collection.showChildrenPreviews = show_previews; pagingContainer.togglePreviews(); AjaxHelpers.expectJsonRequest(requests, 'POST', defaultUrl, {showChildrenPreviews: !show_previews}); AjaxHelpers.respondWithJson(requests, {showChildrenPreviews: !show_previews}); }); }; testSendsAjax(true); testSendsAjax(false); it('should trigger render on success', function() { spyOn(pagingContainer, 'render'); var requests = AjaxHelpers.requests(this); pagingContainer.togglePreviews(); AjaxHelpers.respondWithJson(requests, {showChildrenPreviews: true}); expect(pagingContainer.render).toHaveBeenCalled(); }); it('should not trigger render on failure', function() { spyOn(pagingContainer, 'render'); var requests = AjaxHelpers.requests(this); pagingContainer.togglePreviews(); AjaxHelpers.respondWithError(requests); expect(pagingContainer.render).not.toHaveBeenCalled(); }); it('should send force_render when new block causes page change', function() { var requests = AjaxHelpers.requests(this); pagingContainer.setPage(0); respondWithMockPage(requests); spyOn(pagingContainer, 'render'); var mockXBlockInfo = new XBlockInfo({id: 'mock-location'}); var mockXBlockView = new XBlockView({model: mockXBlockInfo}); mockXBlockView.model.id = 'mock-location'; pagingContainer.refresh(mockXBlockView, true); expect(pagingContainer.render).toHaveBeenCalled(); expect(pagingContainer.render.calls.mostRecent().args[0].force_render).toEqual('mock-location'); }); }); }); }); });