2022-12-08 18:06:36 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Watermark from '..';
|
|
|
|
import mountTest from '../../../tests/shared/mountTest';
|
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2023-01-10 10:56:54 +08:00
|
|
|
import { render, waitFor, waitFakeTimer } from '../../../tests/utils';
|
2022-12-08 18:06:36 +08:00
|
|
|
|
|
|
|
describe('Watermark', () => {
|
|
|
|
mountTest(Watermark);
|
|
|
|
rtlTest(Watermark);
|
|
|
|
|
|
|
|
const mockSrcSet = jest.spyOn(Image.prototype, 'src', 'set');
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
mockSrcSet.mockImplementation(function fn() {
|
2022-12-08 22:32:37 +08:00
|
|
|
this.onload?.();
|
2022-12-08 18:06:36 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
mockSrcSet.mockRestore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('The watermark should render successfully', () => {
|
|
|
|
const { container } = render(<Watermark className="watermark" content="Ant Design" />);
|
|
|
|
expect(container.querySelector('.watermark div')).toBeTruthy();
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('The offset should be correct', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Watermark
|
|
|
|
className="watermark"
|
|
|
|
offset={[200, 200]}
|
|
|
|
content={['Ant Design', 'Ant Design Pro']}
|
|
|
|
/>,
|
|
|
|
);
|
2022-12-08 22:32:37 +08:00
|
|
|
const target = container.querySelector<HTMLDivElement>('.watermark div');
|
2022-12-13 15:47:57 +08:00
|
|
|
expect(target?.style.left).toBe('150px');
|
|
|
|
expect(target?.style.top).toBe('150px');
|
|
|
|
expect(target?.style.width).toBe('calc(100% - 150px)');
|
|
|
|
expect(target?.style.height).toBe('calc(100% - 150px)');
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Interleaved watermark backgroundSize is correct', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Watermark
|
|
|
|
className="watermark"
|
|
|
|
width={200}
|
|
|
|
height={200}
|
|
|
|
content="Ant Design"
|
|
|
|
gap={[100, 100]}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
const target = container.querySelector<HTMLDivElement>('.watermark div');
|
|
|
|
expect(target?.style.backgroundSize).toBe('600px');
|
2022-12-08 18:06:36 +08:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Image watermark snapshot', () => {
|
|
|
|
const { container } = render(
|
|
|
|
<Watermark image="https://gw.alipayobjects.com/zos/bmw-prod/59a18171-ae17-4fc5-93a0-2645f64a3aca.svg" />,
|
|
|
|
);
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-09 10:08:11 +08:00
|
|
|
it('MutationObserver should work properly', async () => {
|
2022-12-08 18:06:36 +08:00
|
|
|
const { container } = render(<Watermark className="watermark" content="MutationObserver" />);
|
2022-12-08 22:32:37 +08:00
|
|
|
const target = container.querySelector<HTMLDivElement>('.watermark div');
|
2023-01-10 10:56:54 +08:00
|
|
|
await waitFakeTimer();
|
2022-12-08 22:32:37 +08:00
|
|
|
target?.remove();
|
2022-12-09 10:08:11 +08:00
|
|
|
await waitFor(() => expect(target).toBeTruthy());
|
2022-12-08 18:06:36 +08:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-09 10:08:11 +08:00
|
|
|
it('Observe the modification of style', async () => {
|
2022-12-08 18:06:36 +08:00
|
|
|
const { container } = render(
|
|
|
|
<Watermark offset={[-200, -200]} className="watermark" content="MutationObserver" />,
|
|
|
|
);
|
2022-12-08 22:32:37 +08:00
|
|
|
const target = container.querySelector<HTMLDivElement>('.watermark div');
|
2023-01-10 10:56:54 +08:00
|
|
|
await waitFakeTimer();
|
2022-12-08 22:32:37 +08:00
|
|
|
target?.setAttribute('style', '');
|
2022-12-09 10:08:11 +08:00
|
|
|
await waitFor(() => expect(target).toBeTruthy());
|
2022-12-08 18:06:36 +08:00
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|