Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split by '.' only once when parsing 'dataSource' for a text resource variable #2802

Merged
Merged
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
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
Loading