Skip to content

Commit

Permalink
test: add packageGenerator test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Aug 5, 2022
1 parent 2b7847d commit 3f8a751
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
165 changes: 165 additions & 0 deletions __tests__/unit/lib/post-processor/packageGenerator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
'use strict'
const PackageGenerator = require('../../../../src/post-processor/packageGenerator')
const fse = require('fs-extra')
jest.mock('fs-extra')

const mockBuildPackage = jest.fn()
jest.mock('../../../../src/utils/packageHelper', () => {
return jest.fn().mockImplementation(() => {
return { buildPackage: mockBuildPackage }
})
})

describe('PackageGenerator', () => {
describe('cleanPackages', () => {
describe('when destructive contains element from additive', () => {
it('removes same element from destructive', () => {
// Arrange
const type = 'type'
const element = 'element'
const additive = new Map([[type, new Set([element])]])
const destructive = new Map([[type, new Set([element, 'other'])]])
const sut = new PackageGenerator(
{
diffs: { package: additive, destructiveChanges: destructive },
},
{ output: 'test' }
)

// Act
sut.cleanPackages()

// Assert
expect(additive.get(type).has(element)).toEqual(true)
expect(destructive.get(type).has(element)).toEqual(false)
})

describe('when destructive does not have element anymore', () => {
it('removes same element from destructive', () => {
// Arrange
const type = 'type'
const element = 'element'
const additive = new Map([[type, new Set([element])]])
const destructive = new Map([[type, new Set([element])]])
const sut = new PackageGenerator(
{
diffs: { package: additive, destructiveChanges: destructive },
},
{ output: 'test' }
)

// Act
sut.cleanPackages()

// Assert
expect(additive.get(type).has(element)).toEqual(true)
expect(destructive.has(type)).toEqual(false)
})
})
})

describe('when destructive does not contain element from additive', () => {
it('keeps both elements', () => {
// Arrange
const type = 'type'
const element = 'element'
const additive = new Map([[type, new Set([element])]])
const destructive = new Map([[type, new Set(['otherElement'])]])
const sut = new PackageGenerator(
{
diffs: { package: additive, destructiveChanges: destructive },
},
{ output: 'test' }
)

// Act
sut.cleanPackages()

// Assert
expect(additive.get(type).has(element)).toEqual(true)
expect(destructive.get(type).has('otherElement')).toEqual(true)
})
})
})

describe('buildPackages', () => {
let sut
beforeEach(() => {
// Arrange
sut = new PackageGenerator(
{
diffs: { package: new Map(), destructiveChanges: new Map() },
},
{ output: 'test' }
)
})
it('calls `fse.outputFile` for %s', async () => {
// Act
await sut.buildPackages()

// Assert
expect(fse.outputFile).toHaveBeenCalledTimes(3)
})

it('calls `PackageBuilder.buildPackage` for %s', async () => {
// Act
await sut.buildPackages()

// Assert
expect(mockBuildPackage).toHaveBeenCalledTimes(3)
})
})

describe('process', () => {
describe.each([
[
'different map',
new Map([['a', new Set(['a'])]]),
new Map([['d', new Set(['a'])]]),
new Map([['d', new Set(['a'])]]),
],
[
'same map',
new Map([['a', new Set(['a'])]]),
new Map([['a', new Set(['a'])]]),
new Map(),
],

[
'overlaping map',
new Map([['a', new Set(['a'])]]),
new Map([['a', new Set(['a', 'b'])]]),
new Map([['a', new Set(['b'])]]),
],
])(
'when executed with %s',
(_, additive, destructive, expectedDestructive) => {
let sut
beforeEach(async () => {
// Arrange
sut = new PackageGenerator(
{
diffs: { package: additive, destructiveChanges: destructive },
},
{ output: 'test' }
)

await sut.process()
})
it('cleans up the maps', () => {
// Assert
expect(destructive).toEqual(expectedDestructive)
})
it('calls `fse.outputFile` for %s', () => {
// Assert
expect(fse.outputFile).toHaveBeenCalledTimes(3)
})

it('calls `PackageBuilder.buildPackage` for %s', () => {
// Assert
expect(mockBuildPackage).toHaveBeenCalledTimes(3)
})
}
)
})
})
3 changes: 3 additions & 0 deletions src/post-processor/packageGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class PackageGenerator extends BaseProcessor {
[...destructive.get(type)].filter(element => !members.has(element))
)
)
if (destructive.get(type).size === 0) {
destructive.delete(type)
}
}
}
}
Expand Down

0 comments on commit 3f8a751

Please sign in to comment.