diff --git a/cypress/integration/code-highlighting.spec.js b/cypress/integration/code-highlighting.spec.js index c1b7e4dd..691ee6fd 100644 --- a/cypress/integration/code-highlighting.spec.js +++ b/cypress/integration/code-highlighting.spec.js @@ -1,102 +1,104 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ const origin = 'https://github.com/memononen/libtess2'; const contentPath = 'Source/tess.h'; const lineStart = 32; const lineEnd = 42; const $ = Cypress.$; let url; describe('Code highlighting tests', function() { before(function() { - cy.visit('/').window().then(win => { - url = win.Urls.browse_origin_content(origin, contentPath); - }); + url = this.Urls.browse_origin_content(origin, contentPath); }); it('should highlight source code and add line numbers', function() { cy.visit(url); cy.get('.hljs-ln-numbers').then(lnNumbers => { cy.get('.hljs-ln-code').then(lnCode => { assert.equal(lnNumbers.length, lnCode.length); }); }); }); it('should emphasize source code lines based on url fragment', function() { cy.visit(`${url}/#L${lineStart}-L${lineEnd}`); cy.get('.hljs-ln-line').then(lines => { for (let line of lines) { - let lineNumber = parseInt($(line).data('line-number')); + let lineElt = $(line); + let lineNumber = parseInt(lineElt.data('line-number')); if (lineNumber >= lineStart && lineNumber <= lineEnd) { - assert.notEqual($(line).css('background-color'), 'rgba(0, 0, 0, 0)'); + assert.notEqual(lineElt.css('background-color'), 'rgba(0, 0, 0, 0)'); } else { - assert.equal($(line).css('background-color'), 'rgba(0, 0, 0, 0)'); + assert.equal(lineElt.css('background-color'), 'rgba(0, 0, 0, 0)'); } } }); }); it('should emphasize a line by clicking on its number', function() { cy.visit(url); cy.get('.hljs-ln-numbers').then(lnNumbers => { let lnNumber = lnNumbers[Math.floor(Math.random() * lnNumbers.length)]; - assert.equal($(lnNumber).css('background-color'), 'rgba(0, 0, 0, 0)'); - let line = parseInt($(lnNumber).data('line-number')); + let lnNumberElt = $(lnNumber); + assert.equal(lnNumberElt.css('background-color'), 'rgba(0, 0, 0, 0)'); + let line = parseInt(lnNumberElt.data('line-number')); cy.get(`.hljs-ln-numbers[data-line-number="${line}"]`) .scrollIntoView() .click() .then(() => { - assert.notEqual($(lnNumber).css('background-color'), 'rgba(0, 0, 0, 0)'); + assert.notEqual(lnNumberElt.css('background-color'), 'rgba(0, 0, 0, 0)'); }); }); }); it('should emphasize a range of lines by clicking on two line numbers and holding shift', function() { cy.visit(url); cy.get(`.hljs-ln-numbers[data-line-number="${lineStart}"]`) .scrollIntoView() .click() .get(`body`) .type(`{shift}`, { release: false }) .get(`.hljs-ln-numbers[data-line-number="${lineEnd}"]`) .scrollIntoView() .click() .get('.hljs-ln-line') .then(lines => { for (let line of lines) { - let lineNumber = parseInt($(line).data('line-number')); + let lineElt = $(line); + let lineNumber = parseInt(lineElt.data('line-number')); if (lineNumber >= lineStart && lineNumber <= lineEnd) { - assert.notEqual($(line).css('background-color'), 'rgba(0, 0, 0, 0)'); + assert.notEqual(lineElt.css('background-color'), 'rgba(0, 0, 0, 0)'); } else { - assert.equal($(line).css('background-color'), 'rgba(0, 0, 0, 0)'); + assert.equal(lineElt.css('background-color'), 'rgba(0, 0, 0, 0)'); } } }); }); it('should remove emphasized lines when clicking anywhere in code', function() { cy.visit(`${url}/#L${lineStart}-L${lineEnd}`); cy.get(`.hljs-ln-code[data-line-number="1"]`) .scrollIntoView() .click() .get('.hljs-ln-line') .then(lines => { for (let line of lines) { - assert.equal($(line).css('background-color'), 'rgba(0, 0, 0, 0)'); + let lineElt = $(line); + assert.equal(lineElt.css('background-color'), 'rgba(0, 0, 0, 0)'); } }); }); }); diff --git a/cypress/integration/content-display.spec.js b/cypress/integration/content-display.spec.js index bd694739..293fc429 100644 --- a/cypress/integration/content-display.spec.js +++ b/cypress/integration/content-display.spec.js @@ -1,97 +1,91 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ const origin = 'https://github.com/memononen/libtess2'; const contentPath = 'Source/tess.h'; let fileName, filePath, sha1git, rawFilePath, numberLines, originUrl; -let Urls, url; +let url; describe('Test File Rendering', function() { before(function() { + url = this.Urls.browse_origin_content(origin, contentPath); - cy.visit('/').window().then(win => { - Urls = win.Urls; - }).then(() => { - - url = Urls.browse_origin_content(origin, contentPath); - - cy.visit(url); - cy.window().then(async win => { - const metadata = win.swh.webapp.getBrowsedSwhObjectMetadata(); - - fileName = metadata.filename; - filePath = metadata.path; - originUrl = metadata['origin url']; - sha1git = metadata.sha1_git; - rawFilePath = Urls.browse_content_raw(`sha1_git:${sha1git}`) + - `?filename=${encodeURIComponent(fileName)}`; - - cy.request(rawFilePath) - .then((response) => { - const fileText = response.body; - const fileLines = fileText.split('\n'); - numberLines = fileLines.length; - - // If last line is empty its not shown - if (!fileLines[numberLines - 1]) numberLines -= 1; - }); - }); + cy.visit(url); + cy.window().then(async win => { + const metadata = win.swh.webapp.getBrowsedSwhObjectMetadata(); + + fileName = metadata.filename; + filePath = metadata.path; + originUrl = metadata['origin url']; + sha1git = metadata.sha1_git; + rawFilePath = this.Urls.browse_content_raw(`sha1_git:${sha1git}`) + + `?filename=${encodeURIComponent(fileName)}`; + + cy.request(rawFilePath) + .then((response) => { + const fileText = response.body; + const fileLines = fileText.split('\n'); + numberLines = fileLines.length; + + // If last line is empty its not shown + if (!fileLines[numberLines - 1]) numberLines -= 1; + }); }); }); beforeEach(function() { cy.visit(url); }); it('should display correct file name', function() { cy.get('.swh-content-filename') .should('be.visible') .and('contain', fileName) .and('have.css', 'background-color', 'rgb(242, 244, 245)'); }); it('should display all lines', function() { cy.get('.hljs-ln-code') .should('have.length', numberLines) .and('be.visible') .and('have.css', 'background-color', 'rgba(0, 0, 0, 0)'); }); it('should show correct path', function() { // Array containing names of all the ancestor directories of the file const filePathArr = filePath.slice(1, -1).slice('/'); filePathArr.split('/').forEach(dirName => { cy.get('.swh-browse-bread-crumbs') .should('contain', dirName); }); }); it('should have links to all ancestor directories', function() { - const rootDirUrl = Urls.browse_origin_directory(originUrl); + const rootDirUrl = this.Urls.browse_origin_directory(originUrl); cy.get(`a[href='${rootDirUrl}']`) .should('be.visible'); let splittedPath = filePath.split('/'); for (let i = 2; i < splittedPath.length; ++i) { const subDirPath = splittedPath.slice(1, i).join('/'); - const subDirUrl = Urls.browse_origin_directory(originUrl, subDirPath); + const subDirUrl = this.Urls.browse_origin_directory(originUrl, subDirPath); cy.get(`a[href='${subDirUrl}']`) .should('be.visible'); } }); it('should have correct url to raw file', function() { cy.get(`a[href='${rawFilePath}']`) .should('be.visible'); }); }); diff --git a/cypress/integration/directory.spec.js b/cypress/integration/directory.spec.js index 79884b18..79badd53 100644 --- a/cypress/integration/directory.spec.js +++ b/cypress/integration/directory.spec.js @@ -1,83 +1,79 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ import {httpGetJson} from '../utils'; const origin = 'https://github.com/memononen/libtess2'; const $ = Cypress.$; -let Urls, url, dirs, files; +let url, dirs, files; describe('Directory Tests', function() { before(function() { - cy.visit('/').window().then(win => { - Urls = win.Urls; - url = Urls.browse_origin_directory(origin); - }).then(() => { - cy.visit(url).window().then(async win => { - const metadata = win.swh.webapp.getBrowsedSwhObjectMetadata(); - const apiUrl = Cypress.config().baseUrl + Urls.api_1_directory(metadata.directory); - let dirContent = await httpGetJson(apiUrl); - files = []; - dirs = []; - for (let entry of dirContent) { - if (entry.type === 'file') { - files.push(entry); - } else { - dirs.push(entry); - } + url = this.Urls.browse_origin_directory(origin); + cy.visit(url).window().then(async win => { + const metadata = win.swh.webapp.getBrowsedSwhObjectMetadata(); + const apiUrl = Cypress.config().baseUrl + this.Urls.api_1_directory(metadata.directory); + let dirContent = await httpGetJson(apiUrl); + files = []; + dirs = []; + for (let entry of dirContent) { + if (entry.type === 'file') { + files.push(entry); + } else { + dirs.push(entry); } - }); + } }); }); beforeEach(function() { cy.visit(url); }); it('should display all files and directories', function() { cy.get('.swh-directory') .should('have.length', dirs.length) .and('be.visible'); cy.get('.swh-content') .should('have.length', files.length) .and('be.visible'); }); it('should display sizes for files', function() { cy.get('.swh-content') .parent('tr') .then((rows) => { for (let row of rows) { let text = $(row).children('td').eq(2).text(); expect(text.trim()).to.not.be.empty; } }); }); it('should display readme when it is present', function() { cy.get('#readme-panel > .card-body') .should('be.visible') .and('have.class', 'swh-showdown') .and('not.be.empty') .and('not.contain', 'Readme bytes are not available'); }); it('should open subdirectory when clicked', function() { cy.get('.swh-directory') .first() .children('a') .click(); cy.url() .should('include', url + dirs[0]['name']); cy.get('.swh-directory-table') .should('be.visible'); }); }); diff --git a/cypress/support/index.js b/cypress/support/index.js index 1d5c6baf..d1298e22 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -1,10 +1,16 @@ /** * Copyright (C) 2019 The Software Heritage developers * See the AUTHORS file at the top-level directory of this distribution * License: GNU Affero General Public License version 3, or any later version * See top-level LICENSE file for more information */ Cypress.Screenshot.defaults({ screenshotOnRunFailure: false }); + +before(function() { + cy.visit('/').window().then(win => { + this.Urls = win.Urls; + }); +});