-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
autoprefixer-browerslist.spec.ts
38 lines (32 loc) · 1.43 KB
/
autoprefixer-browerslist.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { expect, test } from 'vitest';
import { isBuild, findAssetFile, page } from '~utils';
test('should prefix position: sticky for code in source tree', async () => {
let stickyStyle = isBuild
? await getStyleFromDist('sticky')
: await getStyleFromPage(page, 'sticky');
// minify style to have a more stable expect
stickyStyle = stickyStyle.replace(/\s/g, '').replace(/;$/, '');
expect(stickyStyle).toBe('position:-webkit-sticky;position:sticky');
});
/* unfortunately this test fails. the dependency has a different absolute path and .browerslist is not picked up
test('should prefix position: sticky for imported dependency', async () => {
const stickyStyle = isBuild
? await getStyleFromDist('sticky-dep')
: await getStyleFromPage(page,'sticky-dep');
expect(stickyStyle).toBe('position:-webkit-sticky;position:sticky')
});
*/
async function getStyleFromPage(page, cssClass: string) {
const styleNodes = await page.$$('head style');
const styles: string[] = await Promise.all(styleNodes.map((e) => e.textContent()));
const css = styles.find((s) => s.includes(`.${cssClass}.`));
return extractStyleContent(css, cssClass);
}
async function getStyleFromDist(cssClass: string) {
const css = await findAssetFile(/index.*\.css/);
return extractStyleContent(css, cssClass);
}
function extractStyleContent(css: string, cssClass: string) {
const match = css.match(new RegExp(`\\.${cssClass}\\.[^\\{]*\\{([^\\}]*)\\}`));
return match[1];
}