summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/ng-schematics/test/src/utils.ts
blob: 503cbd5cecf61650674f909b8b915d29589454b2 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import https from 'https';
import {before, after} from 'node:test';
import {join} from 'path';

import type {JsonObject} from '@angular-devkit/core';
import {
  SchematicTestRunner,
  type UnitTestTree,
} from '@angular-devkit/schematics/testing';
import sinon from 'sinon';

const WORKSPACE_OPTIONS = {
  name: 'workspace',
  newProjectRoot: 'projects',
  version: '14.0.0',
};

const SINGLE_APPLICATION_OPTIONS = {
  name: 'sandbox',
  directory: '.',
  createApplication: true,
  version: '14.0.0',
};

const MULTI_APPLICATION_OPTIONS = {
  name: SINGLE_APPLICATION_OPTIONS.name,
};

export const MULTI_LIBRARY_OPTIONS = {
  name: 'components',
};

export function setupHttpHooks(): void {
  // Stop outgoing Request for version fetching
  before(() => {
    const httpsGetStub = sinon.stub(https, 'get');
    httpsGetStub.returns({
      on: (_: string, callback: () => void) => {
        callback();
      },
    } as any);
  });

  after(() => {
    sinon.restore();
  });
}

export function getAngularJsonScripts(
  tree: UnitTestTree,
  isDefault = true,
  name = SINGLE_APPLICATION_OPTIONS.name
): {
  builder: string;
  configurations: Record<string, any>;
  options: Record<string, any>;
} {
  const angularJson = tree.readJson('angular.json') as any;
  const e2eScript = isDefault ? 'e2e' : 'puppeteer';
  return angularJson['projects']?.[name]?.['architect'][e2eScript];
}

export function getPackageJson(tree: UnitTestTree): {
  scripts: Record<string, string>;
  devDependencies: string[];
} {
  const packageJson = tree.readJson('package.json') as JsonObject;
  return {
    scripts: packageJson['scripts'] as any,
    devDependencies: Object.keys(
      packageJson['devDependencies'] as Record<string, string>
    ),
  };
}

export function getMultiApplicationFile(file: string): string {
  return `/${WORKSPACE_OPTIONS.newProjectRoot}/${MULTI_APPLICATION_OPTIONS.name}/${file}`;
}
export function getMultiLibraryFile(file: string): string {
  return `/${WORKSPACE_OPTIONS.newProjectRoot}/${MULTI_LIBRARY_OPTIONS.name}/${file}`;
}

export async function buildTestingTree(
  command: 'ng-add' | 'e2e' | 'config',
  type: 'single' | 'multi' = 'single',
  userOptions?: Record<string, unknown>
): Promise<UnitTestTree> {
  const runner = new SchematicTestRunner(
    'schematics',
    join(__dirname, '../../lib/schematics/collection.json')
  );
  const options = {
    testRunner: 'jasmine',
    ...userOptions,
  };
  let workingTree: UnitTestTree;

  // Build workspace
  if (type === 'single') {
    workingTree = await runner.runExternalSchematic(
      '@schematics/angular',
      'ng-new',
      SINGLE_APPLICATION_OPTIONS
    );
  } else {
    // Build workspace
    workingTree = await runner.runExternalSchematic(
      '@schematics/angular',
      'workspace',
      WORKSPACE_OPTIONS
    );
    // Build dummy application
    workingTree = await runner.runExternalSchematic(
      '@schematics/angular',
      'application',
      MULTI_APPLICATION_OPTIONS,
      workingTree
    );
    // Build dummy library
    workingTree = await runner.runExternalSchematic(
      '@schematics/angular',
      'library',
      MULTI_LIBRARY_OPTIONS,
      workingTree
    );
  }

  if (command !== 'ng-add') {
    // We want to create update the proper files with `ng-add`
    // First else the angular.json will have wrong data
    workingTree = await runner.runSchematic('ng-add', options, workingTree);
  }

  return await runner.runSchematic(command, options, workingTree);
}

export async function runSchematic(
  tree: UnitTestTree,
  command: 'ng-add' | 'test',
  options?: Record<string, any>
): Promise<UnitTestTree> {
  const runner = new SchematicTestRunner(
    'schematics',
    join(__dirname, '../../lib/schematics/collection.json')
  );
  return await runner.runSchematic(command, options, tree);
}