Testing Editor State

Hi,

Should I expect the doc to update in this example? I’m trying to test a state extension’s result. Do I have to dispatch the changes through the view to trigger the update?

    let state = EditorState.create({
        doc: "test",
        extensions: [
            contextualKeywordsState,
        ],
    });

    state.update({ changes: { from: 0, insert: "changes" } });
    console.log(state.doc); // TextLeaf { text: [ 'test' ], length: 4 }

    let result = state.field(contextualKeywordsState);
    return result

Thanks again.

state.update will return a transaction and do absolutely nothing to the (immutable) state object. You probably want state.update(...).state.

Ok, I see. In the app architecture which part asks for the new state? Is it the view?

But my immediate question is, my hope is to get the result of a state field but

update.state.field(contextualKeywordsState);

still returns the same value as before. Is there a better way to test a state field’s logic?

Yes.

It will return the value the field has after the transaction. Running transactions like this is a perfectly reasonable way to test state fields. Maybe you have an issue in the field implementation or the test transaction code?

Ok, thanks for the clarification.