fix(client): cert project help post sans editable region (#49157)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Shaun Hamilton
2023-02-02 11:46:13 -06:00
committed by GitHub
co-authored by Oliver Eyton-Williams
parent 033707d18c
commit 9771e0ad70
2 changed files with 89 additions and 22 deletions
@@ -38,6 +38,31 @@ function filesToMarkdown(challengeFiles = {}) {
}, '\n');
}
export function insertEditableRegions(challengeFiles = []) {
if (challengeFiles?.some(file => file.editableRegionBoundaries?.length > 0)) {
const editableRegionStrings = fileExtension => {
const startComment = fileExtension === 'html' ? '<!--' : '/*';
const endComment = fileExtension === 'html' ? '-->' : '*/';
return `\n${startComment} User Editable Region ${endComment}\n`;
};
const filesWithEditableRegions = challengeFiles.map(file => {
const { contents, editableRegionBoundaries, ext } = file;
if (editableRegionBoundaries.length > 0) {
const comment = editableRegionStrings(ext);
const [start, end] = editableRegionBoundaries;
const lines = contents.split('\n');
lines.splice(start, 0, comment);
lines.splice(end, 0, comment);
return { ...file, contents: lines.join('\n') };
}
return file;
});
return filesWithEditableRegions;
}
return challengeFiles;
}
function createQuestionEpic(action$, state$, { window }) {
return action$.pipe(
ofType(actionTypes.createQuestion),
@@ -50,29 +75,9 @@ function createQuestionEpic(action$, state$, { window }) {
block,
helpCategory
} = challengeMetaSelector(state);
if (
challengeFiles?.some(file => file.editableRegionBoundaries.length > 0)
) {
const editableRegionStrings = fileExtension => {
const startComment = fileExtension === 'html' ? '<!--' : '/*';
const endComment = fileExtension === 'html' ? '-->' : '*/';
return `\n${startComment} User Editable Region ${endComment}\n`;
};
const filesWithEditableRegions = challengeFiles.map(file => {
const { contents, editableRegionBoundaries, ext } = file;
if (editableRegionBoundaries.length > 0) {
const comment = editableRegionStrings(ext);
const [start, end] = editableRegionBoundaries;
const lines = contents.split('\n');
lines.splice(start, 0, comment);
lines.splice(end, 0, comment);
return { ...file, contents: lines.join('\n') };
}
return file;
});
challengeFiles = filesWithEditableRegions;
}
challengeFiles = insertEditableRegions(challengeFiles);
const {
navigator: { userAgent },
location: { pathname, origin }
@@ -1,4 +1,5 @@
import { transformEditorLink } from '../utils';
import { insertEditableRegions } from './create-question-epic';
describe('create-question-epic', () => {
describe('transformEditorLink', () => {
@@ -27,4 +28,65 @@ describe('create-question-epic', () => {
});
});
});
describe('insertEditableRegions', () => {
const multiCertChallengeFiles = [
{
contents: '<h1>Hello World</h1>',
ext: 'html',
fileKey: 'indexhtml',
history: ['index.html'],
head: '',
id: '',
name: 'index',
path: 'index.html',
seed: '',
tail: ''
},
{
contents: '',
ext: 'css',
fileKey: 'stylescss',
history: ['styles.css'],
head: '',
id: '',
name: 'styles',
path: 'styles.css',
seed: '',
tail: ''
}
];
const multiPracticeChallengeFiles = [
{
contents:
'<html>\n <body>\n <h1>Hello World</h1>\n </body>\n</html>',
editableRegionBoundaries: [2, 4],
ext: 'html',
fileKey: 'indexhtml',
history: ['index.html'],
head: '',
id: '',
name: 'index',
path: 'index.html',
seed: '<html>\n <body>\n <h1>Hello World</h1>\n </body>\n</html>',
tail: ''
}
];
it('should not insert editable regions for certification projects', () => {
const challengeFiles = insertEditableRegions(multiCertChallengeFiles);
challengeFiles.forEach(({ contents }) => {
expect(contents).not.toContain('User Editable Region');
});
});
it('should insert editable regions for multifile practice projects', () => {
const challengeFiles = insertEditableRegions(multiPracticeChallengeFiles);
challengeFiles.forEach(({ contents }) => {
expect(contents).toContain('User Editable Region');
});
});
it('should not throw if editableRegionBoundaries is undefined', () => {
expect(() =>
insertEditableRegions(multiCertChallengeFiles)
).not.toThrow();
});
});
});