Skip to content

Commit

Permalink
fix(core) Nested chain not preserving dispatch state (#4152)
Browse files Browse the repository at this point in the history
* Fix nested chain not preserving dispatch state

* Change test to read as sentence
  • Loading branch information
C-Hess committed Jul 7, 2023
1 parent b24df3a commit 26610cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class CommandManager {
transaction: tr,
}),
dispatch: shouldDispatch ? () => undefined : undefined,
chain: () => this.createChain(tr),
chain: () => this.createChain(tr, shouldDispatch),
can: () => this.createCan(tr),
get commands() {
return Object.fromEntries(
Expand Down
28 changes: 28 additions & 0 deletions tests/cypress/integration/core/can.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,32 @@ describe('can', () => {

expect(canSetMarkToBold).to.eq(true)
})

it('builds and passes down an undefined dispatch for nested "can" chain', () => {
const editor = new Editor({
extensions: [Document, Paragraph, Text, History],
})

let capturedOuterDispatch: ((args?: any) => any) | undefined
let capturedInnerDispatch: ((args?: any) => any) | undefined

editor
.can()
.chain()
.command(({ chain, dispatch: outterDispatch }) => {
capturedOuterDispatch = outterDispatch
return chain()
.command(({ dispatch: innerDispatch }) => {
capturedInnerDispatch = innerDispatch
return true
})
.run()
})
.run()

// eslint-disable-next-line no-unused-expressions
expect(capturedOuterDispatch).to.be.undefined
// eslint-disable-next-line no-unused-expressions
expect(capturedInnerDispatch).to.be.undefined
})
})

0 comments on commit 26610cd

Please sign in to comment.