Skip to content

Commit

Permalink
Split by '.' only once when parsing 'dataSource' for a text resource …
Browse files Browse the repository at this point in the history
…variable (#2802)
  • Loading branch information
martinothamar authored Dec 12, 2024
1 parent bcbe457 commit f936ff6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
57 changes: 30 additions & 27 deletions src/features/formData/FormDataReaders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,37 +160,40 @@ describe('FormDataReaders', () => {
.mockName('window.logErrorOnce');
});

it('simple, should render a resource with a variable lookup', async () => {
const { queries, urlFor } = await render({
ids: ['test'],
textResources: [
{
id: 'test',
value: 'Hello {0}',
variables: [
{
dataSource: 'dataModel.someModel',
key: 'name',
},
],
it.each<string>(['someModel', 'someModel1.0'])(
'simple, should render a resource with a variable lookup - %s',
async (modelName: string) => {
const { queries, urlFor } = await render({
ids: ['test'],
textResources: [
{
id: 'test',
value: 'Hello {0}',
variables: [
{
dataSource: `dataModel.${modelName}`,
key: 'name',
},
],
},
],
dataModels: {
[modelName]: {
name: 'World',
},
},
],
dataModels: {
someModel: {
name: 'World',
},
},
defaultDataModel: 'someModel',
});
defaultDataModel: modelName,
});

await waitFor(() => expect(screen.getByTestId('test')).toHaveTextContent('Hello World'));
await waitFor(() => expect(screen.getByTestId('test')).toHaveTextContent('Hello World'));

expect(queries.fetchFormData).toHaveBeenCalledTimes(1);
expect(queries.fetchFormData).toHaveBeenCalledWith(urlFor('someModel'), {});
expect(queries.fetchFormData).toHaveBeenCalledTimes(1);
expect(queries.fetchFormData).toHaveBeenCalledWith(urlFor(modelName), {});

expect(window.logError).not.toHaveBeenCalled();
expect(window.logErrorOnce).not.toHaveBeenCalled();
});
expect(window.logError).not.toHaveBeenCalled();
expect(window.logErrorOnce).not.toHaveBeenCalled();
},
);

it('advanced, should fetch data from multiple models, handle failures', async () => {
jest.useFakeTimers();
Expand Down
17 changes: 16 additions & 1 deletion src/features/language/useLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,21 @@ function getTextResourceByKey(
return getTextResourceByKey(value, textResources, dataSources);
}

function splitNTimes(text: string, sep: string, n: number) {
if (n === 0) {
return [text];
} else if (n < 0) {
throw new Error(`Invalid N:${n}`);
}
const parts = text.split(sep);
const out: string[] = [];
for (let i = 0; i < n; i++) {
out.push(parts.shift() ?? '');
}
out.push(parts.join(sep));
return out;
}

function replaceVariables(text: string, variables: IVariable[], dataSources: TextResourceVariablesDataSources) {
const {
node,
Expand All @@ -345,7 +360,7 @@ function replaceVariables(text: string, variables: IVariable[], dataSources: Tex
let value = variables[idx].key;

if (variable.dataSource.startsWith('dataModel')) {
const dataModelName = variable.dataSource.split('.')[1];
const dataModelName = splitNTimes(variable.dataSource, '.', 1)[1];
const cleanPath = getKeyWithoutIndexIndicators(value);

const dataTypeToRead =
Expand Down

0 comments on commit f936ff6

Please sign in to comment.