Skip to content

Commit

Permalink
[misc cleanup] improve tests
Browse files Browse the repository at this point in the history
- add basic unit test for confirming that the right utils areb eing called

- add missing E2E test for resize observer behavior
  • Loading branch information
cee-chen committed Aug 24, 2023
1 parent f4a9af4 commit 4a96de8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/components/text_truncate/text_truncate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,15 @@ describe('EuiTextTruncate', () => {
cy.get('[data-test-subj="test"]').should('exist');
});
});

describe('resize observer behavior', () => {
it('renders a resize observer if `width` is not passed', () => {
cy.viewport(200, 50);
cy.mount(<EuiTextTruncate {...props} width={undefined} />);
getTruncatedText().should('have.text', 'Lorem ipsum dolor sit amet, …');

cy.viewport(100, 50);
getTruncatedText().should('have.text', 'Lorem ipsum …');
});
});
});
60 changes: 48 additions & 12 deletions src/components/text_truncate/text_truncate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ import { render } from '../../test/rtl';
import { shouldRenderCustomStyles } from '../../test/internal';
import { requiredProps } from '../../test';

// Util mocks
const mockEarlyReturn = { checkIfTruncationIsNeeded: () => false };
const mockCleanup = jest.fn();
jest.mock('./utils', () => {
return {
TruncationUtilsWithDOM: jest.fn(() => ({
...mockEarlyReturn,
cleanup: mockCleanup,
})),
TruncationUtilsWithCanvas: jest.fn(() => mockEarlyReturn),
};
});
import { TruncationUtilsWithCanvas } from './utils';

import { EuiTextTruncate } from './text_truncate';

describe('EuiTextTruncate', () => {
beforeEach(() => jest.clearAllMocks());

const props = {
text: 'Hello world',
width: 50,
Expand All @@ -29,20 +45,40 @@ describe('EuiTextTruncate', () => {
expect(container.firstChild).toMatchSnapshot();
});

it('does not render a resize observer if a width is passed', () => {
const onResize = jest.fn();
render(<EuiTextTruncate {...props} width={100} onResize={onResize} />);
expect(onResize).not.toHaveBeenCalled();
describe('resize observer', () => {
it('does not render a resize observer if a width is passed', () => {
const onResize = jest.fn();
render(<EuiTextTruncate {...props} width={100} onResize={onResize} />);
expect(onResize).not.toHaveBeenCalled();
});

it('renders a resize observer when no width is passed', () => {
const onResize = jest.fn();
render(
<EuiTextTruncate {...props} width={undefined} onResize={onResize} />
);
expect(onResize).toHaveBeenCalledWith(0);
});
});

it('renders a resize observer when no width is passed', () => {
const onResize = jest.fn();
render(
<EuiTextTruncate {...props} width={undefined} onResize={onResize} />
);
expect(onResize).toHaveBeenCalledWith(0);
describe('render API', () => {
it('calls the DOM cleanup method after each render', () => {
render(<EuiTextTruncate {...props} measurementRenderAPI="dom" />);
expect(mockCleanup).toHaveBeenCalledTimes(1);
});

it('allows switching to canvas rendering via `measurementRenderAPI`', () => {
render(
<EuiTextTruncate
width={100}
text="Canvas test"
measurementRenderAPI="canvas"
/>
);
expect(TruncationUtilsWithCanvas).toHaveBeenCalledTimes(1);
expect(mockCleanup).not.toHaveBeenCalled();
});
});

// We can't unit test the actual truncation logic in JSDOM, because
// JSDOM doesn't have `offsetWidth`. See the Cypress spec tests instead
// We can't unit test the actual truncation logic in JSDOM - see Cypress spec tests instead
});

0 comments on commit 4a96de8

Please sign in to comment.