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

[ASL-4413] HBA upload functionality in PPL transfer journeys #327

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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
18 changes: 16 additions & 2 deletions src/snippet/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ 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);
edemirbag marked this conversation as resolved.
Show resolved Hide resolved
if (str === undefined && optional) {
return null;

}
if (str === undefined) {
throw new Error(`Failed to lookup content snippet: ${children}`);
edemirbag marked this conversation as resolved.
Show resolved Hide resolved
return `Failed to lookup content snippet: ${children}`;
}
if (typeof str !== 'string') {
throw new Error(`Invalid content snippet for key ${children}: ${JSON.stringify(str)}`);
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);
});

edemirbag marked this conversation as resolved.
Show resolved Hide resolved
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);
});

});