Skip to content

Commit

Permalink
[ASL-4413] HBA upload functionality in PPL transfer journeys (#327)
Browse files Browse the repository at this point in the history
* - Provide a flexible mechanism for key-based lookup with fallback options in support of content updates related to ASL-4413
- Include tests to validate that the snippet component can correctly handle fallbacks when attempting to retrieve templates

* Update index.jsx - undo commit (throw error instead of return)
  • Loading branch information
edemirbag committed May 15, 2024
1 parent e9472e2 commit 6f50246
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ukhomeoffice/asl-components",
"version": "13.4.1",
"version": "13.5.0",
"description": "React components for ASL layouts and elements",
"main": "src/index.jsx",
"styles": "styles/index.scss",
Expand Down
16 changes: 15 additions & 1 deletion src/snippet/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ import { connect } from 'react-redux';
import Markdown from '../markdown';
import { render } from 'mustache';

function getTemplate(content, primary, fallback) {
const keysToTry = [primary, ...(Array.isArray(fallback) ? fallback : [fallback])];

for (let key of keysToTry) {
const template = get(content, key);
if (template != undefined) {
return template;
}
}

return undefined;
}

export const Snippet = ({ content, children, optional, fallback, ...props }) => {
const str = get(content, children, get(content, fallback));
const str = getTemplate(content, children, fallback);
if (str === undefined && optional) {
return null;

}
if (str === undefined) {
throw new Error(`Failed to lookup content snippet: ${children}`);
Expand Down
12 changes: 12 additions & 0 deletions src/snippet/index.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ two`
expect(wrapper.html()).toEqual(paragraphs);
});

test('can accept single fallback', () => {
const wrapper = render(<div><Snippet content={content} fallback={'paragraphs'}>non.existent</Snippet></div>);
expect(wrapper.find('p').length).toEqual(2);
expect(wrapper.html()).toEqual(paragraphs);
});

test('can accept multiple fallbacks', () => {
const wrapper = render(<div><Snippet content={content} fallback={['non.existent.2', 'paragraphs', 'list']}>non.existent</Snippet></div>);
expect(wrapper.find('p').length).toEqual(2);
expect(wrapper.html()).toEqual(paragraphs);
});

});

0 comments on commit 6f50246

Please sign in to comment.