migrate to jest (#721)

* migrate rrweb-snapshot tests to jest

* migrate rrweb tests to jest
This commit is contained in:
yz-yu
2021-10-06 15:31:42 +08:00
committed by GitHub
parent 5622738e61
commit 18e4356be9
26 changed files with 9313 additions and 7983 deletions

View File

@@ -1,49 +1,47 @@
import 'mocha';
import { JSDOM } from 'jsdom';
import { expect } from 'chai';
import { absoluteToStylesheet, _isBlockedElement } from '../src/snapshot';
describe('absolute url to stylesheet', () => {
const href = 'http://localhost/css/style.css';
it('can handle relative path', () => {
expect(absoluteToStylesheet('url(a.jpg)', href)).to.equal(
expect(absoluteToStylesheet('url(a.jpg)', href)).toEqual(
`url(http://localhost/css/a.jpg)`,
);
});
it('can handle same level path', () => {
expect(absoluteToStylesheet('url("./a.jpg")', href)).to.equal(
expect(absoluteToStylesheet('url("./a.jpg")', href)).toEqual(
`url("http://localhost/css/a.jpg")`,
);
});
it('can handle parent level path', () => {
expect(absoluteToStylesheet('url("../a.jpg")', href)).to.equal(
expect(absoluteToStylesheet('url("../a.jpg")', href)).toEqual(
`url("http://localhost/a.jpg")`,
);
});
it('can handle absolute path', () => {
expect(absoluteToStylesheet('url("/a.jpg")', href)).to.equal(
expect(absoluteToStylesheet('url("/a.jpg")', href)).toEqual(
`url("http://localhost/a.jpg")`,
);
});
it('can handle external path', () => {
expect(
absoluteToStylesheet('url("http://localhost/a.jpg")', href),
).to.equal(`url("http://localhost/a.jpg")`);
expect(absoluteToStylesheet('url("http://localhost/a.jpg")', href)).toEqual(
`url("http://localhost/a.jpg")`,
);
});
it('can handle single quote path', () => {
expect(absoluteToStylesheet(`url('./a.jpg')`, href)).to.equal(
expect(absoluteToStylesheet(`url('./a.jpg')`, href)).toEqual(
`url('http://localhost/css/a.jpg')`,
);
});
it('can handle no quote path', () => {
expect(absoluteToStylesheet('url(./a.jpg)', href)).to.equal(
expect(absoluteToStylesheet('url(./a.jpg)', href)).toEqual(
`url(http://localhost/css/a.jpg)`,
);
});
@@ -54,7 +52,7 @@ describe('absolute url to stylesheet', () => {
'background-image: url(images/b.jpg);background: #aabbcc url(images/a.jpg) 50% 50% repeat;',
href,
),
).to.equal(
).toEqual(
`background-image: url(http://localhost/css/images/b.jpg);` +
`background: #aabbcc url(http://localhost/css/images/a.jpg) 50% 50% repeat;`,
);
@@ -63,13 +61,13 @@ describe('absolute url to stylesheet', () => {
it('can handle data url image', () => {
expect(
absoluteToStylesheet('url(data:image/gif;base64,ABC)', href),
).to.equal('url(data:image/gif;base64,ABC)');
).toEqual('url(data:image/gif;base64,ABC)');
expect(
absoluteToStylesheet(
'url(data:application/font-woff;base64,d09GMgABAAAAAAm)',
href,
),
).to.equal('url(data:application/font-woff;base64,d09GMgABAAAAAAm)');
).toEqual('url(data:application/font-woff;base64,d09GMgABAAAAAAm)');
});
it('preserves quotes around inline svgs with spaces', () => {
@@ -78,7 +76,7 @@ describe('absolute url to stylesheet', () => {
"url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")",
href,
),
).to.equal(
).toEqual(
"url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")",
);
expect(
@@ -86,20 +84,20 @@ describe('absolute url to stylesheet', () => {
'url(\'data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>\')',
href,
),
).to.equal(
).toEqual(
'url(\'data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>\')',
);
expect(
absoluteToStylesheet(
'url("data:image/svg+xml;utf8,<svg width=\"28\" height=\"32\" viewBox=\"0 0 28 32\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M27 14C28\" fill=\"white\"/></svg>")',
'url("data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>")',
href,
),
).to.equal(
'url("data:image/svg+xml;utf8,<svg width=\"28\" height=\"32\" viewBox=\"0 0 28 32\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M27 14C28\" fill=\"white\"/></svg>")',
).toEqual(
'url("data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>")',
);
});
it('can handle empty path', () => {
expect(absoluteToStylesheet(`url('')`, href)).to.equal(`url('')`);
expect(absoluteToStylesheet(`url('')`, href)).toEqual(`url('')`);
});
});
@@ -111,20 +109,20 @@ describe('isBlockedElement()', () => {
JSDOM.fragment(html).querySelector('div')!;
it('can handle empty elements', () => {
expect(subject('<div />')).to.equal(false);
expect(subject('<div />')).toEqual(false);
});
it('blocks prohibited className', () => {
expect(subject('<div class="foo rr-block bar" />')).to.equal(true);
expect(subject('<div class="foo rr-block bar" />')).toEqual(true);
});
it('does not block random data selector', () => {
expect(subject('<div data-rr-block />')).to.equal(false);
expect(subject('<div data-rr-block />')).toEqual(false);
});
it('blocks blocked selector', () => {
expect(
subject('<div data-rr-block />', { blockSelector: '[data-rr-block]' }),
).to.equal(true);
).toEqual(true);
});
});