• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

help-me-mom / ng-mocks / 2669be48-d3c8-47f1-9e0c-601da6ef1817

28 Oct 2024 11:18PM CUT coverage: 100.0%. Remained the same
2669be48-d3c8-47f1-9e0c-601da6ef1817

Pull #10314

circleci

web-flow
chore(e2e/min): update dependency @types/node to v20.17.2
Pull Request #10314: chore(e2e/min): update dependency @types/node to v20.17.2

3269 of 3269 branches covered (100.0%)

Branch coverage included in aggregate %.

4834 of 4834 relevant lines covered (100.0%)

8529.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/libs/ng-mocks/src/lib/common/core.helpers.ts
1
import { getTestBed } from '@angular/core/testing';
1✔
2

3
import coreDefineProperty from './core.define-property';
1✔
4
import coreReflectParametersResolve from './core.reflect.parameters-resolve';
1✔
5
import { AnyDeclaration, AnyType, Type } from './core.types';
6
import funcGetGlobal from './func.get-global';
1✔
7
import funcGetName from './func.get-name';
1✔
8
import ngMocksUniverse from './ng-mocks-universe';
1✔
9

10
/**
11
 * It will be removed from public interface with the next release: A14
12
 * Use ngMocks.get(token) instead.
13
 *
14
 * @deprecated
15
 * @internal
16
 */
17
export const getTestBedInjection = <I>(token: AnyDeclaration<I>): I | undefined => {
1✔
18
  try {
34✔
19
    // istanbul ignore next
20
    return getInjection(token);
21
  } catch {
22
    return undefined;
23✔
23
  }
24
};
25

26
/**
27
 * It will be removed from public interface with the next release: A14
28
 *
29
 * @deprecated
30
 * @internal
31
 */
32
export const getInjection = <I>(token: AnyDeclaration<I>): I => {
1✔
33
  const testBed: any = getTestBed();
165✔
34

35
  // istanbul ignore next
36
  return testBed.inject ? testBed.inject(token) : testBed.get(token);
37
};
38

39
export const flatten = <T>(values: T | T[] | { ɵproviders: T[] }, result: T[] = []): T[] => {
440,707✔
40
  if (Array.isArray(values)) {
351,782✔
41
    for (const value of values) {
262,667✔
42
      flatten(value, result);
262,667✔
43
    }
44
  } else if (values !== null && typeof values === 'object' && Array.isArray((values as any).ɵproviders)) {
228,042✔
45
    for (const value of (values as any).ɵproviders) {
190✔
46
      flatten(value, result);
190✔
47
    }
48
  } else {
49
    // any is needed to cover ɵproviders
50
    result.push(values as any);
228,023✔
51
  }
52

53
  return result;
351,782✔
54
};
55

56
export const mapKeys = <T>(set: Map<T, any>): T[] => {
1✔
57
  const result: T[] = [];
591✔
58
  // eslint-disable-next-line unicorn/no-array-for-each
59
  set.forEach((_, value: T) => result.push(value));
6,177✔
60

61
  return result;
591✔
62
};
63

64
export const mapValues = <T>(set: { forEach(a1: (value: T) => void): void }, destination?: Set<T>): T[] => {
1✔
65
  const result: T[] = [];
38,896✔
66
  if (destination) {
38,896✔
67
    // eslint-disable-next-line unicorn/no-array-for-each
68
    set.forEach((value: T) => {
6,914✔
69
      destination.add(value);
5,122✔
70
    });
71
  } else {
72
    // eslint-disable-next-line unicorn/no-array-for-each
73
    set.forEach((value: T) => {
31,982✔
74
      result.push(value);
86,541✔
75
    });
76
  }
77

78
  return result;
38,896✔
79
};
80

81
export const mapEntries = <K, T>(set: Map<K, T>, destination?: Map<K, T>): Array<[K, T]> => {
1✔
82
  const result: Array<[K, T]> = [];
15,801✔
83

84
  if (destination) {
15,801✔
85
    // eslint-disable-next-line unicorn/no-array-for-each
86
    set.forEach((value: T, key: K) => destination.set(key, value));
4,280✔
87
  } else {
88
    // eslint-disable-next-line unicorn/no-array-for-each
89
    set.forEach((value: T, key: K) => result.push([key, value]));
205,272✔
90
  }
91

92
  return result;
15,801✔
93
};
94

95
const extractDependencyArray = (deps: any[], set: Set<any>): void => {
1✔
96
  for (const flag of deps) {
130✔
97
    const name = flag && typeof flag === 'object' ? flag.ngMetadataName : undefined;
130✔
98
    if (name === 'Optional' || name === 'SkipSelf' || name === 'Self') {
130✔
99
      continue;
88✔
100
    }
101
    set.add(flag);
42✔
102
  }
103
};
104

105
// Accepts an array of dependencies from providers, skips injections flags,
106
// and adds the providers to the set.
107
export const extractDependency = (deps: any[], set?: Set<any>): void => {
1✔
108
  if (!set) {
8,256✔
109
    return;
275✔
110
  }
111
  for (const dep of deps) {
11,220✔
112
    if (!Array.isArray(dep)) {
11,220✔
113
      set.add(dep);
11,178✔
114
      continue;
11,178✔
115
    }
116
    extractDependencyArray(dep, set);
42✔
117
  }
118
};
119

120
export const extendClassicClass = <I>(base: AnyType<I>): Type<I> => {
1✔
121
  let child: any;
122
  const index = ngMocksUniverse.index();
3,410✔
123

124
  const glb = funcGetGlobal();
3,410✔
125
  glb.ngMocksParent = base;
3,410✔
126

127
  // First we try to eval es2015 style and if it fails to use es5 transpilation in the catch block.
128
  // The next step is to respect constructor parameters as the parent class via jitReflector.
129
  // istanbul ignore next
130
  try {
131
    eval(`
132
      var glb = typeof window === 'undefined' ? global : window;
133
      class MockMiddleware${index} extends glb.ngMocksParent {};
134
      glb.ngMocksResult = MockMiddleware${index};
135
    `);
136
    child = glb.ngMocksResult;
137
  } catch {
138
    class MockMiddleware extends glb.ngMocksParent {}
139
    child = MockMiddleware;
140
  } finally {
141
    glb.ngMocksResult = undefined;
142
    glb.ngMocksParent = undefined;
143
  }
144

145
  // A16: adding unique property.
146
  coreDefineProperty(child.prototype, `__ngMocks_index_${index}`, undefined, false);
3,410✔
147

148
  return child;
3,410✔
149
};
150

151
export const extendClass = <I>(base: AnyType<I>): Type<I> => {
1✔
152
  const child: Type<I> = extendClassicClass(base);
3,410✔
153
  coreDefineProperty(child, 'name', `MockMiddleware${funcGetName(base)}`, true);
3,410✔
154

155
  const parameters = coreReflectParametersResolve(base);
3,410✔
156
  if (parameters.length > 0) {
3,410✔
157
    coreDefineProperty(child, 'parameters', [...parameters]);
3,250✔
158
  }
159

160
  return child;
3,410✔
161
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc