update-css-variables.test.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import { expect, it } from 'vitest';
  2. import { updateCSSVariables } from './update-css-variables';
  3. it('updateCSSVariables should update CSS variables in :root selector', () => {
  4. // 模拟初始的内联样式表内容
  5. const initialStyleContent = ':root { --primaryColor: red; }';
  6. document.head.innerHTML = `<style id="custom-styles">${initialStyleContent}</style>`;
  7. // 要更新的CSS变量和它们的新值
  8. const updatedVariables = {
  9. fontSize: '16px',
  10. primaryColor: 'blue',
  11. secondaryColor: 'green',
  12. };
  13. // 调用函数来更新CSS变量
  14. updateCSSVariables(updatedVariables, 'custom-styles');
  15. // 获取更新后的样式内容
  16. const styleElement = document.querySelector('#custom-styles');
  17. const updatedStyleContent = styleElement ? styleElement.textContent : '';
  18. // 检查更新后的样式内容是否包含正确的更新值
  19. expect(
  20. updatedStyleContent?.includes('primaryColor: blue;') &&
  21. updatedStyleContent?.includes('secondaryColor: green;') &&
  22. updatedStyleContent?.includes('fontSize: 16px;'),
  23. ).toBe(true);
  24. });