-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from getodk/features/engine/trigger
Engine support for `<trigger>`
- Loading branch information
Showing
23 changed files
with
502 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@getodk/scenario": minor | ||
"@getodk/web-forms": minor | ||
"@getodk/xforms-engine": minor | ||
--- | ||
|
||
Engine support for `<trigger>` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { InspectableComparisonError } from '@getodk/common/test/assertions/helpers.ts'; | ||
import type { SimpleAssertionResult } from '@getodk/common/test/assertions/vitest/shared-extension-types.ts'; | ||
import type { TriggerNode } from '@getodk/xforms-engine'; | ||
import type { ComparableAnswer } from './ComparableAnswer.ts'; | ||
import { ValueNodeAnswer } from './ValueNodeAnswer.ts'; | ||
|
||
class TriggerNodeAnswerComparisonError extends Error { | ||
constructor() { | ||
super('Trigger value should be compared with/asserted as a boolean'); | ||
} | ||
} | ||
|
||
export class TriggerNodeAnswer extends ValueNodeAnswer<TriggerNode> { | ||
private readonly triggerValue: boolean; | ||
|
||
override get booleanValue(): boolean { | ||
return this.triggerValue; | ||
} | ||
|
||
constructor(node: TriggerNode) { | ||
super(node); | ||
|
||
this.triggerValue = node.currentState.value; | ||
} | ||
|
||
override equals(expected: ComparableAnswer): SimpleAssertionResult | null { | ||
const { booleanValue } = expected; | ||
|
||
if (booleanValue == null) { | ||
throw new TriggerNodeAnswerComparisonError(); | ||
} | ||
|
||
const pass = this.booleanValue === booleanValue; | ||
|
||
return pass || new InspectableComparisonError(expected, this, 'equal'); | ||
} | ||
|
||
get stringValue(): string { | ||
throw new TriggerNodeAnswerComparisonError(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TriggerNodeAnswer } from '../../answer/TriggerNodeAnswer.ts'; | ||
import { UntypedAnswer } from '../../answer/UntypedAnswer.ts'; | ||
import type { ValueNodeAnswer } from '../../answer/ValueNodeAnswer.ts'; | ||
import { QuestionEvent } from './QuestionEvent.ts'; | ||
|
||
export class TriggerQuestionEvent extends QuestionEvent<'trigger'> { | ||
getAnswer(): TriggerNodeAnswer { | ||
return new TriggerNodeAnswer(this.node); | ||
} | ||
|
||
answerQuestion(answerValue: unknown): ValueNodeAnswer { | ||
const { booleanValue } = new UntypedAnswer(answerValue); | ||
|
||
this.node.setValue(booleanValue); | ||
|
||
return new TriggerNodeAnswer(this.node); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
body, | ||
head, | ||
html, | ||
label, | ||
mainInstance, | ||
model, | ||
t, | ||
title, | ||
} from '@getodk/common/test/fixtures/xform-dsl/index.ts'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { booleanAnswer } from '../src/answer/ExpectedBooleanAnswer.ts'; | ||
import { Scenario } from '../src/jr/Scenario.ts'; | ||
|
||
describe('<trigger>', () => { | ||
type TriggerValue = '' | 'OK'; | ||
|
||
interface TriggerCase { | ||
readonly initialFormDefinitionValue: TriggerValue; | ||
readonly expectedInitialValue: boolean; | ||
readonly assignState: boolean; | ||
readonly expectedAssignedValue: boolean; | ||
} | ||
|
||
it.each<TriggerCase>([ | ||
{ | ||
initialFormDefinitionValue: '', | ||
expectedInitialValue: false, | ||
assignState: true, | ||
expectedAssignedValue: true, | ||
}, | ||
{ | ||
initialFormDefinitionValue: 'OK', | ||
expectedInitialValue: true, | ||
assignState: false, | ||
expectedAssignedValue: false, | ||
}, | ||
{ | ||
initialFormDefinitionValue: 'OK', | ||
expectedInitialValue: true, | ||
assignState: true, | ||
expectedAssignedValue: true, | ||
}, | ||
{ | ||
initialFormDefinitionValue: '', | ||
expectedInitialValue: false, | ||
assignState: false, | ||
expectedAssignedValue: false, | ||
}, | ||
])( | ||
'supports ODK XForms trigger semantics (initial state: $initialFormDefinitionValue; expected initial: $expectedInitialValue; assign state: $assignState; expected assigned: $expectedAssignedValue)', | ||
async ({ | ||
initialFormDefinitionValue, | ||
expectedInitialValue, | ||
assignState, | ||
expectedAssignedValue, | ||
}) => { | ||
const scenario = await Scenario.init( | ||
'Form with trigger', | ||
// prettier-ignore | ||
html( | ||
head( | ||
title('Form with trigger'), | ||
model( | ||
mainInstance(t("data id='multilingual-select'", | ||
t('maybe-ok', initialFormDefinitionValue)))) | ||
), | ||
body( | ||
t('trigger ref="/data/maybe-ok"', | ||
label('Maybe OK')) | ||
) | ||
) | ||
); | ||
|
||
expect(scenario.answerOf('/data/maybe-ok')).toEqualAnswer( | ||
booleanAnswer(expectedInitialValue) | ||
); | ||
|
||
scenario.answer('/data/maybe-ok', assignState); | ||
|
||
expect(scenario.answerOf('/data/maybe-ok')).toEqualAnswer( | ||
booleanAnswer(expectedAssignedValue) | ||
); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.