Skip to content

Commit

Permalink
Port FormEntryPromptTest.java
Browse files Browse the repository at this point in the history
- Adds proposed `Scenario` API with roughly equivalent semantics to the ported tests’ calls into JavaRosa internals
- Includes supplemental tests and corresponding `Scenario` API proposal, asserting **all** of the available select items’ labels against the same form definitions in the ported tests.
  • Loading branch information
eyelidlessness committed May 16, 2024
1 parent e314cec commit 410a6c9
Show file tree
Hide file tree
Showing 2 changed files with 505 additions and 1 deletion.
70 changes: 69 additions & 1 deletion packages/scenario/src/jr/Scenario.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { XFormsElement } from '@odk-web-forms/common/test/fixtures/xform-dsl/XFormsElement.ts';
import type { AnyNode, RootNode } from '@odk-web-forms/xforms-engine';
import type { AnyNode, RootNode, SelectNode } from '@odk-web-forms/xforms-engine';
import type { Accessor, Setter } from 'solid-js';
import { createMemo, createSignal, runWithOwner } from 'solid-js';
import { afterEach, expect } from 'vitest';
Expand All @@ -17,6 +17,7 @@ import type { EndOfFormEvent } from './event/EndOfFormEvent.ts';
import { PositionalEvent } from './event/PositionalEvent.ts';
import type { QuestionNodeType } from './event/QuestionEvent.ts';
import { RepeatInstanceEvent } from './event/RepeatInstanceEvent.ts';
import type { SelectQuestionEvent } from './event/SelectQuestionEvent.ts';
import {
getPositionalEvents,
type AnyPositionalEvent,
Expand Down Expand Up @@ -655,6 +656,73 @@ export class Scenario {
return label;
}

private getCurrentSelectNode(options: AssertCurrentReferenceOptions): SelectNode {
const { assertCurrentReference } = options;
const event = this.getSelectedPositionalEvent();

this.assertReference(event, assertCurrentReference);

if (event.eventType !== 'QUESTION') {
throw new Error(`Expected a question event, got ${event.eventType}`);
}

const { node } = event;
const { currentState, nodeType } = node;

if (nodeType !== 'select') {
throw new Error(`Expected node at ${currentState.reference} to be a select, got ${nodeType}`);
}

return node;
}

/**
* **PORTING NOTES**
*
* Proposed as a close semantic equivalent to JavaRosa's
* `FormEntryPrompt.getAnswerText`, as used in a testing context.
*
* This method is expected to be called when the current positional state is a
* reference to a {@link SelectQuestionEvent}, and will assert that based on
* the provided {@link AssertCurrentReferenceOptions.assertCurrentReference}
* option.
*
* The method's return value will be an array of strings, mapping each
* **selected item** (items included in the question node's
* `currentState.value`) to that item's label, as a serialized string.
*
* @todo At present, this maps the selected items their labels **if they have
* one**, otherwise falling back to their value. This is understood to be the
* intent for form display, but it's treated as a client concern. It may be
* something we want the engine to handle in the future, for greater
* inter-client consistency and to reduce the amount of test surface area
* that's effectively testing {@link Scenario}'s own behavior as a client.
*/
proposed_getSelectedOptionLabelsAsText(
options: AssertCurrentReferenceOptions
): readonly string[] {
const node = this.getCurrentSelectNode(options);

return node.currentState.value.map((item) => {
return item.label?.asString ?? item.value;
});
}

/**
* **PORTING NOTES**
*
* Used in supplemental tests, added to check **all** of a select's
* item/option labels, corresponding to tests calling
* {@link proposed_getSelectedOptionLabelsAsText}.
*/
proposed_getAvailableOptionLabels(options: AssertCurrentReferenceOptions): readonly string[] {
const node = this.getCurrentSelectNode(options);

return node.currentState.valueOptions.map((item) => {
return item.label?.asString ?? item.value;
});
}

// TODO: consider adapting tests which use the following interfaces to use
// more portable concepts (either by using conceptually similar `Scenario`
// APIs, or by reframing the tests' logic to the same behavioral concerns with
Expand Down
Loading

0 comments on commit 410a6c9

Please sign in to comment.