Skip to content

Commit

Permalink
Scenario: add test for skipping validation of non-relevant nodes (at …
Browse files Browse the repository at this point in the history
…node and form level)
  • Loading branch information
eyelidlessness committed Jul 12, 2024
1 parent 23171c7 commit a385483
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/scenario/src/assertion/extensions/answers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const answerExtensions = extendExpect(expect, {
return new UnreachableError(expected);
}

return pass || new InspectableComparisonError(condition, expected, 'be');
return pass || new InspectableComparisonError(actual, expected, 'be');
}
),

Expand Down
7 changes: 7 additions & 0 deletions packages/scenario/src/assertion/extensions/node-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ const nodeStateExtensions = extendExpect(expect, {
currentState: { relevant: false },
}),

toBeRequired: new StaticConditionExpectExtension(assertEngineNode, {
currentState: { required: true },
}),
toBeOptional: new StaticConditionExpectExtension(assertEngineNode, {
currentState: { required: false },
}),

/**
* **PORTING NOTES**
*
Expand Down
87 changes: 87 additions & 0 deletions packages/scenario/test/validity-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AnswerResult, Scenario } from '../src/jr/Scenario.ts';
import { r } from '../src/jr/resource/ResourcePathHelper.ts';
import {
ANSWER_CONSTRAINT_VIOLATED,
ANSWER_OK,
ANSWER_REQUIRED_BUT_EMPTY,
} from '../src/jr/validation/ValidateOutcome.ts';

Expand Down Expand Up @@ -446,3 +447,89 @@ describe('Validity messages', () => {
expect(result).toHaveConstraintMessage(null);
});
});

describe('Validation and relevance', () => {
it('does not produce validation errors for non-relevant questions', async () => {
const scenario = await Scenario.init(
'Validation and relevance',
html(
head(
title('Validation and relevance'),
// prettier-ignore
model(
mainInstance(
t('data id="validation-and-relevance"',
t('a', 'default value'),
t('b', 'A'),
t('validated-when-relevant')
)
),
bind('/data/a'),
bind('/data/b'),
bind('/data/validated-when-relevant')
.relevant("/data/a != 'default value'")
.required('/data/b > 10')
)
),
body(input('/data/a'), input('/data/b'), input('/data/validated-when-relevant'))
)
);

// Sanity check: initially non-relevant and optional
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeNonRelevant();
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeOptional();

// Sanity check: no validation errors while optional
expect(scenario.answerOf('/data/validated-when-relevant')).toHaveValidityStatus(
AnswerResult.OK
);

// Make required condition effective
scenario.answer('/data/b', 11);

// Sanity check: still non-relevant, now required
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeNonRelevant();
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeRequired();

// Check form has no validation error while non-relevant
let validate = scenario.getValidationOutcome();

expect(validate.failedPrompt).toBeNull();
expect(validate.outcome).toBe(ANSWER_OK);

// Check answer/node has no validation error while non-relevant
expect(scenario.answerOf('/data/validated-when-relevant')).toHaveValidityStatus(
AnswerResult.OK
);

// Make relevant, sanity check
scenario.answer('/data/a', 'another value, not the default!');
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeRelevant();

// Check validation error while relevant
validate = scenario.getValidationOutcome();

expect(validate.failedPrompt).toBe(scenario.indexOf('/data/validated-when-relevant'));
expect(validate.outcome).toBe(ANSWER_REQUIRED_BUT_EMPTY);

// Check answer/node has no validation error while non-relevant
expect(scenario.answerOf('/data/validated-when-relevant')).toHaveValidityStatus(
AnswerResult.REQUIRED_BUT_EMPTY
);

// Make non-relevant again
scenario.answer('/data/a', 'default value');
expect(scenario.getInstanceNode('/data/validated-when-relevant')).toBeNonRelevant();

// Check form has no validation error again
validate = scenario.getValidationOutcome();

expect(validate.failedPrompt).toBeNull();
expect(validate.outcome).toBe(ANSWER_OK);

// Check answer/node has no validation error again
expect(scenario.answerOf('/data/validated-when-relevant')).toHaveValidityStatus(
AnswerResult.OK
);
});
});

0 comments on commit a385483

Please sign in to comment.