forked from emberjs/ember-test-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecations.ts
112 lines (97 loc) · 3.8 KB
/
deprecations.ts
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
import { BaseContext } from '../setup-context';
import { registerDeprecationHandler } from '@ember/debug';
import isPromise from './is-promise';
export interface DeprecationOptions {
id: string;
until: string;
for?: string;
since?: {
available: string;
};
}
export interface DeprecationFailure {
message: string;
options?: DeprecationOptions;
}
const DEPRECATIONS = new WeakMap<BaseContext, Array<DeprecationFailure>>();
/**
*
* Provides the list of deprecation failures associated with a given base context;
*
* @private
* @param {BaseContext} [context] the test context
* @return {Array<DeprecationFailure>} the Deprecation Failures associated with the corresponding BaseContext;
*/
export function getDeprecationsForContext(
context: BaseContext
): Array<DeprecationFailure> {
if (!context) {
throw new TypeError(
`[@ember/test-helpers] could not get deprecations for an invalid test context: '${context}'`
);
}
let deprecations = DEPRECATIONS.get(context);
if (!Array.isArray(deprecations)) {
deprecations = [];
DEPRECATIONS.set(context, deprecations);
}
return deprecations;
}
/**
*
* Provides the list of deprecation failures associated with a given base
* context which occure while a callback is executed. This callback can be
* synchonous, or it can be an async function.
*
* @private
* @param {BaseContext} [context] the test context
* @param {Function} [callback] The callback that when executed will have its DeprecationFailure recorded
* @return {Array<DeprecationFailure>} The Deprecation Failures associated with the corresponding baseContext which occured while the CallbackFunction was executed
*/
export function getDeprecationsDuringCallbackForContext(
context: BaseContext,
callback: () => void
): Array<DeprecationFailure> | Promise<Array<DeprecationFailure>> {
if (!context) {
throw new TypeError(
`[@ember/test-helpers] could not get deprecations for an invalid test context: '${context}'`
);
}
const deprecations = getDeprecationsForContext(context);
const previousLength = deprecations.length;
const result = callback();
if (isPromise(result)) {
return Promise.resolve(result).then(() => {
return deprecations.slice(previousLength); // only return deprecations created as a result of the callback
});
} else {
return deprecations.slice(previousLength); // only return deprecations created as a result of the callback
}
}
// This provides (when the environment supports) queryParam support for deprecations:
// * squelch deprecations by name via: `/tests/index.html?disabledDeprecations=this-property-fallback,some-other-thing`
// * enable a debuggger when a deprecation by a specific name is encountered via: `/tests/index.html?debugDeprecations=some-other-thing` when the
if (typeof URLSearchParams !== 'undefined') {
let queryParams = new URLSearchParams(document.location.search.substring(1));
const disabledDeprecations = queryParams.get('disabledDeprecations');
const debugDeprecations = queryParams.get('debugDeprecations');
// When using `/tests/index.html?disabledDeprecations=this-property-fallback,some-other-thing`
// those deprecations will be squelched
if (disabledDeprecations) {
registerDeprecationHandler((message, options, next) => {
if (!options || !disabledDeprecations.includes(options.id)) {
next.apply(null, [message, options]);
}
});
}
// When using `/tests/index.html?debugDeprecations=some-other-thing` when the
// `some-other-thing` deprecation is triggered, this `debugger` will be hit`
if (debugDeprecations) {
registerDeprecationHandler((message, options, next) => {
if (options && debugDeprecations.includes(options.id)) {
debugger; // eslint-disable-line no-debugger
}
next.apply(null, [message, options]);
});
}
}