Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `Improvement` - Removed "Narrow" ("Thin") mode. On non-mobile viewports, Plus and Block Tunes controls now use a responsive action rail beside the editor content.
- `Improvement` - `paste` config supports filter functions for tag-based substitution
- `Fix` - "Convert to" is not shown in the Inline Toolbar when there are no tools to convert to
- `Fix` - Invoke tool `reset()` methods on `Tools.destroy()`, catching and logging any errors as warnings

### 2.31.7

Expand Down
8 changes: 6 additions & 2 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ export default class Tools extends Module {
* Calls each Tool reset method to clean up anything set by Tool
*/
public destroy(): void {
Object.values(this.available).forEach(async tool => {
Array.from(this.available.values()).forEach(async tool => {
if (_.isFunction(tool.reset)) {
await tool.reset();
try {
await tool.reset();
} catch (e) {
_.log(`Tool «${tool.name}» reset method throws an Error %o`, 'warn', e);
}
}
});
}
Expand Down
56 changes: 56 additions & 0 deletions test/cypress/tests/modules/Tools.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,62 @@ describe('Tools module', () => {
});
});

context('.destroy()', () => {
it('should call Tools reset method on destroy', () => {
const resetStub = cy.stub();

class WithReset {
public static reset = resetStub;
}

const module = constructModule({
defaultBlock: 'withReset',
tools: {
withReset: {
class: WithReset as any,
},
},
});

return module.prepare().then(() => {
module.destroy();

expect(resetStub).to.be.calledOnce;
});
});

it('should catch and log warning if tool reset rejects', () => {
const resetStub = cy.stub().rejects(new Error('Reset error'));
const warnSpy = cy.spy(console, 'warn');

class WithFailingReset {
public static reset = resetStub;
}

const module = constructModule({
defaultBlock: 'withFailingReset',
tools: {
withFailingReset: {
class: WithFailingReset as any,
},
},
});

return module.prepare()
.then(() => {
expect(() => {
module.destroy();
}).not.to.throw();

expect(resetStub).to.be.calledOnce;
})
.then(() => new Promise((resolve) => setTimeout(resolve, 10)))
.then(() => {
expect(warnSpy).to.be.called;
});
});
});

context('collection accessors', () => {
let module: Tools;

Expand Down
Loading