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

chore(tooltip): Added tests for gux-tooltip-title component #471

Merged
merged 1 commit into from
Jun 4, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gux-tooltip-title #render should render component as expected (1) 1`] = `"<gux-tooltip-title hydrated="" class="gux-overflow-hidden" aria-describedby="gux-tooltip-i"><!----><span class="gux-title-container"><span><slot aria-hidden="true" onslotchange="{this.onSlotChange.bind(this)}">Some long text to truncate and use a tooltip</slot></span></span><gux-tooltip aria-hidden="true" hidden="" id="gux-tooltip-i" role="tooltip" hydrated=""><div slot="content"></div></gux-tooltip></gux-tooltip-title>"`;

exports[`gux-tooltip-title #render should render component as expected (2) 1`] = `"<gux-tooltip-title hydrated=""><!----><span class="gux-title-container"><div style="max-width: 200px;">Some short text</div></span></gux-tooltip-title>"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { E2EPage, newE2EPage } from '@stencil/core/testing';
import { a11yCheck } from '../../../../test/e2eTestUtils';

async function newNonrandomE2EPage(
{
html
}: {
html: string;
},
lang: string = 'en'
): Promise<E2EPage> {
const page = await newE2EPage();

await page.evaluateOnNewDocument(() => {
Math.random = () => 0.5;
});
await page.setContent(`<div lang=${lang}>${html}</div>`);
await page.waitForChanges();
await page.addScriptTag({
path: '../../node_modules/axe-core/axe.min.js'
});
await page.waitForChanges();

return page;
}

describe('gux-tooltip-title', () => {
describe('#render', () => {
[
'<div style="max-width: 40px"><gux-tooltip-title><span><slot aria-hidden="true" onSlotchange={this.onSlotChange.bind(this)}>Some long text to truncate and use a tooltip</slot></span></gux-tooltip-title></div>',
'<gux-tooltip-title><div style="max-width: 200px">Some short text</div></gux-tooltip-title>'
].forEach((html, index) => {
it(`should render component as expected (${index + 1})`, async () => {
const page = await newNonrandomE2EPage({ html });
const element = await page.find('gux-tooltip-title');

await a11yCheck(page);

expect(element.outerHTML).toMatchSnapshot();
});
});
});

it('should render a tooltip if the component text width is greater than the parent container', async () => {
const page = await newNonrandomE2EPage({
html: `
<div style="max-width: 40px">
<gux-tooltip-title>
<span>
<slot aria-hidden="true" onSlotchange={this.onSlotChange.bind(this)}>Some long text to truncate and use a tooltip</slot>
</span>
</gux-tooltip-title>
</div>
`
});

const tooltipElement = await page.find('pierce/gux-tooltip');
expect(tooltipElement).toBeDefined();
});

it('should not render a tooltip if the component text width is less than the parent container', async () => {
const page = await newNonrandomE2EPage({
html: `
<gux-tooltip-title>
<span>Some short text</span>
</gux-tooltip-title>
`
});

const tooltipElement = await page.find('pierce/gux-tooltip');
expect(tooltipElement).toBeNull();
});

it('should truncate title text if tooltip is rendered', async () => {
const page = await newNonrandomE2EPage({
html: `
<div style="max-width: 40px">
<gux-tooltip-title>
<span>
<slot aria-hidden="true" onSlotchange={this.onSlotChange.bind(this)}>Some long text to truncate and use a tooltip</slot>
</span>
</gux-tooltip-title>
</div>
`
});

const element = await page.find('gux-tooltip-title');
expect(element).toHaveClass('gux-overflow-hidden');
});

it('Tooltip title text should match screenreader text if gux-icon is being used and no title text is set', async () => {
const page = await newNonrandomE2EPage({
html: `
<div style="max-width: 40px">
<gux-tooltip-title>
<span>
<gux-icon icon-name="unknown" decorative screenreader-text="test screenreader text"></gux-icon>
</span>
</gux-tooltip-title>
</div>
`
});

const tooltipElement = await page.find('gux-tooltip');
expect(tooltipElement.innerText).toBe('test screenreader text');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ gux-tooltip {

.gux-truncate-slot-container {
display: block;
overflow: hidden;
text-overflow: ellipsis;
}

.gux-overflow-hidden {
overflow: hidden;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export class GuxTruncate {
}}
>
<span
class="gux-truncate-slot-container"
class={{
'gux-overflow-hidden': this.needsTruncation(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is backwards compatible.

'gux-truncate-slot-container': true
}}
style={{ webkitLineClamp: this.maxLines?.toString() }}
>
<slot />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { E2EPage, newE2EPage } from '@stencil/core/testing';
import { a11yCheck } from '../../../../test/e2eTestUtils';
import { a11yCheck, newSparkE2EPage } from '../../../../test/e2eTestUtils';

async function newNonrandomE2EPage(
{
Expand Down Expand Up @@ -42,4 +42,60 @@ describe('gux-truncate', () => {
});
});
});

it('should truncate text if the component text width is greater than the parent container', async () => {
const page = await newSparkE2EPage({
html: `
<gux-truncate style="width: 40px">
<div>Div <span>with a span</span> inside</div>
</gux-truncate>
`
});

const element = await page.find('pierce/.gux-truncate-slot-container');
expect(element).toHaveClass('gux-overflow-hidden');
});

it('should not truncate text if the component text width is less than the parent container', async () => {
const page = await newSparkE2EPage({
html: `
<gux-truncate style="width: 400px">
<div>Div <span>with a span</span> inside</div>
</gux-truncate>
`
});

const element = await page.find('pierce/.gux-truncate-slot-container');
expect(element.classList.contains('gux-overflow-hidden')).toBe(false);
});

it('should truncate text after 3 lines of wrapped text', async () => {
const page = await newSparkE2EPage({
html: `
<gux-truncate max-lines="3">
<div>This is a long text that should be truncated after three lines of wrapped text</div>
</gux-truncate>
`
});

const truncateMultiContainer = await page.find(
'pierce/.gux-truncate-multi-line'
);
expect(truncateMultiContainer).toBeDefined();
});

it('should not truncate wrapped text if max-lines attribute is not defined on the gux-truncate tag', async () => {
const page = await newSparkE2EPage({
html: `
<gux-truncate>
<div>This is a long text that should not be truncated</div>
</gux-truncate>
`
});

const truncateContainer = await page.find(
'pierce/.gux-truncate-multi-line'
);
expect(truncateContainer).toBeNull();
});
});
Loading