Skip to content

Commit

Permalink
feat(quantic): refactored QuanticResultAction to make it use the Quan…
Browse files Browse the repository at this point in the history
…ticTooltip component (#3359)

* refactored QuanticResultAction with the QuanticTooltip component

* method names updated
  • Loading branch information
mmitiche authored Nov 3, 2023
1 parent 6b2dd15 commit 9221f84
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import {

const resultQuickviewActions = (selector: ResultQuickviewSelector) => {
return {
clickPreview: (variant?: string) =>
clickPreview: () =>
selector
.buttonPreview(variant)
.buttonPreview()
.click()
.logAction('When clicking preview button'),

hoverOverPreview: () =>
selector
.buttonPreviewContainer()
.trigger('mouseenter')
.logAction('When hovering over the preview button'),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ import {
ResultQuickviewSelectors,
} from './result-quickview-selectors';

export type PreviewVariantType = 'brand' | 'outline-brand' | 'result-action';

function resultQuickviewExpectations(selector: ResultQuickviewSelector) {
return {
displayButtonPreview: (display: boolean, variant?: string) => {
displayButtonPreview: (display: boolean) => {
selector
.buttonPreview(variant)
.buttonPreview()
.should(display ? 'exist' : 'not.exist')
.logDetail(`${should(display)} display the button preview`);
},
displayCorrectPreviewButtonVariant: (variant: PreviewVariantType) => {
const expectedCssVariantClasses = {
brand: 'slds-button_brand',
'outline-brand': 'slds-button_outline-brand',
'result-action': 'slds-button_icon-border-filled',
};
selector
.buttonPreview()
.should('have.class', expectedCssVariantClasses[`${variant}`])
.log(
`the preview button should be displayed in the ${variant} variant`
);
},
displayButtonPreviewIcon: (display: boolean) => {
selector
.buttonPreviewIcon()
Expand All @@ -29,9 +44,9 @@ function resultQuickviewExpectations(selector: ResultQuickviewSelector) {
.should('contain', iconName)
.logDetail(`the icon in button preview should contain "${iconName}"`);
},
buttonPreviewIsDisabled: (disabled: boolean, variant?: string) => {
buttonPreviewIsDisabled: (disabled: boolean) => {
selector
.buttonPreview(variant)
.buttonPreview()
.invoke('attr', 'disabled')
.should(disabled ? 'exist' : 'not.exist')
.logDetail(`The button preview ${should(disabled)} be disabled`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {ComponentSelector, CypressSelector} from '../../common-selectors';
export const resultQuickviewComponent = 'c-quantic-result-quickview';

export interface ResultQuickviewSelector extends ComponentSelector {
buttonPreview: (variant?: string) => CypressSelector;
buttonPreview: () => CypressSelector;
buttonPreviewContainer: () => CypressSelector;
buttonPreviewIcon: () => CypressSelector;
sectionPreview: () => CypressSelector;
closeButton: () => CypressSelector;
Expand All @@ -19,10 +20,12 @@ export interface ResultQuickviewSelector extends ComponentSelector {
export const ResultQuickviewSelectors: ResultQuickviewSelector = {
get: () => cy.get(resultQuickviewComponent),

buttonPreview: (variant?: string) =>
variant
? ResultQuickviewSelectors.get().find('.slds-button_' + variant)
: ResultQuickviewSelectors.get().find('.quickview__button-base'),
buttonPreviewContainer: () =>
ResultQuickviewSelectors.get().find(
'[data-cy="quick-view-button__container"]'
),
buttonPreview: () =>
ResultQuickviewSelectors.get().find('[data-cy="quick-view-button"]'),
buttonPreviewIcon: () =>
ResultQuickviewSelectors.buttonPreview().find('lightning-icon'),
sectionPreview: () => ResultQuickviewSelectors.get().find('section'),
Expand All @@ -40,5 +43,5 @@ export const ResultQuickviewSelectors: ResultQuickviewSelector = {
ResultQuickviewSelectors.get().find(
'.quickview__content-container .iframe-wrapper .quickview__spinner-container'
),
tooltip: () => ResultQuickviewSelectors.get().find('.slds-popover'),
tooltip: () => ResultQuickviewSelectors.get().find('[data-cy="tooltip"]'),
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import {
} from '../../../page-objects/search';
import {scope} from '../../../reporters/detailed-collector';
import {ResultQuickviewActions as Actions} from './result-quickview-actions';
import {ResultQuickviewExpectations as Expect} from './result-quickview-expectations';
import {
ResultQuickviewExpectations as Expect,
PreviewVariantType,
} from './result-quickview-expectations';

const variants = ['brand', 'outline-brand', 'result-action'];

interface ResultQuickviewOptions {
result: string;
maximumPreviewSize: number;
previewButtonIcon: string;
previewButtonLabel: string;
previewButtonVariant: string;
previewButtonVariant: PreviewVariantType;
tooltip: string;
}

Expand Down Expand Up @@ -101,17 +106,6 @@ describe('quantic-result-quickview', () => {
Expect.displaySectionPreview(true);
});

scope('custom #previewButtonVariant', () => {
visitResultQuickview({
previewButtonVariant: 'outline-brand',
});
mockResultHtmlContent('div');

Expect.displayButtonPreview(true, 'outline-brand');
Actions.clickPreview('outline-brand');
Expect.displaySectionPreview(true);
});

scope('custom #tooltip', () => {
const customTooltip = 'Quick view';
visitResultQuickview({
Expand All @@ -120,21 +114,29 @@ describe('quantic-result-quickview', () => {
mockResultHtmlContent('div');

Expect.displayButtonPreview(true);
Actions.hoverOverPreview();
Expect.displayTooltip(customTooltip);
});
});
});

describe('with #previewButtonVariant set to result-action', () => {
it('should display the quickview button as a result action', () => {
visitResultQuickview({
previewButtonVariant: 'result-action',
});
mockResultHtmlContent('div');
describe('the preview button with custom variants', () => {
variants.forEach((variantValue) => {
it(`should display the quick view button in the ${variantValue} variant`, () => {
visitResultQuickview({
previewButtonVariant: <PreviewVariantType>variantValue,
});
mockResultHtmlContent('div');

scope('when loading the page', () => {
Expect.events.receivedEvent(true, resultActionRegister);
Expect.displayButtonPreview(true, 'icon-border-filled');
scope('when loading the page', () => {
if (variantValue === 'result-action') {
Expect.events.receivedEvent(true, resultActionRegister);
}
Expect.displayButtonPreview(true);
Expect.displayCorrectPreviewButtonVariant(
<PreviewVariantType>variantValue
);
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const functionsMocks = {
listener: jest.fn(() => {}),
};

const selectors = {
tooltip: 'c-quantic-tooltip',
tooltipContent: 'c-quantic-tooltip > div[slot="content"]',
};

const exampleLabel = 'example label';
const exampleSelectedLabel = 'example label on';
const exampleIconName = 'example icon name';
Expand Down Expand Up @@ -91,11 +96,15 @@ describe('c-quantic-result-action', () => {
const resultActionButton = element.shadowRoot.querySelector(
'lightning-button-icon-stateful'
);
const tooltip = element.shadowRoot.querySelector('.slds-popover_tooltip');
const tooltip = element.shadowRoot.querySelector(selectors.tooltip);
const tooltipContent = element.shadowRoot.querySelector(
selectors.tooltipContent
);

expect(resultActionButton).not.toBeNull();
expect(tooltip).not.toBeNull();
expect(tooltip.textContent).toBe(exampleLabel);
expect(tooltipContent).not.toBeNull();
expect(tooltipContent.textContent).toBe(exampleLabel);
expect(resultActionButton.iconName).toBe(exampleIconName);
});

Expand Down Expand Up @@ -190,10 +199,14 @@ describe('c-quantic-result-action', () => {
const element = createTestComponent({...defaultOptions, selected: true});
await flushPromises();

const tooltip = element.shadowRoot.querySelector('.slds-popover_tooltip');
const tooltip = element.shadowRoot.querySelector(selectors.tooltip);
const tooltipContent = element.shadowRoot.querySelector(
selectors.tooltipContent
);

expect(tooltip).not.toBeNull();
expect(tooltip.textContent).toBe(exampleSelectedLabel);
expect(tooltipContent).not.toBeNull();
expect(tooltipContent.textContent).toBe(exampleSelectedLabel);
});

it('should display the correct icon', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="slds-is-relative slds-show_inline result-action_container result-action_white-container">
<div class="slds-is-relative slds-show_inline result-action_container result-action_white-container" onmouseenter={showTooltip} onmouseleave={hideTooltip}>
<template if:true={loading}>
<button class={loadingClasses} aria-label={displayedLabel}>
<div class="slds-spinner_container slds-var-m-around_xx-small">
Expand All @@ -16,10 +16,9 @@
</lightning-button-icon-stateful>
</template>
<template if:true={displayedLabel}>
<div class="slds-popover slds-popover_tooltip slds-nubbin_bottom-left result-action_tooltip slds-fall-into-ground"
aria-hidden="true">
<div class="slds-popover__body">{displayedLabel}</div>
</div>
<c-quantic-tooltip>
<div slot="content">{displayedLabel}</div>
</c-quantic-tooltip>
</template>
</div>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ export default class QuanticResultAction extends LightningElement {
this.dispatchEvent(resultActionEvent);
}

showTooltip() {
this.tooltipComponent?.showTooltip();
}

hideTooltip() {
this.tooltipComponent?.hideTooltip();
}

/**
* @returns {Object}
*/
get tooltipComponent() {
return this.template.querySelector('c-quantic-tooltip');
}

/**
* Returns the label to be displayed in the tooltip.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,9 @@
.slds-nubbin_bottom-left:before,
.slds-nubbin_bottom-left:after {
left: 50%;
}

.slds-popover_tooltip {
width: max-content;
}

.result-action_tooltip {
position: absolute;
top: -3rem;
left: 50%;
transform: translate(-50%, 0);
}

.result-action_white-container {
background-color: white;
display: inline-block;
border-radius: var(--lwc-borderRadiusMedium, 0.25rem);
}

.result-action_container:hover > .result-action_tooltip {
visibility: visible;
opacity: 1;
}

.result-action_button {
--lwc-borderWidthThin: 0.5px;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class={buttonContainerClass}>
<button aria-hidden={isQuickviewOpen} class={buttonClass} onclick={openQuickview} title={buttonTitle}
<div data-cy="quick-view-button__container" class={buttonContainerClass} onmouseenter={showTooltip} onmouseleave={hideTooltip}>
<button data-cy="quick-view-button" aria-hidden={isQuickviewOpen} class={buttonClass} onclick={openQuickview} title={buttonTitle}
disabled={hasNoPreview} aria-label={buttonAriaLabelValue}>
<template if:true={hasButtonLabel}> {previewButtonLabel} </template>
<template if:true={hasIcon}>
Expand All @@ -10,10 +10,9 @@
</template>
</button>
<template if:true={tooltip}>
<div class="slds-popover slds-popover_tooltip slds-nubbin_bottom-left result-action_tooltip slds-fall-into-ground"
aria-hidden="true">
<div class="slds-popover__body">{tooltip}</div>
</div>
<c-quantic-tooltip>
<div slot="content">{tooltip}</div>
</c-quantic-tooltip>
</template>
</div>
<template if:true={isQuickviewOpen}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ export default class QuanticResultQuickview extends LightningElement {
this._isLoading = false;
}

showTooltip() {
this.tooltipComponent?.showTooltip();
}

hideTooltip() {
this.tooltipComponent?.hideTooltip();
}

/**
* @returns {Object}
*/
get tooltipComponent() {
return this.template.querySelector('c-quantic-tooltip');
}

get contentURL() {
return this.state.contentURL?.includes(
encodeURIComponent(this.result.uniqueId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.tooltip__content {
left: 50%;
transform: translate(-50%, -100%);
top: -0.7rem;
top: -0.4rem;
}

.tooltip__content--visible {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class={tooltipCSSClass} aria-hidden="true">
<div data-cy="tooltip" class={tooltipCSSClass} aria-hidden="true">
<div class="slds-popover__body">
<slot name="content"></slot>
</div>
Expand Down

0 comments on commit 9221f84

Please sign in to comment.