summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/test/e2e/lib/path-helpers.ts
blob: bd8593dc885696a57ee87522574e508ff31bce18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { expect } from '@playwright/test';

import { RoutePath } from '../../../src/shared/routes';

export function generatePath(path: RoutePath, params: Record<string, string>): string {
  return Object.entries(params)
    .reduce(
      (path, [name, value]) => path.replace(new RegExp(`:${name}\\??`), value),
      path as string,
    )
    .replaceAll(new RegExp('/:.*?\\?', 'g'), '');
}

// Match the actual path against against the expected path where the expected can contain parameters
function toMatchPath(actual: string, expected: string | null) {
  const pass = matchPaths(expected, actual);
  const message = () =>
    pass
      ? `Expected path to be "${expected}"`
      : `Expected path "${expected}", but found "${actual}"`;
  return { pass, message };
}

expect.extend({ toMatchPath });

function trimTrailingSlash(value: string): string {
  return value.replaceAll(/\/$/g, '');
}

// Match b against a where a can contain parameters
export function matchPaths(a: string | null, b: string | null): boolean {
  if (a === null || b === null) {
    return a === b;
  }

  const aParts = trimTrailingSlash(a).split('/');
  const bParts = trimTrailingSlash(b).split('/');

  if (bParts.some((part) => part.startsWith(':'))) {
    throw new Error('Only first argument is allowed to contain dynamic route path segments');
  }

  return (
    aParts.length >= bParts.length &&
    aParts.every((aPart, i) => {
      if (aPart.startsWith(':')) {
        return aPart.endsWith('?') ? true : bParts[i] !== undefined;
      } else {
        return aPart === bParts[i];
      }
    })
  );
}