Skip to content

Commit

Permalink
test: add flowTranslationProcessor test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Aug 6, 2022
1 parent 00b66d5 commit dbf0410
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 1 deletion.
4 changes: 3 additions & 1 deletion __mocks__/fast-xml-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ fxp.__setMockContent = contents => {
}
}

const mockParse = jest.fn(content => JSON.parse(xml2json.get(content)))
fxp.XMLParser = function () {
return {
parse: jest.fn(content => JSON.parse(xml2json.get(content))),
parse: mockParse,
}
}

Expand All @@ -25,3 +26,4 @@ fxp.XMLBuilder = function () {
}

module.exports = fxp
module.exports.mockParse = mockParse
190 changes: 190 additions & 0 deletions __tests__/unit/lib/post-processor/flowTranslationProcessor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
'use strict'
const FlowTranslationProcessor = require('../../../../src/post-processor/flowTranslationProcessor')
const fs = require('fs')
const { mockParse } = require('fast-xml-parser')
const {
FLOW_DIRECTORY_NAME,
} = require('../../../../src/utils/metadataConstants')
const { copyFiles, scanExtension } = require('../../../../src/utils/fsHelper')
const {
fillPackageWithParameter,
} = require('../../../../src/utils/packageHelper')

jest.mock('fs-extra')
jest.mock('fs')
jest.mock('fast-xml-parser')
jest.mock('../../../../src/utils/packageHelper')
jest.mock('../../../../src/utils/fsHelper', () => ({
scanExtension: jest.fn(),
copyFiles: jest.fn(),
}))

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>
</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>`

describe('FlowTranslationProcessor', () => {
const work = {
diffs: {
package: new Map(),
destructiveChanges: new Map(),
},
config: { repo: 'repo', output: 'output', generateDelta: true },
}
describe('process', () => {
let sut
beforeEach(() => {
sut = new FlowTranslationProcessor(work)
})
describe('when there is no translation file', () => {
beforeEach(() => {
// Arrange
scanExtension.mockImplementationOnce(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
done: true,
}),
}),
}))
})
it('should not add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).not.toHaveBeenCalled()
expect(fillPackageWithParameter).not.toHaveBeenCalled()
expect(copyFiles).not.toHaveBeenCalled()
})
})

describe('when there is a translation file without flow def', () => {
beforeEach(() => {
// Arrange
let toggle = true
let flap = () => {
toggle = !toggle
return toggle
}
scanExtension.mockImplementationOnce(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
value: 'fr.translation-meta.xml',
done: flap(),
}),
}),
}))
fs.promises.readFile.mockImplementationOnce(
() => translationWithoutFlow
)
mockParse.mockImplementationOnce(() => ({}))
})
it('should not add translation file', async () => {
// Act
await sut.process()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(1)
expect(fillPackageWithParameter).not.toHaveBeenCalled()
expect(copyFiles).not.toHaveBeenCalled()
})
})

describe('when there is a translation file with flow def', () => {
beforeEach(() => {
// Arrange
let toggle = true
let flap = () => {
toggle = !toggle
return toggle
}
scanExtension.mockImplementationOnce(() => ({
[Symbol.asyncIterator]: () => ({
next: () => ({
value: 'fr.translation-meta.xml',
done: flap(),
}),
}),
}))
fs.promises.readFile.mockImplementationOnce(() => translationWithFlow)
})
describe('when there is no flow matching the translation', () => {
beforeEach(() => {
mockParse.mockImplementationOnce(() => ({
Translations: { flowDefinitions: [{ fullName: 'wrong' }] },
}))
})
it('should not add translation', async () => {
// Arrange

// Act
await sut.process()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(1)
expect(fillPackageWithParameter).not.toHaveBeenCalled()
expect(copyFiles).not.toHaveBeenCalled()
})
})

describe('when there is flow matching the translation', () => {
describe.each([true, false])(
'when config.generateDelta is %s',
generateDelta => {
beforeEach(() => {
mockParse.mockImplementationOnce(() => ({
Translations: { flowDefinitions: [{ fullName: flowFullName }] },
}))
work.diffs.package = new Map([
[FLOW_DIRECTORY_NAME, new Set([flowFullName])],
])
work.config.generateDelta = generateDelta
})
it('should add translation', async () => {
// Arrange

// Act
await sut.process()

// Assert
expect(work.diffs.package.size).toBe(1)
expect(scanExtension).toHaveBeenCalledTimes(1)
expect(mockParse).toHaveBeenCalledTimes(1)
expect(fillPackageWithParameter).toHaveBeenCalledTimes(1)
expect(copyFiles).toHaveBeenCalledTimes(generateDelta ? 1 : 0)
})
}
)
})
})
})
})

0 comments on commit dbf0410

Please sign in to comment.