Skip to content

Commit ea37553

Browse files
authored
Merge pull request #207 from simonihmig/enhanced-flushDeprecations
Allow passing a custom handler to flushDeprecations
2 parents a5487ec + 69abb5b commit ea37553

6 files changed

+48
-28
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ addressing a single deprecation at a time, and prevents backsliding
6060

6161
4. Run your test suite\* with `ember test --server`.
6262
5. Navigate to your tests (default: http://localhost:7357/)
63-
6. Run `deprecationWorkflow.flushDeprecations()` in your browsers console.
63+
6. Run `deprecationWorkflow.flushDeprecations()` in your browsers console. Or `flushDeprecations({ handler: 'log' })` if you want a different [handler](#handlers) than the default of `silence`.
6464
7. Copy the string output and overwrite the content of `app/deprecation-workflow.js`.
6565

6666
In Chrome, use right click → "Copy string contents" to avoid escape characters.

addon/index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const LOG_LIMIT = 100;
55
export default function setupDeprecationWorkflow(config) {
66
self.deprecationWorkflow = self.deprecationWorkflow || {};
77
self.deprecationWorkflow.deprecationLog = {
8-
messages: {},
8+
messages: new Set(),
99
};
1010

1111
registerDeprecationHandler((message, options, next) =>
@@ -53,12 +53,16 @@ export function detectWorkflow(config, message, options) {
5353
}
5454
}
5555

56-
export function flushDeprecations() {
56+
export function flushDeprecations({ handler = 'silence' } = {}) {
5757
let messages = self.deprecationWorkflow.deprecationLog.messages;
5858
let logs = [];
5959

60-
for (let message in messages) {
61-
logs.push(messages[message]);
60+
for (let id of messages.values()) {
61+
logs.push(
62+
` { handler: ${JSON.stringify(handler)}, matchId: ${JSON.stringify(
63+
id,
64+
)} }`,
65+
);
6266
}
6367

6468
let deprecations = logs.join(',\n') + '\n';
@@ -110,11 +114,7 @@ export function handleDeprecationWorkflow(config, message, options, next) {
110114
}
111115

112116
export function deprecationCollector(message, options, next) {
113-
let key = (options && options.id) || message;
114-
let matchKey = options && key === options.id ? 'matchId' : 'matchMessage';
115-
116-
self.deprecationWorkflow.deprecationLog.messages[key] =
117-
' { handler: "silence", ' + matchKey + ': ' + JSON.stringify(key) + ' }';
117+
self.deprecationWorkflow.deprecationLog.messages.add(options.id);
118118

119119
next(message, options);
120120
}

tests/acceptance/flush-deprecations-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module('flushDeprecations', function (hooks) {
1010
});
1111

1212
hooks.afterEach(function () {
13-
window.deprecationWorkflow.deprecationLog = { messages: {} };
13+
window.deprecationWorkflow.deprecationLog = { messages: new Set() };
1414
window.Testem.handleConsoleMessage = originalWarn;
1515
});
1616

tests/acceptance/workflow-config-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module('workflow config', function (hooks) {
1212
});
1313

1414
hooks.afterEach(function () {
15-
window.deprecationWorkflow.deprecationLog = { messages: {} };
15+
window.deprecationWorkflow.deprecationLog = { messages: new Set() };
1616
window.Testem.handleConsoleMessage = originalWarn;
1717
});
1818

tests/unit/deprecation-collector-test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module('deprecationCollector', function (hooks) {
1717
originalConfig = self.deprecationWorkflow = {
1818
config: null,
1919
deprecationLog: {
20-
messages: {},
20+
messages: new Set(),
2121
},
2222
};
2323
});
@@ -50,10 +50,10 @@ module('deprecationCollector', function (hooks) {
5050
() => {},
5151
);
5252

53-
assert.deepEqual(self.deprecationWorkflow.deprecationLog.messages, {
54-
first: ' { handler: "silence", matchId: "first" }',
55-
second: ' { handler: "silence", matchId: "second" }',
56-
});
53+
assert.deepEqual(
54+
self.deprecationWorkflow.deprecationLog.messages,
55+
new Set(['first', 'second']),
56+
);
5757
});
5858

5959
test('should call next', function (assert) {
@@ -119,9 +119,9 @@ module('deprecationCollector', function (hooks) {
119119
() => {},
120120
);
121121

122-
assert.deepEqual(self.deprecationWorkflow.deprecationLog.messages, {
123-
first: ' { handler: "silence", matchId: "first" }',
124-
second: ' { handler: "silence", matchId: "second" }',
125-
});
122+
assert.deepEqual(
123+
self.deprecationWorkflow.deprecationLog.messages,
124+
new Set(['first', 'second']),
125+
);
126126
});
127127
});

tests/unit/flush-deprecations-test.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ module('flushDeprecations', function (hooks) {
1616
originalConfig = self.deprecationWorkflow = {
1717
config: null,
1818
deprecationLog: {
19-
messages: [],
19+
messages: new Set(),
2020
},
2121
};
2222
});
2323

2424
hooks.afterEach(function () {
2525
self.deprecationWorkflow.config = originalConfig;
26-
self.deprecationWorkflow.deprecationLog = { messages: {} };
26+
self.deprecationWorkflow.deprecationLog = { messages: new Set() };
2727
console.warn = originalWarn;
2828
});
2929

30-
test('calling flushDeprecations returns string of deprecations', function (assert) {
31-
self.deprecationWorkflow.deprecationLog.messages = {
32-
first: ' { handler: "silence", matchId: "first" }',
33-
second: ' { handler: "silence", matchId: "second" }',
34-
};
30+
test('calling flushDeprecations returns workflow config', function (assert) {
31+
self.deprecationWorkflow.deprecationLog.messages = new Set([
32+
'first',
33+
'second',
34+
]);
3535

3636
let deprecationsPayload = flushDeprecations();
3737
assert.strictEqual(
@@ -43,6 +43,26 @@ setupDeprecationWorkflow({
4343
{ handler: "silence", matchId: "first" },
4444
{ handler: "silence", matchId: "second" }
4545
]
46+
});`,
47+
);
48+
});
49+
50+
test('calling flushDeprecations with custom handler returns workflow config', function (assert) {
51+
self.deprecationWorkflow.deprecationLog.messages = new Set([
52+
'first',
53+
'second',
54+
]);
55+
56+
let deprecationsPayload = flushDeprecations({ handler: 'log' });
57+
assert.strictEqual(
58+
deprecationsPayload,
59+
`import setupDeprecationWorkflow from 'ember-cli-deprecation-workflow';
60+
61+
setupDeprecationWorkflow({
62+
workflow: [
63+
{ handler: "log", matchId: "first" },
64+
{ handler: "log", matchId: "second" }
65+
]
4666
});`,
4767
);
4868
});

0 commit comments

Comments
 (0)