首页 > 解决方案 > 用于定位的 jest 测试 css

问题描述

有谁知道如何为此编写一个笑话测试?

loginBtn.onclick = (()=>{
  loginForm.style.marginLeft = "0%";
  loginText.style.marginLeft = "0%";
});

我已经尝试过了,但它没有用

beforeEach(() => {
        document.documentElement.innerHTML = html.toString();
    });

test('clicking signup moves the styles', () => {
        const loginForm = document.querySelector("form.login");
        const loginText = document.querySelector(".title-text .login");
        expect(loginForm.style.marginLeft).toBeFalsy();
        expect(loginText.style.marginLeft).toBeFalsy();

标签: csstestingjestjs

解决方案


.toBeFalsy() 不会是真的,因为 '0%' 是一个不同于 0 的字符串,它是假的

你将不得不使用toEqual('0%')这种情况

expect(loginForm.style.marginLeft).toEqual('0%');
expect(loginText.style.marginLeft).toEqual('0%');

推荐阅读