Skip to content

Commit

Permalink
Adapt replacements to modify json schema tests (apideck-libraries#343)
Browse files Browse the repository at this point in the history
* Adapt replacements to modify json schema tests

* Ignore prettier and eslint warnings. Test data has to contain the escaped chars.

* Improve readability of tests
  • Loading branch information
miriamgreis authored Jul 15, 2022
1 parent a7930ce commit fb4bbe6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/application/globals/writeRawReplacements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,41 @@ describe('writeRawReplacements', () => {
})
})
})

it('should use config with quotes to remove all found values in escaped string', () => {
const config = [
{
searchFor: '"format":"date",',
replaceWith: ''
}
]

const obj = {
exec: ['... "format":"date", ...']
}
const string = JSON.stringify(obj, null, 2)
const result = writeRawReplacements(string, config)
const resultObj = JSON.parse(result)
expect(resultObj).toStrictEqual({
exec: ['... ...']
})
})

it('should use config with quotes to replace all found values in escaped string', () => {
const config = [
{
searchFor: '"format":"date"',
replaceWith: '"format":"specialDate"'
}
]

const obj = {
exec: ['... "format":"date", ...']
}
const string = JSON.stringify(obj, null, 2)
const result = writeRawReplacements(string, config)
const resultObj = JSON.parse(result)
expect(resultObj).toStrictEqual({
exec: ['... "format":"specialDate", ...']
})
})
2 changes: 1 addition & 1 deletion src/application/globals/writeRawReplacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const writeRawReplacements = (
globalReplacements: GlobalReplacement[]
): string => {
globalReplacements.map(({ searchFor, replaceWith }) => {
const pattern = searchFor.replace(/"/g, '\\\\"')
const pattern = searchFor.replace(/"/g, '\\"')
const replacement = replaceWith.replace(/"/g, '\\"')
collectionAsString = collectionAsString.replace(
new RegExp(escapeRegExp(pattern), 'g'),
Expand Down

0 comments on commit fb4bbe6

Please sign in to comment.