Skip to content

Commit

Permalink
Convert many tagSpecific tests to vitest (#255)
Browse files Browse the repository at this point in the history
* properly type core in tests
* descendent composites of text must have a replacement
* bug fixes to handle the ["ldots"] math-expressions and to allow lists from props to change.
* mock spreadsheet component's hyperformula call for vitest
  • Loading branch information
dqnykamp authored Nov 11, 2024
1 parent 62ab001 commit 7678ab1
Show file tree
Hide file tree
Showing 118 changed files with 32,929 additions and 83,047 deletions.
7 changes: 7 additions & 0 deletions packages/doenetml-worker/__mocks__/hyperformula.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { vi } from "vitest";

/** Mock the call to hyperformula that occurs inside the evaluatedCells of spreadsheet.
* The evaluatedCells variable will always be empty, but at least tests can run.*/
export const HyperFormula = {
buildFromArray: vi.fn(() => ({ getSheetValues: vi.fn() })),
};
2 changes: 1 addition & 1 deletion packages/doenetml-worker/src/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -12068,7 +12068,7 @@ export default class Core {
}

if (stateVarObj.additionalStateVariablesDefined) {
// combine workspaces of additional state varibles into one
// combine workspaces of additional state variables into one
for (let varName2 of stateVarObj.additionalStateVariablesDefined) {
let stateVariableForWorkspace2 = varName2;
let stateVarObj2 = component.state[varName2];
Expand Down
35 changes: 20 additions & 15 deletions packages/doenetml-worker/src/components/BooleanList.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export default class BooleanList extends CompositeComponent {
warnings.push(...processResult.warnings);

workspace.componentsCopied = componentsCopied;
workspace.numComponents = numComponents;

return {
replacements: processResult.serializedComponents,
Expand All @@ -397,26 +398,30 @@ export default class BooleanList extends CompositeComponent {
let errors = [];
let warnings = [];

let componentsToCopy = [];
let numComponents = await component.stateValues.numComponents;

let childNameByComponent =
await component.stateValues.childNameByComponent;
if (numComponents === workspace.numComponents) {
let componentsToCopy = [];

for (let childName of childNameByComponent) {
let replacementSource = components[childName];
let childNameByComponent =
await component.stateValues.childNameByComponent;

if (replacementSource) {
componentsToCopy.push(replacementSource.componentName);
for (let childName of childNameByComponent) {
let replacementSource = components[childName];

if (replacementSource) {
componentsToCopy.push(replacementSource.componentName);
}
}
}

if (
componentsToCopy.length == workspace.componentsCopied.length &&
workspace.componentsCopied.every(
(x, i) => x === componentsToCopy[i],
)
) {
return [];
if (
componentsToCopy.length == workspace.componentsCopied.length &&
workspace.componentsCopied.every(
(x, i) => x === componentsToCopy[i],
)
) {
return [];
}
}

// for now, just recreate
Expand Down
36 changes: 24 additions & 12 deletions packages/doenetml-worker/src/components/ChoiceInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export default class Choiceinput extends Input {
(x) => x + 1,
);
} else {
// if desiredIndices is specfied, use those
// if desiredIndices is specified, use those
let desiredChoiceOrder =
dependencyValues.variants?.desiredVariant?.indices;
if (desiredChoiceOrder !== undefined) {
Expand Down Expand Up @@ -1048,6 +1048,14 @@ export default class Choiceinput extends Input {
globalDependencyValues,
stateValues,
}) {
let desiredSelectedValues =
desiredStateVariableValues.selectedValues;

// if attempt to set to a string, set it as the first desired value
if (typeof desiredSelectedValues === "string") {
desiredSelectedValues = [desiredSelectedValues];
}

let selectMultiple = await stateValues.selectMultiple;

let componentType = globalDependencyValues.componentType;
Expand All @@ -1058,9 +1066,8 @@ export default class Choiceinput extends Input {
];

let foundValue = false;
for (let key in desiredStateVariableValues.selectedValues) {
let desiredVal =
desiredStateVariableValues.selectedValues[key];
for (let key in desiredSelectedValues) {
let desiredVal = desiredSelectedValues[key];
let ind = getIndOfDesiredValue(desiredVal);
if (ind !== -1) {
newSelectedIndices[key] = ind + 1; // 1-indexed
Expand Down Expand Up @@ -1095,8 +1102,7 @@ export default class Choiceinput extends Input {
};
}
} else {
let desiredVal =
desiredStateVariableValues.selectedValues[0];
let desiredVal = desiredSelectedValues[0];
let ind = getIndOfDesiredValue(desiredVal);

if (ind !== -1) {
Expand Down Expand Up @@ -1130,9 +1136,9 @@ export default class Choiceinput extends Input {
}
}
} else {
ind = globalDependencyValues.choiceTexts.indexOf(
desiredVal?.toLowerCase().trim(),
);
ind = globalDependencyValues.choiceTexts
.map((v) => v.toLowerCase().trim())
.indexOf(desiredVal?.toLowerCase().trim());
}
return ind;
}
Expand Down Expand Up @@ -1439,12 +1445,18 @@ export default class Choiceinput extends Input {
skipRendererUpdate = false,
}) {
if (!(await this.stateValues.disabled)) {
let choicesDisabled = await this.stateValues.choicesDisabled;

let effectiveSelectedIndices = selectedIndices.filter(
(v) => !choicesDisabled[v - 1],
);

let updateInstructions = [
{
updateType: "updateValue",
componentName: this.componentName,
stateVariable: "allSelectedIndices",
value: selectedIndices,
value: effectiveSelectedIndices,
},
];

Expand All @@ -1456,8 +1468,8 @@ export default class Choiceinput extends Input {
componentType: this.componentType,
},
result: {
response: selectedIndices,
responseText: selectedIndices.map(
response: effectiveSelectedIndices,
responseText: effectiveSelectedIndices.map(
(i) => choiceTexts[i - 1],
),
},
Expand Down
1 change: 1 addition & 0 deletions packages/doenetml-worker/src/components/MathInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default class MathInput extends Input {
createComponentOfType: "integer",
createStateVariable: "minWidth",
defaultValue: 50,
clamp: [0, Infinity],
public: true,
forRenderer: true,
};
Expand Down
45 changes: 26 additions & 19 deletions packages/doenetml-worker/src/components/MathList.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ export default class MathList extends CompositeComponent {
warnings.push(...processResult.warnings);

workspace.componentsCopied = componentsCopied;
workspace.numComponents = numComponents;

return {
replacements: processResult.serializedComponents,
Expand All @@ -660,30 +661,36 @@ export default class MathList extends CompositeComponent {
let errors = [];
let warnings = [];

let componentsToCopy = [];
let numComponents = await component.stateValues.numComponents;

let childInfoByComponent =
await component.stateValues.childInfoByComponent;
if (numComponents === workspace.numComponents) {
let componentsToCopy = [];

for (let childInfo of childInfoByComponent) {
let replacementSource = components[childInfo.childName];
let childInfoByComponent =
await component.stateValues.childInfoByComponent;

if (childInfo.nComponents !== undefined) {
componentsToCopy.push(
replacementSource.componentName + ":" + childInfo.component,
);
} else {
componentsToCopy.push(replacementSource.componentName);
for (let childInfo of childInfoByComponent) {
let replacementSource = components[childInfo.childName];

if (childInfo.nComponents !== undefined) {
componentsToCopy.push(
replacementSource.componentName +
":" +
childInfo.component,
);
} else {
componentsToCopy.push(replacementSource.componentName);
}
}
}

if (
componentsToCopy.length == workspace.componentsCopied.length &&
workspace.componentsCopied.every(
(x, i) => x === componentsToCopy[i],
)
) {
return [];
if (
componentsToCopy.length == workspace.componentsCopied.length &&
workspace.componentsCopied.every(
(x, i) => x === componentsToCopy[i],
)
) {
return [];
}
}

// for now, just recreate
Expand Down
Loading

0 comments on commit 7678ab1

Please sign in to comment.