Home / File/ DisjointSet-test.ts — react Source File

DisjointSet-test.ts — react Source File

Architecture documentation for DisjointSet-test.ts, a typescript file in the react codebase. 2 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  8717d424_843c_a729_f999_954766189242["DisjointSet-test.ts"]
  edec7689_7b1d_03c9_9cbb_bb9b0552bc30["DisjointSet.ts"]
  8717d424_843c_a729_f999_954766189242 --> edec7689_7b1d_03c9_9cbb_bb9b0552bc30
  1765a682_3028_4441_b26f_c712ca2597d5["DisjointSet"]
  8717d424_843c_a729_f999_954766189242 --> 1765a682_3028_4441_b26f_c712ca2597d5
  style 8717d424_843c_a729_f999_954766189242 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import DisjointSet from '../Utils/DisjointSet';

type TestIdentifier = {
  id: number;
  name: string;
};

describe('DisjointSet', () => {
  let identifierId = 0;
  function makeIdentifier(name: string): TestIdentifier {
    return {
      id: identifierId++,
      name,
    };
  }

  function makeIdentifiers(...names: string[]): TestIdentifier[] {
    return names.map(name => makeIdentifier(name));
  }

  beforeEach(() => {
    identifierId = 0;
  });

  it('.find - finds the correct group which the item is associated with', () => {
    const identifiers = new DisjointSet<TestIdentifier>();
    const [x, y, z] = makeIdentifiers('x', 'y', 'z');

    identifiers.union([x]);
    identifiers.union([y, x]);

    expect(identifiers.find(x)).toBe(y);
    expect(identifiers.find(y)).toBe(y);
    expect(identifiers.find(z)).toBe(null);
  });

  it('.size - returns 0 when empty', () => {
    const identifiers = new DisjointSet<TestIdentifier>();

    expect(identifiers.size).toBe(0);
  });

  it('.size - returns the correct size when non-empty', () => {
    const identifiers = new DisjointSet<TestIdentifier>();
    const [x, y] = makeIdentifiers('x', 'y', 'z');

    identifiers.union([x]);
    identifiers.union([y, x]);

    expect(identifiers.size).toBe(2);
  });

  it('.buildSets - returns non-overlapping sets', () => {
    const identifiers = new DisjointSet<TestIdentifier>();
    const [a, b, c, x, y, z] = makeIdentifiers('a', 'b', 'c', 'x', 'y', 'z');

    identifiers.union([a]);
    identifiers.union([b, a]);
    identifiers.union([c, b]);

    identifiers.union([x]);
    identifiers.union([y, x]);
    identifiers.union([z, y]);
    identifiers.union([x, z]);

    expect(identifiers.buildSets()).toMatchInlineSnapshot(`
      [
        Set {
          {
            "id": 0,
            "name": "a",
          },
          {
            "id": 1,
            "name": "b",
          },
          {
            "id": 2,
            "name": "c",
          },
        },
        Set {
          {
            "id": 3,
            "name": "x",
          },
          {
            "id": 4,
            "name": "y",
          },
          {
            "id": 5,
            "name": "z",
          },
        },
      ]
    `);
  });

  // Regression test for issue #933
  it("`forEach` doesn't infinite loop when there are cycles", () => {
    const identifiers = new DisjointSet<TestIdentifier>();
    const [x, y, z] = makeIdentifiers('x', 'y', 'z');

    identifiers.union([x]);
    identifiers.union([y, x]);
    identifiers.union([z, y]);
    identifiers.union([x, z]);

    identifiers.forEach((_, group) => expect(group).toBe(z));
  });
});

Frequently Asked Questions

What does DisjointSet-test.ts do?
DisjointSet-test.ts is a source file in the react codebase, written in typescript. It belongs to the TestingUtilities domain.
What does DisjointSet-test.ts depend on?
DisjointSet-test.ts imports 2 module(s): DisjointSet, DisjointSet.ts.
Where is DisjointSet-test.ts in the architecture?
DisjointSet-test.ts is located at compiler/packages/babel-plugin-react-compiler/src/__tests__/DisjointSet-test.ts (domain: TestingUtilities, directory: compiler/packages/babel-plugin-react-compiler/src/__tests__).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free