Skip to content

Commit

Permalink
migration added
Browse files Browse the repository at this point in the history
  • Loading branch information
vitPinchuk committed Dec 12, 2024
1 parent 06877f7 commit d629042
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
93 changes: 93 additions & 0 deletions src/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,97 @@ describe('Migration', () => {
});
});
});

describe('5.6.0', () => {
describe('Execution code', () => {
it.each([
{
name: 'content',
initial: `<br>\\n<p class=\"panel-title\">Test</p>\\n<span>next line</span>;`,
expected: `<br>\n<p class=\"panel-title\">Test</p>\n<span>next line</span>;`,
},
{
name: 'afterRender',
initial: `console.log('after render')\\nconsole.log('after render')\\nconsole.log('after render')`,
expected: `console.log('after render')\nconsole.log('after render')\nconsole.log('after render')`,
},
{
name: 'helpers',
initial: `console.log('before render')\\nconsole.log('before render')\\nconsole.log('before render')`,
expected: `console.log('before render')\nconsole.log('before render')\nconsole.log('before render')`,
},
{
name: 'defaultContent',
initial: `<br>\\n<p class=\"panel-title\">Test default content</p>\\n<span>next line</span>;`,
expected: `<br>\n<p class=\"panel-title\">Test default content</p>\n<span>next line</span>;`,
},
{
name: 'styles',
initial: `.test{ \\n color:red;\\n}`,
expected: `.test{ \n color:red;\n}`,
},
])('Should migrate $name', ({ initial, expected }) => {
expect(
getMigratedOptions({
pluginVersion: '5.5.0',
options: {
helpers: initial,
afterRender: initial,
content: initial,
defaultContent: initial,
styles: initial,
},
} as any).helpers
).toEqual(expected);
expect(
getMigratedOptions({
pluginVersion: '5.5.0',
options: {
helpers: initial,
afterRender: initial,
content: initial,
defaultContent: initial,
styles: initial,
},
} as any).afterRender
).toEqual(expected);
expect(
getMigratedOptions({
pluginVersion: '5.5.0',
options: {
helpers: initial,
afterRender: initial,
content: initial,
defaultContent: initial,
styles: initial,
},
} as any).content
).toEqual(expected);
expect(
getMigratedOptions({
pluginVersion: '5.5.0',
options: {
helpers: initial,
afterRender: initial,
content: initial,
defaultContent: initial,
styles: initial,
},
} as any).defaultContent
).toEqual(expected);
expect(
getMigratedOptions({
pluginVersion: '5.5.0',
options: {
helpers: initial,
afterRender: initial,
content: initial,
defaultContent: initial,
styles: initial,
},
} as any).styles
).toEqual(expected);
});
});
});
});
22 changes: 21 additions & 1 deletion src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,34 @@ const normalizeHelpersOption = (code: string): string => {
*/
export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions & PanelOptions>): PanelOptions => {
const { everyRow, ...actualOptions } = panel.options;

/**
* Normalize non context code parameters before 5.0.0
*/
if (panel.pluginVersion && semver.lt(panel.pluginVersion, '5.0.0')) {
actualOptions.helpers = normalizeHelpersOption(actualOptions.helpers);
}

/**
* Normalize \\n characters to \n for versions lt 5.6.0
*/
if (panel.pluginVersion && semver.lt(panel.pluginVersion, '5.6.0')) {
if (actualOptions.helpers) {
actualOptions.helpers = actualOptions.helpers.replaceAll('\\n', '\n');
}
if (actualOptions.afterRender) {
actualOptions.afterRender = actualOptions.afterRender.replaceAll('\\n', '\n');
}
if (actualOptions.defaultContent) {
actualOptions.defaultContent = actualOptions.defaultContent.replaceAll('\\n', '\n');
}
if (actualOptions.content) {
actualOptions.content = actualOptions.content.replaceAll('\\n', '\n');
}
if (actualOptions.styles) {
actualOptions.styles = actualOptions.styles.replaceAll('\\n', '\n');
}
}

/**
* Normalize every row
*/
Expand Down

0 comments on commit d629042

Please sign in to comment.