Skip to content

Commit

Permalink
refactor: create LWC handler to segregate isProcessable algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Apr 20, 2023
1 parent 9d07cd1 commit 442f1a0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 33 deletions.
22 changes: 0 additions & 22 deletions __tests__/unit/lib/service/inResourceHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,26 +219,4 @@ describe('InResourceHandler', () => {
})
})
})

describe('when the line should not be processed', () => {
it.each([`${basePath}/.eslintrc.json`])(
'does not handle the line',
async entityPath => {
// Arrange
const sut = new InResourceHandler(
`A ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(copyFiles).not.toHaveBeenCalled()
}
)
})
})
96 changes: 96 additions & 0 deletions __tests__/unit/lib/service/lwcHandler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
'use strict'
const LwcHandler = require('../../../../src/service/lwcHandler')
const { copyFiles, readDir } = require('../../../../src/utils/fsHelper')
const {
ADDITION,
DELETION,
MODIFICATION,
} = require('../../../../src/utils/gitConstants')

jest.mock('../../../../src/utils/fsHelper')

readDir.mockImplementationOnce(() => Promise.resolve([]))

const objectType = 'lwc'
const element = 'component'
const basePath = `force-app/main/default/${objectType}`
const entityPath = `${basePath}/${element}/${element}.js`
const xmlName = 'LightningComponentBundle'
let work
beforeEach(() => {
jest.clearAllMocks()
work = {
config: { output: '', repo: '', generateDelta: true },
diffs: { package: new Map(), destructiveChanges: new Map() },
}
})

describe('lwcHandler', () => {
let globalMetadata
beforeAll(async () => {
// eslint-disable-next-line no-undef
globalMetadata = await getGlobalMetadata()
})
describe('when the line should not be processed', () => {
it.each([`${basePath}/.eslintrc.json`, `${basePath}/jsconfig.json`])(
'does not handle the line',
async entityPath => {
// Arrange
const sut = new LwcHandler(
`${ADDITION} ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.size).toBe(0)
expect(copyFiles).not.toHaveBeenCalled()
}
)
})

describe('when the line should be processed', () => {
it.each([ADDITION, MODIFICATION])(
'handles the line for "%s" type change',
async changeType => {
// Arrange
const sut = new LwcHandler(
`${changeType} ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.package.get(xmlName)).toEqual(new Set([element]))
expect(copyFiles).toHaveBeenCalled()
}
)

it('handles the line for "D" type change', async () => {
// Arrange
const sut = new LwcHandler(
`${DELETION} ${entityPath}`,
objectType,
work,
globalMetadata
)

// Act
await sut.handle()

// Assert
expect(work.diffs.destructiveChanges.get(xmlName)).toEqual(
new Set([element])
)
expect(copyFiles).not.toHaveBeenCalled()
})
})
})
10 changes: 1 addition & 9 deletions src/service/inResourceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const StandardHandler = require('./standardHandler')
const { join, parse } = require('path')
const { pathExists, readDir } = require('../utils/fsHelper')
const { META_REGEX } = require('../utils/metadataConstants')
const { sep } = require('path')
const { cleanUpPackageMember } = require('../utils/packageHelper')

const STATICRESOURCE_TYPE = 'staticresources'
Expand Down Expand Up @@ -77,14 +76,7 @@ class ResourceHandler extends StandardHandler {
}

_isProcessable() {
const parsedLine = parse(this.line)
const parentFolder = parsedLine.dir.split(sep).pop()

return (
super._isProcessable() ||
parentFolder !== this.type ||
!parsedLine.name.startsWith('.')
)
return true
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/service/lwcHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'
const InResourceHandler = require('./inResourceHandler')
const { parse, sep } = require('path')

class LwcHandler extends InResourceHandler {
_isProcessable() {
const parentFolder = parse(this.line).dir.split(sep).pop()

return parentFolder !== this.type
}
}

module.exports = LwcHandler
5 changes: 3 additions & 2 deletions src/service/typeHandlerFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const InBundleHandler = require('./inBundleHandler')
const InFile = require('./inFileHandler')
const InFolder = require('./inFolderHandler')
const InResource = require('./inResourceHandler')
const LwcHandler = require('./lwcHandler')
const Standard = require('./standardHandler')
const SubCustomObject = require('./subCustomObjectHandler')
const ObjectTranslation = require('./ObjectTranslationHandler')
Expand All @@ -15,7 +16,7 @@ const { getType } = require('../utils/typeUtils')
const classes = {
assignmentRules: InFile,
autoResponseRules: InFile,
aura: InResource,
aura: LwcHandler,
bots: Bot,
businessProcesses: SubCustomObject,
compactLayouts: SubCustomObject,
Expand All @@ -32,7 +33,7 @@ const classes = {
indexes: SubCustomObject,
labels: InFile,
listViews: SubCustomObject,
lwc: InResource,
lwc: LwcHandler,
matchingRules: InFile,
objects: CustomObject,
objectTranslations: ObjectTranslation,
Expand Down

0 comments on commit 442f1a0

Please sign in to comment.