.ts
TypeScript
(application/typescript)
import { AppRoute } from "../../routes.defs";
import { default as buildRouteLink } from "./buildRouteLink";

describe("buildRouteLink", () => {
  it(`should return path from route without transform when it gets called with a null as routeParams argument`, () => {
    // Given
    const routeName: AppRoute = AppRoute.HOME;
    // When
    const link = buildRouteLink(routeName, null);
    // Then
    expect(link).toMatchInlineSnapshot(`"/"`);
  });
  it(`should return path from route without transform when it gets called with an empty object as routeParams argument`, () => {
    // Given
    const routeName: AppRoute = AppRoute.HOME;
    // When
    const link = buildRouteLink(routeName, {});
    // Then
    expect(link).toMatchInlineSnapshot(`"/"`);
  });
  it(`should return path from route with params values when it gets called with an object containing values as routeParams argument`, () => {
    // Given
    const routeName: AppRoute = AppRoute.ORGANIZATION_DETAILS;
    // When
    const link = buildRouteLink(routeName, {
      orgSlug: "my-super-org",
    });
    // Then
    expect(link).toMatchInlineSnapshot(`"/my-super-org"`);
  });
  it(`should return path from route with params values when it gets called with an object containing values and magic "*" value as routeParams argument`, () => {
    // Given
    const routeName: AppRoute = AppRoute.REPOSITORY_BROWSER_WITH_PATH;
    // When
    const link = buildRouteLink(routeName, {
      orgSlug: "my-super-org",
      repoSlug: "my-super-repo",
      currentRef: "main",
      "*": "app.manifest.json",
    });
    // Then
    expect(link).toMatchInlineSnapshot(
      `"/my-super-org/my-super-repo/main/tree/app.manifest.json"`
    );
  });
  it(`should return path from route with url encoded params values when it gets called with an object containing values as routeParams argument`, () => {
    // Given
    const routeName: AppRoute = AppRoute.REPOSITORY_BROWSER_WITH_PATH;
    // When
    const link = buildRouteLink(routeName, {
      orgSlug: "my-super-org",
      repoSlug: "my-super-repo",
      currentRef: "main",
      "*": "app/services/auth/isExistingEmailAddress.ts",
    });
    // Then
    expect(link).toMatchInlineSnapshot(
      `"/my-super-org/my-super-repo/main/tree/app%2Fservices%2Fauth%2FisExistingEmailAddress.ts"`
    );
  });
  it(`should return path from route with url encoded params values when it gets called without an options object`, () => {
    // Given
    const routeName: AppRoute = AppRoute.USER_DETAILS;
    // When
    const link = buildRouteLink(routeName, {
      username: "test-user",
    });
    // Then
    expect(link).toMatchInlineSnapshot(`"/%40test-user"`);
  });
  it(`should return path from route with url encoded params values when it gets called with options.encodeURIComponent is set to true`, () => {
    // Given
    const routeName: AppRoute = AppRoute.USER_DETAILS;
    // When
    const link = buildRouteLink(
      routeName,
      {
        username: "test-user",
      },
      { encodeURIComponent: true }
    );
    // Then
    expect(link).toMatchInlineSnapshot(`"/%40test-user"`);
  });
  it(`should return path from route WITHOUT url encoded params values when it gets called with options.encodeURIComponent is set to false`, () => {
    // Given
    const routeName: AppRoute = AppRoute.USER_DETAILS;
    // When
    const link = buildRouteLink(
      routeName,
      {
        username: "test-user",
      },
      { encodeURIComponent: false }
    );
    // Then
    expect(link).toMatchInlineSnapshot(`"/@test-user"`);
  });
});