Add regression test for modifier.getFormula respecting enabled

This commit is contained in:
thepaperpilot 2024-02-13 06:48:56 -06:00 committed by thepaperpilot
parent cba79df80d
commit 4092cd6d56
2 changed files with 27 additions and 5 deletions

View file

@ -276,8 +276,12 @@ export function createExponentialModifier<T extends ExponentialModifierOptions>(
export function createSequentialModifier< export function createSequentialModifier<
T extends Modifier[], T extends Modifier[],
S = T extends WithRequired<Modifier, "invert">[] S = T extends WithRequired<Modifier, "invert">[]
? WithRequired<Modifier, "description" | "invert"> ? T extends WithRequired<Modifier, "getFormula">[]
: Omit<WithRequired<Modifier, "description">, "invert"> ? WithRequired<Modifier, "description" | "invert" | "getFormula">
: Omit<WithRequired<Modifier, "description" | "getFormula">, "invert">
: T extends WithRequired<Modifier, "invert">[]
? WithRequired<Modifier, "getFormula" | "invert">
: Omit<WithRequired<Modifier, "getFormula">, "invert">
>(modifiersFunc: () => T): S { >(modifiersFunc: () => T): S {
return createLazyProxy(() => { return createLazyProxy(() => {
const modifiers = modifiersFunc(); const modifiers = modifiersFunc();
@ -296,10 +300,14 @@ export function createSequentialModifier<
: undefined, : undefined,
getFormula: modifiers.every(m => m.getFormula != null) getFormula: modifiers.every(m => m.getFormula != null)
? (gain: FormulaSource) => ? (gain: FormulaSource) =>
modifiers modifiers.reduce((acc, curr) => {
if (curr.enabled == null || curr.enabled === true) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.reduce((acc, curr) => Formula.if(acc, curr.enabled ?? true, return curr.getFormula!(acc);
acc => curr.getFormula!(acc), acc => acc), gain) }
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return Formula.if(acc, curr.enabled, acc => curr.getFormula!(acc));
}, gain)
: undefined, : undefined,
enabled: modifiers.some(m => m.enabled != null) enabled: modifiers.some(m => m.enabled != null)
? computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0) ? computed(() => modifiers.filter(m => unref(m.enabled) !== false).length > 0)

View file

@ -199,6 +199,20 @@ describe("Sequential Modifiers", () => {
// So long as one is true or undefined, enable should be true // So long as one is true or undefined, enable should be true
expect(unref(modifier.enabled)).toBe(true); expect(unref(modifier.enabled)).toBe(true);
}); });
test("respects enabled", () => {
const value = ref(10);
const enabled = ref(false);
const modifier = createSequentialModifier(() => [
createMultiplicativeModifier(() => ({ multiplier: 5, enabled }))
]);
expect(modifier.getFormula(Formula.variable(value)).evaluate()).compare_tolerance(
value.value
);
enabled.value = true;
expect(modifier.getFormula(Formula.variable(value)).evaluate()).not.compare_tolerance(
value.value
);
});
}); });
describe("applies smallerIsBetter correctly", () => { describe("applies smallerIsBetter correctly", () => {