Əsas məzmuna keç
FrontDepth, Ana səhifə

Mövzuya qayıt

Məşq: UI-ı istifadəçinin işlətdiyi kimi test etmək

Əvvəlcə cavab ver, sonra müqayisə et. Özünü dürüst qiymətləndir — zəif yerlərin siyahısı buna əsasən qurulur.

3 nəticə sualı · 3 açıq sual

Sualların səviyyəsi

Seçilmiş səviyyəyə qədər olan suallar göstərilir.

Bu səviyyə üçün hələ məşq sualı yoxdur.

  1. Nəticəni təxmin etJunior

    Bu, Testing Library-nin özü deyil, onun query qaydalarının adi JS simulyasiyasıdır: page hər elementin rolunu və accessible name-ini sadalayır. Ad sətirlə verilibsə, bütün adla dəqiq üst-üstə düşməlidir, RegExp isə adın bir hissəsinə uyğun gələ bilər. getByRole heç bir element və ya birdən çox element uyğun gələndə xəta atır; queryByRole heç nə tapmayanda null qaytarır, amma bir neçə element tapanda yenə xəta atır. Real xəta mesajları daha uzundur. Konsola nə çıxacaq?

    JavaScript
    const page = [
      { role: "heading", name: "Your cart" },
      { role: "button", name: "Remove Trail backpack" },
      { role: "button", name: "Remove Rain jacket" },
      { role: "button", name: "Checkout" },
    ];
    
    function queryAllByRole(role, name) {
      return page.filter(
        (el) =>
          el.role === role &&
          (name === undefined || (name instanceof RegExp ? name.test(el.name) : el.name === name)),
      );
    }
    
    function getByRole(role, name) {
      const found = queryAllByRole(role, name);
      if (found.length === 0) throw new Error("no match");
      if (found.length > 1) throw new Error("multiple matches");
      return found[0];
    }
    
    function queryByRole(role, name) {
      const found = queryAllByRole(role, name);
      if (found.length > 1) throw new Error("multiple matches");
      return found[0] ?? null;
    }
    
    function run(label, query) {
      try {
        const element = query();
        console.log(label + ": " + (element === null ? "null" : element.name));
      } catch (error) {
        console.log(label + ": throws, " + error.message);
      }
    }
    
    run("A", () => getByRole("button", "Checkout"));
    run("B", () => getByRole("button", "checkout"));
    run("C", () => queryByRole("alert"));
    run("D", () => queryByRole("button", /remove/i));
    run("E", () => getByRole("button", /rain/i));
    console.log("F: " + queryAllByRole("button", /remove/i).length);
  2. Açıq sualJunior

    "Proqramı istifadəçilərin işlətdiyi kimi test et" React komponent testi üçün nə deməkdir və səni hansı query-lərə aparır?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  3. Nəticəni təxmin etMiddle

    Testing Library və jest-dom-un mətni necə müqayisə etdiyinin adi JS simulyasiyası; kitabxanaların özü deyil. normalize default normalizer-dir: mətnin kənarlarındakı boşluqları silir və ardıcıl boşluqları bir boşluğa çevirir. getByTextMatches getByText-i modelləşdirir: yalnız elementin öz mətn node-larını görür, sətir isə bütün mətnə bərabər olmalıdır; exact false olanda bu, böyük-kiçik hərfə həssas olmayan alt sətir axtarışına çevrilir. toHaveTextContent jest-dom matcher-ini modelləşdirir: tam textContent-i oxuyur, sətir isə alt sətir kimi axtarılır. Konsola nə çıxacaq?

    JavaScript
    const normalize = (text) => text.trim().replace(/\s+/g, " ");
    
    function getByTextMatches(ownText, matcher, exact = true) {
      const text = normalize(ownText);
      if (matcher instanceof RegExp) return matcher.test(text);
      return exact ? text === matcher : text.toLowerCase().includes(matcher.toLowerCase());
    }
    
    function toHaveTextContent(textContent, expected) {
      const text = normalize(textContent);
      return expected instanceof RegExp ? expected.test(text) : text.includes(expected);
    }
    
    // <span>  Free   delivery </span>
    const badge = { ownText: "  Free   delivery ", textContent: "  Free   delivery " };
    // <p>Total: <strong>$40</strong></p>
    const total = { ownText: "Total: ", textContent: "Total: $40" };
    
    console.log(getByTextMatches(badge.ownText, "Free delivery"));
    console.log(getByTextMatches(badge.ownText, "free delivery"));
    console.log(getByTextMatches(badge.ownText, "free", false));
    console.log(getByTextMatches(total.ownText, "Total: $40"));
    console.log(getByTextMatches(total.ownText, "Total:"));
    console.log(toHaveTextContent(total.textContent, "$4"));
    console.log(toHaveTextContent(total.textContent, /^Total: \$4$/));
  4. Açıq sualMiddle

    Yanlış email ünvanı üçün validasiya mesajı göstərən formu necə test edərdin?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  5. Nəticəni təxmin etSenior

    jest-dom-un toBeVisible() matcher-inin etdiyi yoxlamaların adi JS simulyasiyası; kitabxananın özü deyil. Element o zaman görünən sayılır ki, nə onun özündə, nə də heç bir üst elementində (ancestor) hidden atributu, display: none, visibility: hidden və ya collapse, ya da opacity: 0 olmasın. cssLoaded stylesheet-in test sənədinə çatıb-çatmadığını modelləşdirir. Vitest-in default ayarlarında çatmır: import olunan CSS faylları boş sətirlə əvəz olunur, ona görə yalnız inline stillər nəzərə alınır. Konsola nə çıxacaq?

    JavaScript
    const stylesheet = { "is-collapsed": { display: "none" } };
    
    function computedStyle(el, cssLoaded) {
      const fromClasses = cssLoaded ? el.classes.map((name) => stylesheet[name] ?? {}) : [];
      return Object.assign({ display: "block", visibility: "visible", opacity: "1" }, ...fromClasses, el.style);
    }
    
    function isVisible(el, cssLoaded) {
      if (el === null) return true;
      const style = computedStyle(el, cssLoaded);
      const shown =
        !el.hidden &&
        style.display !== "none" &&
        style.visibility !== "hidden" &&
        style.visibility !== "collapse" &&
        style.opacity !== "0";
      return shown && isVisible(el.parent, cssLoaded);
    }
    
    const make = (props) => ({ classes: [], style: {}, hidden: false, parent: null, ...props });
    const panel = make({ classes: ["is-collapsed"] });
    const answer = make({ parent: panel });
    const ghost = make({ style: { opacity: "0" } });
    const drawer = make({ hidden: true });
    const offscreen = make({ style: { position: "absolute", left: "-9999px" } });
    
    console.log("answer without CSS: " + isVisible(answer, false));
    console.log("answer with CSS: " + isVisible(answer, true));
    console.log("ghost: " + isVisible(ghost, false));
    console.log("drawer: " + isVisible(drawer, false));
    console.log("offscreen: " + isVisible(offscreen, true));
  6. Açıq sualSenior

    Komponent testlərin jsdom-da işləyir. Onlar sənə nəyi deyə bilmir və nəyi real brauzerdə test edəcəyinə necə qərar verirsən?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.