How to use where clause in Knex
Create refunds with Stripe
Load HTML with Cheerio
How to send POST request with Axios
Find intersection of array in Lodash
Powered by Official white Bloop logo with a period

Terms / Privacy / Search / Support

  • import {
      Inject,
      Injectable,
      InjectionToken,
    } from "@angular/core";
    
    export const BROWSER_STORAGE = new InjectionToken(
      "Browser Storage",
      {
        providedIn: "root",
        factory: () => localStorage,
      }
    );
    
    @Injectable({
      providedIn: "root",
    })
    export class BrowserStorageService {
      constructor(
        @Inject(BROWSER_STORAGE) public storage: Storage
      ) {}
    
      get(key: string) {
        return this.storage.getItem(key);
      }
    
      set(key: string, value: string) {
        this.storage.setItem(key, value);
      }
    
      remove(key: string) {
        this.storage.removeItem(key);
      }
    
      clear() {
        this.storage.clear();
      }
    }
    
    Docs
    0
  • import { InjectionToken } from "@angular/core";
    
    export const TITLE = new InjectionToken("title");
    
    Docs
    0
  • import {
      Inject,
      Injectable,
      InjectionToken,
    } from "@angular/core";
    
    export const BROWSER_STORAGE = new InjectionToken(
      "Browser Storage",
      {
        providedIn: "root",
        factory: () => localStorage,
      }
    );
    
    @Injectable({
      providedIn: "root",
    })
    export class BrowserStorageService {
      constructor(
        @Inject(BROWSER_STORAGE) public storage: Storage
      ) {}
    
      get(key: string) {
        return this.storage.getItem(key);
      }
    
      set(key: string, value: string) {
        this.storage.setItem(key, value);
      }
    
      remove(key: string) {
        this.storage.removeItem(key);
      }
    
      clear() {
        this.storage.clear();
      }
    }
    
    Docs
    0
  • import { Component, Inject } from "@angular/core";
    
    import { DateLoggerService } from "./date-logger.service";
    import { Hero } from "./hero";
    import { HeroService } from "./hero.service";
    import { LoggerService } from "./logger.service";
    import { MinimalLogger } from "./minimal-logger.service";
    import { RUNNERS_UP, runnersUpFactory } from "./runners-up";
    
    @Component({
      selector: "app-hero-of-the-month",
      templateUrl: "./hero-of-the-month.component.html",
      providers: [
        { provide: Hero, useValue: someHero },
        { provide: TITLE, useValue: "Hero of the Month" },
        { provide: HeroService, useClass: HeroService },
        { provide: LoggerService, useClass: DateLoggerService },
        { provide: MinimalLogger, useExisting: LoggerService },
        {
          provide: RUNNERS_UP,
          useFactory: runnersUpFactory(2),
          deps: [Hero, HeroService],
        },
      ],
    })
    export class HeroOfTheMonthComponent {
      logs: string[] = [];
    
      constructor(
        logger: MinimalLogger,
        public heroOfTheMonth: Hero,
        @Inject(RUNNERS_UP) public runnersUp: string,
        @Inject(TITLE) public title: string
      ) {
        this.logs = logger.logs;
        logger.logInfo("starting up");
      }
    }
    
    Docs
    0
  • const handler = jest.fn();
    
    const {
      container: { firstChild: input },
    } = render();
    
    fireEvent.input(input, { target: { value: "a" } });
    
    expect(handler).toHaveBeenCalledTimes(1);
    
    Docs
    0
  • const ref = createRef();
    const spy = jest.fn();
    
    render(
      h(elementType, {
        onDblClick: spy,
        ref,
      })
    );
    
    fireEvent["onDblClick"](ref.current);
    
    expect(spy).toHaveBeenCalledTimes(1);
    
    Docs
    0
  • const { getByLabelText, queryAllByTestId } =
      render(Component);
    
    Docs
    0
  • // With options.
    const { results } = render(YourComponent, {
      target: MyTarget,
      props: { myProp: "value" },
    });
    
    // Props only.
    const { results } = render(YourComponent, {
      myProp: "value",
    });
    
    Docs
    0
  • const cb = jest.fn();
    
    
    function Counter() {
      useEffect(cb);
    
    
      const [count, setCount] = useState(0);
    
    
      return ;
    }
    
    
    const { container: { firstChild: buttonNode }, } = render();
    
    
    // Clear the first call to useEffect that the initial render triggers.
    cb.mockClear();
    
    
    // Fire event Option 1.
    fireEvent.click(buttonNode);
    
    
    // Fire event Option 2.
    fireEvent(
    buttonNode,
    new Event('MouseEvent', {
      bubbles: true,
      cancelable: true,
      button: 0,
    });
    
    
    expect(buttonNode).toHaveTextContent('1');
    expect(cb).toHaveBeenCalledTimes(1);
    Docs
    0
  • const table = document.createElement("table");
    
    const { container } = render(TableBody, {
      props,
      container: document.body.appendChild(table),
    });
    
    Docs
    0
  • const { getByDataCy } = render();
    
    expect(getByDataCy("my-component")).toHaveTextContent(
      "Hello"
    );
    
    Docs
    0
  • test("has correct welcome text", () => {
      render();
      expect(screen.getByRole("heading")).toHaveTextContent(
        "Welcome, John Doe"
      );
    });
    
    Docs
    0
  • render();
    
    Docs
    0
  • test("handles click correctly", () => {
      render();
    
      userEvent.click(screen.getByText("Check"));
      expect(screen.getByLabelText("Check")).toBeChecked();
    });
    
    Docs
    0
  • // Example, a function to traverse table contents
    import * as tableQueries from "my-table-query-library";
    import { queries } from "@testing-library/react";
    
    const { getByRowColumn, getByText } = render(, {
      queries: { ...queries, ...tableQueries },
    });
    
    Docs
    0
  • test("has correct input value", () => {
      render();
      expect(screen.getByRole("form")).toHaveFormValues({
        firstName: "John",
        lastName: "Doe",
      });
    });
    
    Docs
    0
  • const table = document.createElement("table");
    
    const { container } = render(, {
      container: document.body.appendChild(table),
    });
    
    Docs
    0
  • // @testing-library/react
    const { container } = render();
    const foo = container.querySelector('[data-foo="bar"]');
    
    Docs
    0
  • const { getByLabelText, queryAllByTestId } = render(
      
    );
    
    Docs
    0
  • test("loads items eventually", async () => {
      // Click button
      fireEvent.click(getByText(node, "Load"));
    
      // Wait for page to update with query text
      const items = await findByText(node, /Item #[0-9]: /);
      expect(items).toHaveLength(10);
    });
    
    Docs
    0
  • test("movie title appears", async () => {
      // element is initially not present...
    
      // wait for appearance inside an assertion
      await waitFor(() => {
        expect(getByText("the lion king")).toBeInTheDocument();
      });
    });
    
    Docs
    0
  • const puppeteer = require("puppeteer");
    const {
      getDocument,
      queries,
      waitFor,
    } = require("pptr-testing-library");
    
    const { getByTestId, getByLabelText } = queries;
    
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    
    // Grab ElementHandle for document
    const $document = await getDocument(page);
    // Your favorite query methods are available
    const $form = await getByTestId($document, "my-form");
    // returned elements are Puppeteer ElementHandles too!
    const $email = await getByLabelText($form, "Email");
    // interact with puppeteer like usual
    await $email.type("pptr@example.com");
    // waiting works too!
    await waitFor(() => getByText("Loading..."));
    
    Docs
    0
  • await fireEvent.click(getByText("Click me"));
    
    Docs
    0
  • test("movie title goes away", async () => {
      // element is initially present...
      // note use of queryBy instead of getBy to return null
      // instead of throwing in the query itself
      await waitFor(() => {
        expect(queryByText("i, robot")).not.toBeInTheDocument();
      });
    });
    
    Docs
    0
  • expect(queryByText("submit")).toBeNull();
    
    // or, if using jest-dom:
    import "@testing-library/jest-dom";
    expect(queryByText("submit")).not.toBeInTheDocument();
    
    Docs
    0
  • test("movie title no longer present in DOM", async () => {
      // element is removed
      await waitForElementToBeRemoved(() =>
        queryByText("the mummy")
      );
    });
    
    Docs
    0
  • Powered by Official black Bloop logo with a period
    download the IDE extension

    View other examples