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

test(atomic): write unit tests for load more functional button #5001

Merged
merged 18 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions packages/atomic/src/components/common/load-more/button.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import enTranslations from '@/dist/atomic/lang/en.json';
import i18next, {i18n as I18n} from 'i18next';
import {html, render} from 'lit';
import {loadMoreButton} from './button';

describe('loadMoreButton', () => {
let container: HTMLElement;
let i18n: I18n;

beforeAll(async () => {
i18n = i18next.createInstance();
await i18n.init({
lng: 'en',
resources: {
en: {
translation: enTranslations,
},
},
});
});

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
});

const renderLoadMoreButton = (props: {
moreAvailable: boolean;
label?: 'load-more-results' | 'load-more-products';
}) => {
render(
html`${loadMoreButton({
props: {
i18n,
onClick: () => {},
moreAvailable: props.moreAvailable,
label: props.label ?? 'load-more-results',
},
})}`,
container
);
};

test('should render nothing when moreAvailable is false', () => {
renderLoadMoreButton({moreAvailable: false});

expect(container).toBeEmptyDOMElement();
});

test('should render a button with the correct props', () => {
renderLoadMoreButton({moreAvailable: true});

const button = container.querySelector('button');
expect(button).toHaveClass('btn-primary');
expect(button).toHaveClass('my-2');
expect(button).toHaveClass('p-3');
expect(button).toHaveClass('font-bold');
});

test('should render the children as the label', () => {
renderLoadMoreButton({moreAvailable: true, label: 'load-more-products'});

const button = container.querySelector('button');
expect(button).toHaveTextContent('Load more products');
});
});
4 changes: 2 additions & 2 deletions packages/atomic/src/components/common/load-more/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface LoadMoreButtonProps {
i18n: i18n;
onClick: () => void;
moreAvailable: boolean;
label?: 'load-more-results' | 'load-more-products';
label: 'load-more-results' | 'load-more-products';
}

export const loadMoreButton: FunctionalComponent<LoadMoreButtonProps> = ({
Expand All @@ -25,6 +25,6 @@ export const loadMoreButton: FunctionalComponent<LoadMoreButtonProps> = ({
};
return button({
props: buttonProps,
children: html`${i18n.t(label || 'load-more-results')}`,
children: html`${i18n.t(label)}`,
});
};
Loading