Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions packages/@coorpacademy-player-store/src/utils/state-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
toString as _toString,
isNil
} from 'lodash/fp';
// eslint-disable-next-line lodash-fp/use-fp
import {template} from 'lodash';
import type {
Answer,
Choice,
Expand Down Expand Up @@ -393,14 +391,15 @@ export const getCurrentExitNode = (state: State): ExitNode | void => {
const ref = progression.state.nextContent.ref;
const counters = getOr({}, 'state.variables', progression);

const translateWithCounters = templateValue =>
templateValue
? // eslint-disable-next-line lodash-fp/no-extraneous-args
template(templateValue, {
interpolate: /{{([\s\S]+?)}}/g,
imports: {}
})(counters)
: null;
// Use simple string replacement instead of lodash template
// to avoid SyntaxError when content contains HTML (e.g., <br/>)
const translateWithCounters = (templateValue: ?string): ?string => {
if (!templateValue) return null;
return templateValue.replace(/{{([\s\S]+?)}}/g, (match: string, key: string): string => {
const trimmedKey = key.trim();
return counters[trimmedKey] !== undefined ? counters[trimmedKey] : match;
});
};

return pipe(
getExitNode(ref),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,48 @@ test('getCurrentExitNode should get current exit node from state with counters',
});
});

test('getCurrentExitNode should keep unmatched template variables', t => {
const exitNode = {
ref: 'successExitNode',
title: 'Score: {{ score }}, Unknown: {{ unknownVar }}'
};
const progression = {
state: {nextContent: {ref: 'successExitNode'}, variables: {score: 5}}
};
const state = pipe(
set('ui.current.progressionId', '0'),
set('data.progressions.entities', {0: progression}),
set('data.exitNodes.entities', {successExitNode: exitNode})
)({});

t.deepEqual(getCurrentExitNode(state), {
ref: 'successExitNode',
title: 'Score: 5, Unknown: {{ unknownVar }}'
});
});

test('getCurrentExitNode should handle HTML content without crashing', t => {
const exitNode = {
ref: 'successExitNode',
title: 'Congratulations!',
description: 'Your transcript will be updated.<br/>Thanks for your patience.'
};
const progression = {
state: {nextContent: {ref: 'successExitNode'}, variables: {}}
};
const state = pipe(
set('ui.current.progressionId', '0'),
set('data.progressions.entities', {0: progression}),
set('data.exitNodes.entities', {successExitNode: exitNode})
)({});

t.deepEqual(getCurrentExitNode(state), {
ref: 'successExitNode',
title: 'Congratulations!',
description: 'Your transcript will be updated.<br/>Thanks for your patience.'
});
});

test('getCurrentExitNode should return undefined if no progression is found', t => {
const state = set('ui.current.progressionId', '0')({});
t.is(getCurrentExitNode(state), undefined);
Expand Down