2022-12-04 22:38:56 +01:00
|
|
|
import { describe, it, expect, vi, afterEach } from 'vitest';
|
2024-02-01 22:38:21 +09:00
|
|
|
import { render, fireEvent, cleanup } from '@testing-library/vue';
|
|
|
|
|
import { Smile, Edit2, Pen } from '../src/lucide-vue-next';
|
2021-05-20 21:24:54 +02:00
|
|
|
|
|
|
|
|
describe('Using lucide icon components', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
afterEach(() => cleanup());
|
2022-12-04 22:38:56 +01:00
|
|
|
|
2021-05-20 21:24:54 +02:00
|
|
|
it('should render an component', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const { container } = render(Smile);
|
2022-02-18 14:53:55 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-05-20 21:24:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should adjust the size, stroke color and stroke width', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const { container } = render(Smile, {
|
2022-02-18 14:53:55 +01:00
|
|
|
props: {
|
2021-05-20 21:24:54 +02:00
|
|
|
size: 48,
|
2022-02-18 14:53:55 +01:00
|
|
|
color: 'red',
|
2024-02-01 22:38:21 +09:00
|
|
|
'stroke-width': 4,
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-05-20 21:24:54 +02:00
|
|
|
|
2022-02-18 14:53:55 +01:00
|
|
|
const [icon] = document.getElementsByClassName('lucide');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(icon.getAttribute('width')).toBe('48');
|
|
|
|
|
expect(icon.getAttribute('stroke')).toBe('red');
|
|
|
|
|
expect(icon.getAttribute('stroke-width')).toBe('4');
|
2022-02-18 14:53:55 +01:00
|
|
|
|
|
|
|
|
expect(container).toMatchSnapshot();
|
2021-05-20 21:24:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a class to the element', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const { container } = render(Smile, {
|
2021-05-20 21:24:54 +02:00
|
|
|
attrs: {
|
2024-02-01 22:38:21 +09:00
|
|
|
class: 'my-icon',
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-05-20 21:24:54 +02:00
|
|
|
|
2022-02-18 14:53:55 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
|
|
|
|
|
const [icon] = document.getElementsByClassName('my-icon');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(icon).toHaveClass('my-icon');
|
|
|
|
|
expect(icon).toHaveClass('lucide');
|
|
|
|
|
expect(icon).toHaveClass('lucide-smile-icon');
|
2021-05-20 21:24:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a style attribute to the element', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const { container } = render(Smile, {
|
2021-05-20 21:24:54 +02:00
|
|
|
attrs: {
|
|
|
|
|
style: 'position: absolute',
|
2024-02-01 22:38:21 +09:00
|
|
|
},
|
|
|
|
|
});
|
2021-05-20 21:24:54 +02:00
|
|
|
|
2022-02-18 14:53:55 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
|
|
|
|
|
|
const [icon] = document.getElementsByClassName('lucide');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(icon).toHaveStyle({ position: 'absolute' });
|
2022-02-18 14:53:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should call the onClick event', async () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const onClick = vi.fn();
|
2022-02-18 14:53:55 +01:00
|
|
|
render(Smile, {
|
|
|
|
|
attrs: {
|
|
|
|
|
onClick,
|
2024-02-01 22:38:21 +09:00
|
|
|
},
|
|
|
|
|
});
|
2022-02-18 14:53:55 +01:00
|
|
|
|
|
|
|
|
const [icon] = document.getElementsByClassName('lucide');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
await fireEvent.click(icon);
|
2022-02-18 14:53:55 +01:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(onClick).toHaveBeenCalled();
|
2022-02-18 14:53:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should pass children to the icon slot', () => {
|
2024-02-01 22:38:21 +09:00
|
|
|
const testText = 'Hello World';
|
2022-02-18 14:53:55 +01:00
|
|
|
const template = {
|
|
|
|
|
name: 'Stub',
|
2024-02-01 22:38:21 +09:00
|
|
|
template: `<text>${testText}</text>`,
|
|
|
|
|
};
|
2022-02-18 14:53:55 +01:00
|
|
|
const { getByText, container } = render(Smile, {
|
|
|
|
|
slots: {
|
2024-02-01 22:38:21 +09:00
|
|
|
default: template,
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-02-18 14:53:55 +01:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const textElement = getByText(testText);
|
2022-02-18 14:53:55 +01:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(textElement).toBeInTheDocument();
|
2022-02-18 14:53:55 +01:00
|
|
|
expect(container).toMatchSnapshot();
|
2021-05-20 21:24:54 +02:00
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
|
|
|
|
|
it('should render the alias icon', () => {
|
|
|
|
|
const { container } = render(Pen, {
|
|
|
|
|
props: {
|
|
|
|
|
size: 48,
|
|
|
|
|
color: 'red',
|
2024-02-01 22:38:21 +09:00
|
|
|
'stroke-width': 4,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
const PenIconRenderedHTML = container.innerHTML;
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
cleanup();
|
2023-04-20 16:08:34 +02:00
|
|
|
|
|
|
|
|
const { container: Edit2Container } = render(Edit2, {
|
|
|
|
|
props: {
|
|
|
|
|
size: 48,
|
|
|
|
|
color: 'red',
|
2024-02-01 22:38:21 +09:00
|
|
|
'stroke-width': 4,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(PenIconRenderedHTML).toBe(Edit2Container.innerHTML);
|
|
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
|
|
|
|
|
it('should not scale the strokeWidth when absoluteStrokeWidth is set', () => {
|
|
|
|
|
render(Pen, {
|
|
|
|
|
props: {
|
|
|
|
|
size: 48,
|
|
|
|
|
color: 'red',
|
2024-02-01 22:38:21 +09:00
|
|
|
absoluteStrokeWidth: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-04-20 16:08:34 +02:00
|
|
|
|
|
|
|
|
const [icon] = document.getElementsByClassName('lucide');
|
|
|
|
|
|
2024-02-01 22:38:21 +09:00
|
|
|
expect(icon.getAttribute('width')).toBe('48');
|
|
|
|
|
expect(icon.getAttribute('stroke')).toBe('red');
|
|
|
|
|
expect(icon.getAttribute('stroke-width')).toBe('1');
|
|
|
|
|
});
|
2021-05-20 21:24:54 +02:00
|
|
|
});
|