Skip to content

Commit a284ed3

Browse files
authored
Merge pull request #2974 from jspsych/fix-complex-defaults
Fix defaults for COMPLEX parameter types
2 parents abea847 + 1324361 commit a284ed3

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

.changeset/odd-toes-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jspsych": patch
3+
---
4+
5+
This fixes an issue when a plugin has a COMPLEX parameter and there is a default value specified at the root level of the parameter, rather than for each individual nested parameter (#2972).

packages/jspsych/src/JsPsych.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -781,21 +781,22 @@ export class JsPsych {
781781
for (const param in trial.type.info.parameters) {
782782
// check if parameter is complex with nested defaults
783783
if (trial.type.info.parameters[param].type === ParameterType.COMPLEX) {
784-
if (trial.type.info.parameters[param].array === true) {
784+
// check if parameter is undefined and has a default value
785+
if (typeof trial[param] === "undefined" && trial.type.info.parameters[param].default) {
786+
trial[param] = trial.type.info.parameters[param].default;
787+
}
788+
// if parameter is an array, iterate over each entry after confirming that there are
789+
// entries to iterate over. this is common when some parameters in a COMPLEX type have
790+
// default values and others do not.
791+
if (trial.type.info.parameters[param].array === true && Array.isArray(trial[param])) {
785792
// iterate over each entry in the array
786793
trial[param].forEach(function (ip, i) {
787794
// check each parameter in the plugin description
788795
for (const p in trial.type.info.parameters[param].nested) {
789796
if (typeof trial[param][i][p] === "undefined" || trial[param][i][p] === null) {
790797
if (typeof trial.type.info.parameters[param].nested[p].default === "undefined") {
791798
console.error(
792-
"You must specify a value for the " +
793-
p +
794-
" parameter (nested in the " +
795-
param +
796-
" parameter) in the " +
797-
trial.type +
798-
" plugin."
799+
`You must specify a value for the ${p} parameter (nested in the ${param} parameter) in the ${trial.type.info.name} plugin.`
799800
);
800801
} else {
801802
trial[param][i][p] = trial.type.info.parameters[param].nested[p].default;
@@ -809,11 +810,7 @@ export class JsPsych {
809810
else if (typeof trial[param] === "undefined" || trial[param] === null) {
810811
if (typeof trial.type.info.parameters[param].default === "undefined") {
811812
console.error(
812-
"You must specify a value for the " +
813-
param +
814-
" parameter in the " +
815-
trial.type.info.name +
816-
" plugin."
813+
`You must specify a value for the ${param} parameter in the ${trial.type.info.name} plugin.`
817814
);
818815
} else {
819816
trial[param] = trial.type.info.parameters[param].default;

packages/jspsych/tests/core/default-parameters.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import surveyText from "@jspsych/plugin-survey-text";
22
import { startTimeline } from "@jspsych/test-utils";
33

4+
import jsPsychTestComplex from "./test-complex-plugin";
5+
46
describe("nested defaults", () => {
57
test("work in basic situation", async () => {
68
const { displayElement } = await startTimeline([
@@ -48,3 +50,21 @@ describe("nested defaults", () => {
4850
spy.mockRestore();
4951
});
5052
});
53+
54+
describe("defaults for COMPLEX parameters", () => {
55+
test("default at the top level should work", async () => {
56+
const { expectFinished, getData } = await startTimeline([
57+
{
58+
type: jsPsychTestComplex,
59+
},
60+
]);
61+
62+
await expectFinished();
63+
64+
expect(getData().values()[0].blocks).toEqual([
65+
{ x: 10, y: 10 },
66+
{ x: 20, y: 20 },
67+
{ x: 30, y: 30 },
68+
]);
69+
});
70+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { JsPsych, JsPsychPlugin, ParameterType, TrialType } from "jspsych";
2+
3+
const info = <const>{
4+
name: "test-complex-plugin",
5+
parameters: {
6+
blocks: {
7+
type: ParameterType.COMPLEX,
8+
array: true,
9+
default: [
10+
{ x: 10, y: 10 },
11+
{ x: 20, y: 20 },
12+
{ x: 30, y: 30 },
13+
],
14+
nested: {
15+
x: {
16+
type: ParameterType.INT,
17+
default: undefined,
18+
},
19+
y: {
20+
type: ParameterType.INT,
21+
default: undefined,
22+
},
23+
},
24+
},
25+
},
26+
};
27+
28+
type Info = typeof info;
29+
30+
class TestComplexPlugin implements JsPsychPlugin<Info> {
31+
static info = info;
32+
33+
constructor(private jsPsych: JsPsych) {}
34+
35+
trial(display_element: HTMLElement, trial: TrialType<Info>) {
36+
// save data
37+
var trialdata = {
38+
blocks: trial.blocks,
39+
};
40+
41+
// next trial
42+
this.jsPsych.finishTrial(trialdata);
43+
}
44+
}
45+
46+
export default TestComplexPlugin;

0 commit comments

Comments
 (0)