> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dittofeed.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Testing SDK

*Coming Soon*

A first-in-class Time-traveling Test SDK for customer journeys. Catch regressions in our messaging automation in CI, before code gets deployed to production.

Check out a sample jest test for a React component.

```javascript src/bookmarkButton.test.tsx theme={null}
describe("When the bookmark button is clicked", () => {
  let df: DittofeedTestEnv;

  beforeEach(async () => {
    df = await Dittofeed.setupTestEnv();
    ...
  })

  it("Emails the clicking user", async () => {
    render(<BookmarkButton/>)
    fireEvent.click(screen.getByText(/bookmark/i))

    // Using simulated time.
    await df.sleep("1 week");
    const messages = await df.fetchMessages();

    expect(messages).toEqual([
      expect.objectContaining({
        to: "test@email.com",
        type: "Email",
        body: expect.stringContaining("https://app.com/user-1/bookmarks")
        ...
      });
    ]);
  });
});
```

<Accordion title="Full Example.">
  <CodeGroup>
    ```javascript src/bookmarkButton.test.tsx theme={null}
    import {render, screen, fireEvent} from '@testing-library/react'

    import BookmarkButton from "./bookmarkButton";


    describe("When the bookmark button is clicked", () => {
      let df: DittofeedTestEnv;

      beforeEach(async () => {
        df = await Dittofeed.setupTestEnv();

        const segment = await df.createSegment({
          name: "Users who have bookmarked"
          definition: {
            type: "Event",
            event: "Article Bookmarked",
          }
        });

        const template = await df.createTemplate({
          name: "First Bookmark"
          definition: {
            type: "Event",
            event: "Article Bookmarked",
            // Liquid templates
            from: '{{ user.accountManager | default: "hello@company.com"}}',
            subject: 'Hi {{ user.firstName | default: "there"}}!',
            body: "Thanks for trying out our app!\n" + 
              "Check out your bookmarks here. " +
              '<a href="https://app.com/{{ user.id }}/bookmarks"> Bookmarks </a>'
          }
        });

        // User journey:
        //   * Triggers when a user sends a track bookmark event.
        //   * Waits 1 week.
        //   * Then sends an email.
        await df.createJourney({
          entry: {
            segmentId: segment.id,
            childId: "1"
          },
          nodes: [
            {
              id: "1",
              type: "Delay",
              // 1 week
              seconds: "604800",
              childId: "2"
            },
            {
              id: "2",
              type: "Message",
              variant: {
                type: "Email",
                templateId: template.id
              },
              childId: "ExitNode"
            },
          ]
        });

        await analytics.identify("user-1", {
          firstName: "John",
          email: "test@email.com"
        });
      });

      afterEach(async () => {
        await df.teardown();
      });

      it("Emails the clicking user", async () => {
        render(<BookmarkButton/>)
        fireEvent.click(screen.getByText(/bookmark/i))

        // Using simulated time.
        await df.sleep("1 week");
        const messages = await df.fetchMessages();

        expect(messages).toEqual([
          expect.objectContaining({
            to: "test@email.com",
            from: "hello@company.com",
            subject: "Hi John!",
            type: "Email",
            body: expect.stringContaining("https://app.com/user-1/bookmarks")
          });
        ]);
      });
    });

    ```

    ```javascript src/bookmarkButton.tsx theme={null}
    export default function BookmarkButton() {
      return <button onClick={() =>
        // Dittofeed's API is Segment and Rudderstack compatible
        analytics.track('Article Bookmarked', {
          title: 'Snow Fall',
          subtitle: 'The Avalanche at Tunnel Creek',
          author: 'John Branch'
        })  
      }>
        Bookmark
      </button>
    }
    ```
  </CodeGroup>
</Accordion>
