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

src/components/profile: add components for displaying links to questionnaires in the Profile section #573

Merged
merged 7 commits into from
Sep 25, 2024
Merged
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
128 changes: 128 additions & 0 deletions src/components/__tests__/ProfileQuestionnaires.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { colors } from 'quasar';
import ProfileQuestionnaires from 'components/profile/ProfileQuestionnaires.vue';
import { i18n } from '../../boot/i18n';
import {
failOnStatusCode,
httpSuccessfullStatus,
httpTooManyRequestsStatus,
httpTooManyRequestsStatusMessage,
} from '../../../test/cypress/support/commonTests';

// colors
const { getPaletteColor } = colors;
const primary = getPaletteColor('primary');

// selectors
const selectorProfileQuestionnaire = 'profile-questionnaires';
const selectorQuestionnaireItem = 'questionnaire-item';
const selectorQuestionnaireTitle = 'questionnaire-title';
const selectorQuestionnaireButton = 'questionnaire-button';
const selectorQuestionnaireButtonIcon = 'questionnaire-button-icon';

// variables
const iconSize = 18;

describe('<ProfileQuestionnaires>', () => {
let questionnaires;

before(() => {
cy.fixture('listQuestionnaires').then((fixtureData) => {
questionnaires = fixtureData;
});
});

it('has translation for all strings', () => {
cy.testLanguageStringsInContext(
['buttonFillQuestionnaire'],
'profile',
i18n,
);
});

context('desktop', () => {
beforeEach(() => {
cy.mount(ProfileQuestionnaires, {
props: {},
});
cy.viewport('macbook-16');
});

coreTests();
});

context('mobile', () => {
beforeEach(() => {
cy.mount(ProfileQuestionnaires, {
props: {},
});
cy.viewport('iphone-6');
});

coreTests();
});

function coreTests() {
it('renders component', () => {
cy.dataCy(selectorProfileQuestionnaire).should('be.visible');
});

it('renders correct number of questionnaire items', () => {
cy.dataCy(selectorQuestionnaireItem).should(
'have.length',
questionnaires.length,
);
});

it('displays correct title and button URL for each questionnaire item', () => {
questionnaires.forEach((questionnaire, index) => {
cy.dataCy(selectorQuestionnaireItem)
.eq(index)
.within(() => {
// link
cy.dataCy(selectorQuestionnaireTitle).should(
'contain',
questionnaire.title,
);
// link url
cy.dataCy(selectorQuestionnaireButton)
.should('have.attr', 'href', questionnaire.link.url)
.and('have.attr', 'target', questionnaire.link.target);
// successful response from url
cy.request({
url: questionnaire.link.url,
failOnStatusCode: failOnStatusCode,
}).then((resp) => {
if (resp.status === httpTooManyRequestsStatus) {
cy.log(httpTooManyRequestsStatusMessage);
return;
}
expect(resp.status).to.eq(httpSuccessfullStatus);
});
// icon external link
if (questionnaire.link.target === '_blank') {
cy.dataCy(selectorQuestionnaireButtonIcon)
.should('be.visible')
.and('have.color', primary);
cy.dataCy(selectorQuestionnaireButtonIcon)
.invoke('height')
.should('be.eq', iconSize);
cy.dataCy(selectorQuestionnaireButtonIcon)
.invoke('width')
.should('be.eq', iconSize);
} else {
cy.dataCy(selectorQuestionnaireButtonIcon).should('not.exist');
}
});
});
});

it('renders button link with correct text for each item', () => {
cy.dataCy(selectorQuestionnaireButton).each(($button) => {
cy.wrap($button).should(
'contain',
i18n.global.t('profile.buttonFillQuestionnaire'),
);
});
});
}
});
21 changes: 14 additions & 7 deletions src/components/__tests__/ProfileTabs.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { routesConf } from 'src/router/routes_conf';
// selectors
const selectorProfileTabs = 'profile-tabs';
const selectorButtonDetails = 'profile-tabs-button-details';
const selectorButtonForms = 'profile-tabs-button-forms';
const selectorButtonQuestionnaires = 'profile-tabs-button-questionnaires';
const selectorButtonNewsletter = 'profile-tabs-button-newsletter';
const selectorButtonNotifications = 'profile-tabs-button-notifications';
const selectorPanelDetails = 'profile-tabs-panel-details';
const selectorPanelForms = 'profile-tabs-panel-forms';
const selectorPanelQuestionnaires = 'profile-tabs-panel-questionnaires';
const selectorPanelNewsletter = 'profile-tabs-panel-newsletter';
const selectorPanelNotifications = 'profile-tabs-panel-notifications';
const selectorProfileQuestionnaires = 'profile-questionnaires';

describe('<ProfileTabs>', () => {
it('has translation for all strings', () => {
Expand Down Expand Up @@ -48,7 +49,7 @@ function coreTests() {
'contain',
i18n.global.t('profile.tabDetails'),
);
cy.dataCy(selectorButtonForms).and(
cy.dataCy(selectorButtonQuestionnaires).and(
'contain',
i18n.global.t('profile.tabForms'),
);
Expand All @@ -63,14 +64,14 @@ function coreTests() {

cy.dataCy(selectorButtonDetails).click();
cy.dataCy(selectorPanelDetails).should('be.visible');
cy.dataCy(selectorPanelForms).should('not.exist');
cy.dataCy(selectorPanelQuestionnaires).should('not.exist');
cy.dataCy(selectorPanelNewsletter).should('not.exist');
cy.dataCy(selectorPanelNotifications).should('not.exist');
});

it('allows to switch tabs', () => {
cy.dataCy(selectorButtonForms).click();
cy.dataCy(selectorPanelForms).should('be.visible');
cy.dataCy(selectorButtonQuestionnaires).click();
cy.dataCy(selectorPanelQuestionnaires).should('be.visible');
cy.dataCy(selectorButtonNewsletter).click();
cy.dataCy(selectorPanelNewsletter).should('be.visible');
cy.dataCy(selectorButtonNotifications).click();
Expand All @@ -83,7 +84,7 @@ function coreTests() {
// initial state
cy.url().should('include', routesConf['profile_details'].path);
// switch to details tab
cy.dataCy(selectorButtonForms).click();
cy.dataCy(selectorButtonQuestionnaires).click();
cy.url().should('not.include', routesConf['profile_details'].path);
cy.url().should('include', routesConf['profile_forms'].path);
// switch to forms tab
Expand All @@ -99,4 +100,10 @@ function coreTests() {
cy.url().should('include', routesConf['profile_newsletter'].path);
cy.dataCy(selectorPanelNewsletter).should('be.visible');
});

it('renders questionnaires tab', () => {
cy.dataCy(selectorButtonQuestionnaires).click();
cy.dataCy(selectorPanelQuestionnaires).should('be.visible');
cy.dataCy(selectorProfileQuestionnaires).should('be.visible');
});
}
153 changes: 153 additions & 0 deletions src/components/__tests__/QuestionnaireItem.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { colors } from 'quasar';
import QuestionnaireItem from 'components/profile/QuestionnaireItem.vue';
import { i18n } from '../../boot/i18n';

// colors
const { getPaletteColor } = colors;
const primary = getPaletteColor('primary');
const white = getPaletteColor('white');

// selectors
const questionnaireItem = 'questionnaire-item';
const questionnaireTitle = 'questionnaire-title';
const questionnaireButton = 'questionnaire-button';
const questionnaireButtonIcon = 'questionnaire-button-icon';
const questionnaireAvatar = 'questionnaire-avatar';
const questionnaireImage = 'questionnaire-image';

// variables
const avatarSize = 48;
const iconSize = 18;

describe('<QuestionnaireItem>', () => {
let questionnaires;

before(() => {
cy.fixture('listQuestionnaires').then((fixtureData) => {
questionnaires = fixtureData;
});
});

it('has translation for all strings', () => {
cy.testLanguageStringsInContext(
['buttonFillQuestionnaire'],
'profile',
i18n,
);
});

context('desktop - target _blank', () => {
beforeEach(() => {
cy.wrap(questionnaires[0]).as('questionnaire');
cy.get('@questionnaire').then((questionnaire) => {
cy.mount(QuestionnaireItem, {
props: {
questionnaire: questionnaire,
},
});
});
cy.viewport('macbook-16');
});

coreTests();
iconTests();
});

context('desktop - target _self', () => {
beforeEach(() => {
cy.wrap(questionnaires[1]).as('questionnaire');
cy.get('@questionnaire').then((questionnaire) => {
cy.mount(QuestionnaireItem, {
props: {
questionnaire: questionnaire,
},
});
});
cy.viewport('macbook-16');
});

coreTests();

it('does not render an external link icon in the button', () => {
cy.dataCy(questionnaireButtonIcon).should('not.exist');
});
});

context('mobile', () => {
beforeEach(() => {
cy.wrap(questionnaires[0]).as('questionnaire');
cy.get('@questionnaire').then((questionnaire) => {
cy.mount(QuestionnaireItem, {
props: {
questionnaire: questionnaire,
},
});
});
cy.viewport('iphone-6');
});

coreTests();
iconTests();
});

function coreTests() {
it('renders component', () => {
cy.dataCy(questionnaireItem).should('be.visible');
});

it('displays the questionnaire title', () => {
cy.get('@questionnaire').then((questionnaire) => {
cy.dataCy(questionnaireTitle)
.should('have.css', 'font-size', '16px')
.and('have.css', 'font-weight', '400')
.and('contain', questionnaire.title);
});
});

it('renders the questionnaire image', () => {
cy.get('@questionnaire').then((questionnaire) => {
cy.dataCy(questionnaireAvatar)
.should('be.visible')
.invoke('height')
.should('be.eq', avatarSize);
cy.dataCy(questionnaireAvatar)
.invoke('width')
.should('be.eq', avatarSize);
cy.dataCy(questionnaireImage)
.find('img')
.should('have.attr', 'src', questionnaire.image.src)
.and('have.attr', 'alt', questionnaire.image.alt);
});
});

it('renders a button with correct attributes and text', () => {
cy.get('@questionnaire').then((questionnaire) => {
cy.dataCy(questionnaireButton)
.should('be.visible')
.and('have.attr', 'href', questionnaire.link.url)
.and('have.attr', 'target', questionnaire.link.target)
.and('contain', i18n.global.t('profile.buttonFillQuestionnaire'));
});
});

tmszi marked this conversation as resolved.
Show resolved Hide resolved
it('applies correct styles to the component', () => {
cy.dataCy(questionnaireItem)
.should('have.css', 'padding', '16px')
.and('have.backgroundColor', white);
});
}

function iconTests() {
it('renders an external link icon in the button', () => {
cy.dataCy(questionnaireButtonIcon)
.should('be.visible')
.and('have.color', primary);
cy.dataCy(questionnaireButtonIcon)
.invoke('height')
.should('be.eq', iconSize);
cy.dataCy(questionnaireButtonIcon)
.invoke('width')
.should('be.eq', iconSize);
});
}
});
Loading
Loading