@immense/vue-pom-generator 1.0.84 → 1.0.85

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -4539,6 +4539,9 @@ async function introspectNuxtPages(projectRoot, options = {}) {
4539
4539
  routePathMap.set(pathTemplate, componentName);
4540
4540
  routeMetaEntries.push({
4541
4541
  componentName,
4542
+ // Nuxt file-based routes are named by convention, but the static walk does not
4543
+ // recover those names — leave null so callers fall back to path-template navigation.
4544
+ name: null,
4542
4545
  pathTemplate,
4543
4546
  params,
4544
4547
  query: []
@@ -4632,6 +4635,7 @@ async function parseRouterFileFromCwd(routerEntryPath, options = {}) {
4632
4635
  if (typeof pathTemplate === "string" && pathTemplate.length) {
4633
4636
  routeMetaEntries.push({
4634
4637
  componentName,
4638
+ name: typeof r.name === "string" && r.name.length ? r.name : null,
4635
4639
  pathTemplate,
4636
4640
  params: paramsMeta,
4637
4641
  query: queryKeys
@@ -4778,19 +4782,31 @@ async function getRouteMetaByComponent(projectRoot, routerEntry, routerType, opt
4778
4782
  const map = /* @__PURE__ */ new Map();
4779
4783
  for (const entry of routeMetaEntries) {
4780
4784
  const list = map.get(entry.componentName) ?? [];
4781
- list.push({ template: entry.pathTemplate });
4785
+ list.push({ name: entry.name, template: entry.pathTemplate, params: entry.params });
4782
4786
  map.set(entry.componentName, list);
4783
4787
  }
4784
- const chooseRouteMeta = (entries) => {
4788
+ const dedupeRoutes = (entries) => {
4789
+ const seen = /* @__PURE__ */ new Set();
4790
+ const unique = [];
4791
+ for (const entry of entries) {
4792
+ if (seen.has(entry.template))
4793
+ continue;
4794
+ seen.add(entry.template);
4795
+ unique.push(entry);
4796
+ }
4797
+ return unique;
4798
+ };
4799
+ const choosePrimaryTemplate = (entries) => {
4785
4800
  if (!entries.length)
4786
4801
  return null;
4787
- return entries.slice().sort((a, b) => a.template.length - b.template.length || a.template.localeCompare(b.template))[0];
4802
+ return entries.slice().sort((a, b) => a.template.length - b.template.length || a.template.localeCompare(b.template))[0].template;
4788
4803
  };
4789
4804
  const sorted = Array.from(map.entries()).sort((a, b) => a[0].localeCompare(b[0]));
4790
4805
  return Object.fromEntries(
4791
- sorted.map(([componentName, entries]) => {
4792
- const chosen = chooseRouteMeta(entries);
4793
- return chosen ? [componentName, chosen] : null;
4806
+ sorted.map(([componentName, rawEntries]) => {
4807
+ const routes = dedupeRoutes(rawEntries);
4808
+ const primaryTemplate = choosePrimaryTemplate(routes);
4809
+ return primaryTemplate ? [componentName, { template: primaryTemplate, routes }] : null;
4794
4810
  }).filter((entry) => !!entry)
4795
4811
  );
4796
4812
  }
@@ -4805,27 +4821,131 @@ function generateRouteProperty(routeMeta) {
4805
4821
  })
4806
4822
  ];
4807
4823
  }
4808
- function generateGoToSelfMethod(componentName) {
4824
+ function buildParamTypeString(params) {
4825
+ return `{ ${params.map((p) => `${p.name}${p.optional ? "?" : ""}: string | number`).join("; ")} }`;
4826
+ }
4827
+ function chooseParamlessRoute(routes) {
4828
+ const paramless = routes.filter((r) => r.params.length === 0);
4829
+ if (!paramless.length)
4830
+ return null;
4831
+ return paramless.slice().sort((a, b) => a.template.length - b.template.length || a.template.localeCompare(b.template))[0];
4832
+ }
4833
+ function chooseParametrizedRoute(routes) {
4834
+ const parametrized = routes.filter((r) => r.params.length > 0);
4835
+ if (!parametrized.length)
4836
+ return null;
4837
+ return parametrized.slice().sort((a, b) => a.params.length - b.params.length || a.template.length - b.template.length || a.template.localeCompare(b.template))[0];
4838
+ }
4839
+ const GOTO_RUNTIME_LINES = [
4840
+ "const runtimeEnv = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env;",
4841
+ "const runtimeBaseUrl = runtimeEnv?.PLAYWRIGHT_RUNTIME_BASE_URL ?? runtimeEnv?.PLAYWRIGHT_TEST_BASE_URL ?? runtimeEnv?.VITE_PLAYWRIGHT_BASE_URL;",
4842
+ "const resolvedUrl = runtimeBaseUrl ? new URL(targetUrl, runtimeBaseUrl).toString() : targetUrl;",
4843
+ "await this.page.goto(resolvedUrl);"
4844
+ ];
4845
+ const ROUTER_GLOBAL_NAME = "__appRouter";
4846
+ const pushBlock = (routeNameExpr, paramsExpr, nullable) => {
4847
+ const routerType = `{ ${ROUTER_GLOBAL_NAME}?: { push: (to: { name: string; params: Record<string, unknown> }) => Promise<unknown>; resolve: (to: { name: string; params: Record<string, unknown> }) => { href: string } } }`;
4848
+ const routeParamsLine = paramsExpr === "undefined" ? "const routeParams = {};" : nullable ? `const routeParams = Object.fromEntries(Object.entries(${paramsExpr} ?? {}).filter(([, v]) => v !== undefined));` : `const routeParams = Object.fromEntries(Object.entries(${paramsExpr}).filter(([, v]) => v !== undefined));`;
4849
+ return [
4850
+ routeParamsLine,
4851
+ `const isCold = await this.page.evaluate(() => typeof (globalThis as { ${ROUTER_GLOBAL_NAME}?: unknown }).${ROUTER_GLOBAL_NAME} === "undefined");`,
4852
+ `if (isCold) {`,
4853
+ ` await this.page.goto("/", { waitUntil: "commit" });`,
4854
+ ` await this.page.waitForFunction(() => typeof (globalThis as { ${ROUTER_GLOBAL_NAME}?: unknown }).${ROUTER_GLOBAL_NAME} !== "undefined", { timeout: 15000 });`,
4855
+ ` const href = await this.page.evaluate(({ name, params }) => (globalThis as ${routerType}).${ROUTER_GLOBAL_NAME}?.resolve({ name, params })?.href, { name: ${routeNameExpr}, params: routeParams });`,
4856
+ ` if (href) { await this.page.goto(href, { waitUntil: "domcontentloaded" }); }`,
4857
+ `} else {`,
4858
+ ` await this.page.evaluate(async ({ name, params }) => {`,
4859
+ ` await (globalThis as ${routerType}).${ROUTER_GLOBAL_NAME}?.push({ name, params });`,
4860
+ ` }, { name: ${routeNameExpr}, params: routeParams });`,
4861
+ `}`
4862
+ ];
4863
+ };
4864
+ function generateGoToMethod(componentName, routeMeta) {
4865
+ if (!routeMeta) {
4866
+ return [
4867
+ createClassMethod({
4868
+ name: "goTo",
4869
+ isAsync: true,
4870
+ statements: [
4871
+ `throw new Error("[pom] No router path found for component/page-object '${componentName}'.");`
4872
+ ]
4873
+ })
4874
+ ];
4875
+ }
4876
+ const paramless = chooseParamlessRoute(routeMeta.routes);
4877
+ const parametrized = chooseParametrizedRoute(routeMeta.routes);
4878
+ const hasParamless = paramless !== null;
4879
+ const hasParametrized = parametrized !== null;
4880
+ if (!hasParamless && !hasParametrized) {
4881
+ return [
4882
+ createClassMethod({
4883
+ name: "goTo",
4884
+ isAsync: true,
4885
+ statements: [
4886
+ `throw new Error("[pom] No router path found for component/page-object '${componentName}'.");`
4887
+ ]
4888
+ })
4889
+ ];
4890
+ }
4891
+ const paramStatements = (entry) => {
4892
+ const lines = [];
4893
+ for (const param of entry.params) {
4894
+ const token = getParamToken(param.name);
4895
+ if (param.optional) {
4896
+ lines.push(`targetUrl = params.${param.name} === undefined ? targetUrl.replaceAll(${JSON.stringify(`/${token}`)}, "") : targetUrl.replaceAll(${JSON.stringify(token)}, String(params.${param.name}));`);
4897
+ } else {
4898
+ lines.push(`targetUrl = targetUrl.replaceAll(${JSON.stringify(token)}, String(params.${param.name}));`);
4899
+ }
4900
+ }
4901
+ return lines;
4902
+ };
4903
+ if (hasParamless && !hasParametrized) {
4904
+ const route = paramless;
4905
+ return [
4906
+ createClassMethod({
4907
+ name: "goTo",
4908
+ isAsync: true,
4909
+ statements: route.name !== null ? pushBlock(JSON.stringify(route.name), "undefined", false) : [`const targetUrl = ${JSON.stringify(route.template)};`, ...GOTO_RUNTIME_LINES]
4910
+ })
4911
+ ];
4912
+ }
4913
+ const paramType = buildParamTypeString(parametrized.params);
4914
+ if (hasParametrized && !hasParamless) {
4915
+ const route = parametrized;
4916
+ const allOptional = route.params.every((p) => p.optional);
4917
+ return [
4918
+ createClassMethod({
4919
+ name: "goTo",
4920
+ isAsync: true,
4921
+ parameters: [{ name: "params", type: paramType, hasQuestionToken: allOptional }],
4922
+ statements: route.name !== null ? pushBlock(JSON.stringify(route.name), "params", allOptional) : [`let targetUrl = ${JSON.stringify(route.template)};`, ...paramStatements(route), ...GOTO_RUNTIME_LINES]
4923
+ })
4924
+ ];
4925
+ }
4926
+ const paramlessRoute = paramless;
4927
+ const parametrizedRoute = parametrized;
4928
+ const usePush = paramlessRoute.name !== null && parametrizedRoute.name !== null;
4809
4929
  return [
4810
4930
  createClassMethod({
4811
4931
  name: "goTo",
4812
4932
  isAsync: true,
4813
- statements: [
4814
- "await this.goToSelf();"
4815
- ]
4816
- }),
4817
- createClassMethod({
4818
- name: "goToSelf",
4819
- isAsync: true,
4820
- statements: [
4821
- `const route = ${componentName}.route;`,
4822
- "if (!route) {",
4823
- ` throw new Error("[pom] No router path found for component/page-object '${componentName}'.");`,
4933
+ overloads: [
4934
+ { parameters: [], returnType: "Promise<void>" },
4935
+ { parameters: [{ name: "params", type: paramType }], returnType: "Promise<void>" }
4936
+ ],
4937
+ parameters: [{ name: "params", type: paramType, hasQuestionToken: true }],
4938
+ statements: usePush ? pushBlock(
4939
+ `params ? ${JSON.stringify(parametrizedRoute.name)} : ${JSON.stringify(paramlessRoute.name)}`,
4940
+ "params",
4941
+ true
4942
+ ) : [
4943
+ `const template = params ? ${JSON.stringify(parametrizedRoute.template)} : ${JSON.stringify(paramlessRoute.template)};`,
4944
+ "let targetUrl = template;",
4945
+ `if (params) {`,
4946
+ ...paramStatements(parametrizedRoute).map((line) => ` ${line}`),
4824
4947
  "}",
4825
- "const runtimeEnv = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env;",
4826
- "const runtimeBaseUrl = runtimeEnv?.PLAYWRIGHT_RUNTIME_BASE_URL ?? runtimeEnv?.PLAYWRIGHT_TEST_BASE_URL ?? runtimeEnv?.VITE_PLAYWRIGHT_BASE_URL;",
4827
- "const targetUrl = runtimeBaseUrl ? new URL(route.template, runtimeBaseUrl).toString() : route.template;",
4828
- "await this.page.goto(targetUrl);"
4948
+ ...GOTO_RUNTIME_LINES
4829
4949
  ]
4830
4950
  })
4831
4951
  ];
@@ -5869,7 +5989,7 @@ function prepareViewObjectModelClass(componentName, dependencies, componentHiera
5869
5989
  if (isView && options.vueRouterFluentChaining) {
5870
5990
  const routeMeta = options.routeMetaByComponent?.[componentName] ?? null;
5871
5991
  members.push(...generateRouteProperty(routeMeta));
5872
- members.push(...generateGoToSelfMethod(className));
5992
+ members.push(...generateGoToMethod(className, routeMeta));
5873
5993
  }
5874
5994
  members.push(...generateMethodsContentForDependencies(componentName, dependencies));
5875
5995
  return {