Skip to content

Commit

Permalink
fix: handling xml parsing not returning array for one element (#343)
Browse files Browse the repository at this point in the history
* fix: fxp not returning array for one element

* test: add test for one element returned by fxp

* refactor: use asArray for inFile handler xml parsing

* test: unit test asArray new method

* build: upgrade dependencies
  • Loading branch information
scolladon authored Aug 26, 2022
1 parent 2ca036b commit c5abb9a
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 359 deletions.
69 changes: 23 additions & 46 deletions __tests__/unit/lib/post-processor/flowTranslationProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ const {
FLOW_DIRECTORY_NAME,
TRANSLATION_TYPE,
} = require('../../../../src/utils/metadataConstants')
const {
copyFiles,
scanExtension,
readFile,
} = require('../../../../src/utils/fsHelper')

const { copyFiles, scanExtension } = require('../../../../src/utils/fsHelper')
jest.mock('fs-extra')
jest.mock('fast-xml-parser')
jest.mock('../../../../src/utils/fsHelper', () => ({
Expand All @@ -22,43 +17,6 @@ jest.mock('../../../../src/utils/fsHelper', () => ({
const FR = 'fr'
const EN = 'en'
const flowFullName = 'test-flow'
const translationWithFlow = `
<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http://soap.sforce.com/2006/04/metadata">
<flowDefinitions>
<flows>
<screens>
<fields>
<fieldText>This is a the text</fieldText>
<name>Translated_Text</name>
</fields>
<name>Screen_To_Translate</name>
</screens>
</flows>
<fullName>${flowFullName}</fullName>
</flowDefinitions>
<flowDefinitions>
<flows>
<screens>
<fields>
<fieldText>This is a the text</fieldText>
<name>Translated_Text</name>
</fields>
<name>Screen_To_Translate</name>
</screens>
</flows>
<fullName>anotherFlow</fullName>
</flowDefinitions>
</Translations>`

const translationWithoutFlow = `
<?xml version="1.0" encoding="UTF-8"?>
<Translations xmlns="http://soap.sforce.com/2006/04/metadata">
<customLabels>
<name>myTickets_Attachments_Delete_Modal_Header_Label</name>
<label>Supprimer le fichier ?</label>
</customLabels>
</Translations>`

const trueAfter = (attempt = 0) => {
let count = 0
Expand Down Expand Up @@ -109,7 +67,6 @@ describe('FlowTranslationProcessor', () => {
describe('when there is a translation file without flow def', () => {
beforeEach(() => {
// Arrange
readFile.mockImplementationOnce(() => translationWithoutFlow)
mockParse.mockImplementationOnce(() => ({}))
})
it('should not add translation file', async () => {
Expand All @@ -124,6 +81,28 @@ describe('FlowTranslationProcessor', () => {
})
})

describe('when there is a translation file with one flow def', () => {
beforeEach(() => {
// Arrange
work.diffs.package = new Map([
[FLOW_DIRECTORY_NAME, new Set([flowFullName])],
])
mockParse.mockImplementation(() => ({
Translations: { flowDefinitions: { fullName: flowFullName } },
}))
})
it('should add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.has(TRANSLATION_TYPE)).toBeTruthy()
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(1)
expect(copyFiles).toHaveBeenCalled()
})
})

describe('when there is already a translation related to a flow', () => {
beforeEach(() => {
// Arrange
Expand All @@ -143,8 +122,6 @@ describe('FlowTranslationProcessor', () => {
describe('when there is multiple translation file with multiple flow def', () => {
beforeEach(() => {
// Arrange
readFile.mockImplementationOnce(() => translationWithFlow)
readFile.mockImplementationOnce(() => translationWithFlow)
flap = trueAfter(2)
let count = 0
const getTranslationName = () =>
Expand Down
41 changes: 41 additions & 0 deletions __tests__/unit/lib/utils/fxpHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'
const { asArray } = require('../../../../src/utils/fxpHelper')

describe('asArray', () => {
describe('when called with null', () => {
// Arrange
const expected = null

it('returns empty array', () => {
// Act
const actual = asArray(expected)

// Assert
expect(actual).toEqual([])
})
})
describe('when called with array', () => {
// Arrange
const expected = [{ test: true }]

it('returns the same array', () => {
// Act
const actual = asArray(expected)

// Assert
expect(actual).toBe(expected)
})
})
describe('when called with object', () => {
// Arrange
const expected = { test: true }

it('returns the array with this object', () => {
// Act
const actual = asArray(expected)

// Assert
expect(actual).toEqual([expected])
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"eslint-plugin-jsdoc": "^39.3.6",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.1",
"jest": "^29.0.0",
"jest": "^29.0.1",
"lint-staged": "^13.0.3",
"nyc": "^15.1.0",
"prettier": "^2.7.1",
Expand Down
30 changes: 14 additions & 16 deletions src/post-processor/flowTranslationProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ const {
TRANSLATION_TYPE,
} = require('../utils/metadataConstants')
const { copyFiles, scanExtension, readFile } = require('../utils/fsHelper')
const { XML_PARSER_OPTION } = require('../utils/fxpConfig')
const { parse, resolve } = require('path')
const { XMLParser } = require('fast-xml-parser')
const { asArray, XML_PARSER_OPTION } = require('../utils/fxpHelper')
const { fillPackageWithParameter } = require('../utils/packageHelper')

const getTranslationName = translationPath =>
parse(translationPath.replace(META_REGEX, '')).name

class FlowTranslationProcessor extends BaseProcessor {
flowPerTranslations
translationPaths

constructor(work, metadata) {
super(work, metadata)
this.flowPerTranslations = new Map()
this.translationPaths = new Set()
}

async process() {
Expand All @@ -30,7 +30,7 @@ class FlowTranslationProcessor extends BaseProcessor {
}

async _buildFlowDefinitionsMap() {
this.flowPerTranslations.clear()
this.translationPaths.clear()

const translationsIterator = scanExtension(
this.config.repo,
Expand All @@ -47,21 +47,22 @@ class FlowTranslationProcessor extends BaseProcessor {
const xmlParser = new XMLParser(XML_PARSER_OPTION)
const translationJSON = xmlParser.parse(translationXML)
// implement other kind of metadata here
translationJSON?.Translations?.flowDefinitions?.forEach(
flowDefinition =>
this._addFlowPerTranslation({
translationPath,
fullName: flowDefinition.fullName,
})
const flowDefinitions = asArray(
translationJSON?.Translations?.flowDefinitions
)
flowDefinitions.forEach(flowDefinition =>
this._addFlowPerTranslation({
translationPath,
fullName: flowDefinition.fullName,
})
)
}
}
}

async _handleFlowTranslation() {
const copyTranslationsPromises = []

for (const translationPath of this.flowPerTranslations.keys()) {
for (const translationPath of this.translationPaths.keys()) {
fillPackageWithParameter({
package: this.work.diffs.package,
type: TRANSLATION_TYPE,
Expand All @@ -79,10 +80,7 @@ class FlowTranslationProcessor extends BaseProcessor {
_addFlowPerTranslation({ translationPath, fullName }) {
const packagedElements = this.work.diffs.package.get(FLOW_DIRECTORY_NAME)
if (packagedElements?.has(fullName)) {
if (!this.flowPerTranslations.has(translationPath)) {
this.flowPerTranslations.set(translationPath, new Set())
}
this.flowPerTranslations.get(translationPath).add(fullName)
this.translationPaths.add(translationPath)
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/service/inFileHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const StandardHandler = require('./standardHandler')
const { outputFile } = require('fs-extra')
const { basename, join } = require('path')
const { XMLBuilder, XMLParser } = require('fast-xml-parser')
const { XML_PARSER_OPTION, JSON_PARSER_OPTION } = require('../utils/fxpConfig')
const { asArray } = require('../utils/fxpHelper')
const { XML_PARSER_OPTION, JSON_PARSER_OPTION } = require('../utils/fxpHelper')

class InFileHandler extends StandardHandler {
static xmlObjectToPackageType
Expand Down Expand Up @@ -59,9 +60,7 @@ class InFileHandler extends StandardHandler {
const metadataContent = Object.values(result.fileContent)[0]

result.authorizedKeys.forEach(subType => {
const meta = Array.isArray(metadataContent[subType])
? metadataContent[subType]
: [metadataContent[subType]]
const meta = asArray(metadataContent[subType])
metadataContent[subType] = meta.filter(elem =>
toAdd
.get(InFileHandler.xmlObjectToPackageType.get(subType).directoryName)
Expand Down
5 changes: 5 additions & 0 deletions src/utils/fxpConfig.js → src/utils/fxpHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ const JSON_PARSER_OPTION = {
indentBy: ' ',
}

const asArray = node => {
return node != null ? (Array.isArray(node) ? node : [node]) : []
}

module.exports.asArray = asArray
module.exports.XML_PARSER_OPTION = XML_PARSER_OPTION
module.exports.JSON_PARSER_OPTION = JSON_PARSER_OPTION
Loading

0 comments on commit c5abb9a

Please sign in to comment.