import { describe, it, expect, vi} from 'vitest' import * as Style from '../../src/style/index.js' import * as CSS from '../../src/utils/css.js' import { getSelectorWeight, getSelectorWithWeight } from '../../src/index.js' describe('CSS.optimize', () => { it('should inline imports in css string', async () => { const cssString = `@import url('https://example.com/style.css'); body { background-color: red; }`; const expectedOptimizedCssString = `.container { width: 100%; } body { background-color: red; }`; const spy = vi.spyOn(global, 'fetch'); spy.mockImplementation(async (url: string) => { if (url === 'https://example.com/style.css') { return Promise.resolve(new Response( `.container { width: 100%; }`)); } throw new Error('Invalid URL'); }); const optimizedCssString = await CSS.optimize(cssString); expect(spy).toHaveBeenCalledWith('https://example.com/style.css'); expect(optimizedCssString).toEqual(expectedOptimizedCssString); }); it('should inline imports in css string', async () => { const cssString = `@import url('https://example.com/style.css'); body { background-color: red; }`; const expectedOptimizedCssString = `.container { width: 100%; } .nested { width: 1%; } body { background-color: red; }`; const spy = vi.spyOn(global, 'fetch'); spy.mockImplementation(async (url: string) => { if (url === 'https://example.com/style.css') { return Promise.resolve(new Response( `.container { width: 100%; } @import 'https://example.com/style2.css';`)); } if (url === 'https://example.com/style2.css') { return Promise.resolve(new Response( `.nested { width: 1%; }`)); } throw new Error('Invalid URL'); }); const optimizedCssString = await CSS.optimize(cssString); expect(spy).toHaveBeenCalledWith('https://example.com/style.css'); expect(optimizedCssString).toEqual(expectedOptimizedCssString); }); it('should inline multiple imports in css string', async () => { const cssString = `@import url('https://example.com/style.css'); body { background-color: red; } @import url('https://example.com/style2.css');`; const expectedOptimizedCssString = `.container { width: 100%; } body { background-color: red; } .second { width: 1%; }`; const spy = vi.spyOn(global, 'fetch'); spy.mockImplementation(async (url: string) => { if (url === 'https://example.com/style.css') { return Promise.resolve(new Response( `.container { width: 100%; }`)); } if (url === 'https://example.com/style2.css') { return Promise.resolve(new Response( `.second { width: 1%; }`)); } throw new Error('Invalid URL'); }); const optimizedCssString = await CSS.optimize(cssString); expect(spy).toHaveBeenCalledWith('https://example.com/style.css'); expect(optimizedCssString).toEqual(expectedOptimizedCssString); }); });