diff --git a/cypress/integration/language-select.spec.js b/cypress/integration/language-select.spec.js new file mode 100644 index 00000000..a526a0e9 --- /dev/null +++ b/cypress/integration/language-select.spec.js @@ -0,0 +1,90 @@ +/** + * 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 {random, checkLanguageHighlighting} from '../utils'; + +const origin = { + url: 'https://github.com/memononen/libtess2', + content: { + path: 'premake4.lua' + } +}; + +let contentWithLanguageInfo, contentWithoutLanguageInfo; + +let languages = []; + +const $ = Cypress.$; + +describe('Test Content Language Select', function() { + before(function() { + contentWithLanguageInfo = this.Urls.browse_origin_content(origin.url, origin.content.path); + + cy.visit(contentWithLanguageInfo); + cy.window().then(win => { + const metadata = win.swh.webapp.getBrowsedSwhObjectMetadata(); + + origin.content.name = metadata.filename; + origin.content.sha1git = metadata.sha1_git; + contentWithoutLanguageInfo = this.Urls.browse_content(`sha1_git:${origin.content.sha1git}`); + $('.language-select option').each(function() { + languages.push(this.value); + }); + }); + }); + + context('When Language is detected', function() { + it('should display correct language in dropdown', function() { + cy.visit(contentWithLanguageInfo) + .then(() => { + cy.get(`code.${$('.language-select').val()}`) + .should('exist'); + }); + }); + }); + + context('When Language is not detected', function() { + it('should have no selected language in dropdown', function() { + cy.visit(contentWithoutLanguageInfo).then(() => { + assert.strictEqual($('.language-select').val(), null); + }); + }); + }); + + context('When language is switched from dropdown', function() { + before(function() { + cy.visit(contentWithLanguageInfo); + + let languageSelect = languages[random(0, languages.length)]; + + cy.get('.chosen-container') + .click() + .get('.chosen-results > li') + .contains(languageSelect) + .click() + .then(() => { + assert.strictEqual($('.language-select').val(), languageSelect); + }); + }); + + it('should contain argument with language in url', function() { + cy.location('search') + .should('contain', `language=${$('.language-select').val()}`); + }); + + it('should highlight according to new language', function() { + checkLanguageHighlighting($('.language-select').val()); + }); + }); + + it('should highlight according to the language passed as argument in url', function() { + const languageSelect = languages[random(0, languages.length)]; + cy.visit(`${contentWithLanguageInfo}?language=${languageSelect}`); + checkLanguageHighlighting(languageSelect); + }); + +}); diff --git a/cypress/utils/index.js b/cypress/utils/index.js index 34df946d..cb8cf7f0 100644 --- a/cypress/utils/index.js +++ b/cypress/utils/index.js @@ -1,47 +1,52 @@ /** * 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 axios from 'axios'; export async function httpGetJson(url) { const response = await axios.get(url); return response.data; } /** * Converts string with Time information * to an object with Time information */ export function getTime(text) { const date = new Date(text); function pad(n) { return n < 10 ? '0' + n : n; } const time = { date: date.getUTCDate(), month: date.getUTCMonth(), monthName: date.toLocaleString('en', { month: 'long' }), year: date.getUTCFullYear(), hours: pad(date.getUTCHours()), minutes: pad(date.getUTCMinutes()) }; return time; } export function checkLanguageHighlighting(language) { cy.get('code') .should('be.visible') .and('have.class', 'hljs') .and('have.class', language) .and('not.be.empty') .find('table.hljs-ln') .should('be.visible') .and('not.be.empty'); } + +export function random(start, end) { + const range = end - start; + return Math.floor(Math.random() * range) + start; +}