Testing utility for decorations at a range

I’ve come to enjoy/appreciate how https://github.com/ProseMirror/prosemirror-test-builder assists test-writing for position-dependent code, and have recently been wishing something similar existed for decorations as well.

When writing tests against a custom plugin, where the test is trying to evaluate what decorations have been rendered based on some state update/input, it’s currently somewhat complex to dig into the decoration data structure. I’ve read the code a few times, but still feel like I don’t have a good grasp of what I’m reading! Most of the time, what I want to do is to verify that a decoration of type X has been rendered a position Y, with Decoration spec Z (or with rendered DOM element Z).

As far as I can tell, the main place that this mapping between decorations and document positions happens is in the main view rendering code here:

My dream vision, though, would be to have some kind of test utility where the signature looked like the following:

// : (DecorationSet, Node, number, number) → [Decoration]
function decoAtPos(decos, node, from, to) {
  ....
  return foundDecorations;
}

Which would allow easy introspection of decorations rendered at a given range. There might be good reasons why this isn’t possible, or desirable, but I’d be curious to hear what others think? Happy, also, to file this as an RFC if that’s a channel in which to articulate the problem.

Are you trying to test whether the DOM has the drawn decorations in the right place, or whether the decorations themselves were generated correctly? There’s already a bunch of tests for the former in the prosemirror-view module. For the latter, the node shouldn’t matter, and you can just directly use find on the decoration set which should give you the data you are looking for.

I guess now that I’m thinking about it, I’m mostly concerned with their being generated correctly — e.g. I care about positioning, but not where they’re drawn. I’d totally forgotten about DecorationSet.find, and I’ll just use that, thanks!