Skip to main content
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.
src/bookmarkButton.test.tsx
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: "[email protected]",
        type: "Email",
        body: expect.stringContaining("https://app.com/user-1/bookmarks")
        ...
      });
    ]);
  });
});
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: "[email protected]"}}',
        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: "[email protected]"
    });
  });

  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: "[email protected]",
        from: "[email protected]",
        subject: "Hi John!",
        type: "Email",
        body: expect.stringContaining("https://app.com/user-1/bookmarks")
      });
    ]);
  });
});

I