Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 10, 2022
1 parent 298f906 commit 0e24863
Showing 1 changed file with 177 additions and 3 deletions.
180 changes: 177 additions & 3 deletions download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe('download', () => {
spinnerMock.start.mockReset()
spinnerMock.success.mockReset()
spinnerMock.error.mockReset()
spinnerMock.update.mockReset()

extractMock.mockReset()

Expand Down Expand Up @@ -368,6 +369,8 @@ describe('download', () => {

expect(unlinkMock).toHaveBeenCalledTimes(1)
expect(unlinkMock).toHaveBeenCalledWith('chrome-filenameOS-x64-11.12.13.14.zip', expect.any(Function))

expect(logger.error).toHaveBeenCalledTimes(0)
})

it('should update the progress', async () => {
Expand All @@ -383,6 +386,60 @@ describe('download', () => {

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

// Act
const config = createChromeFullConfig({
autoUnzip: false,
})
await downloadChromium(config)

expect(onMock).toHaveBeenCalledTimes(3)
expect(onMock).toHaveBeenCalledWith('finish', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('end', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('error', expect.any(Function))

const progressCallback = progressOnMock.mock.calls[0][1]

progressCallback({
total: 3 * 1024 * 1024,
progress: 0.1,
})

progressCallback({
total: 3 * 1024 * 1024,
progress: 0.3,
})

expect(progressConstructorMock).toHaveBeenCalledTimes(1)
expect(progressConstructorMock).toHaveBeenCalledWith(zipFileResource, { throttle: 100 })

expect(progressMock.start).toHaveBeenCalledTimes(1)
expect(progressMock.start).toHaveBeenCalledWith({
barLength: 40,
steps: 3,
unit: 'MB',
showNumeric: true,
start: 'Downloading binary...',
success: 'Successfully downloaded to "chrome-filenameOS-x64-11.12.13.14.zip"',
fail: 'Failed to download binary'
})

expect(progressMock.fraction).toHaveBeenCalledTimes(1)
expect(progressMock.fraction).toHaveBeenCalledWith(0.3)
})

it('should extract the zip', async () => {
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn({
selectedVersion: new MappedVersion({
major: 11,
minor: 12,
branch: 13,
patch: 14,
disabled: false,
})
}))

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

// Act
const config = createChromeFullConfig({
autoUnzip: true,
Expand Down Expand Up @@ -425,12 +482,129 @@ describe('download', () => {
expect(spinnerMock.update.mock.calls).toEqual([['Extracting: some-file'], ['Extracting: some-file2']])
})

it.skip('should log an error on failing to remove zip file after extracting', () => {
// download.ts:67
it('should log an error on failing to remove zip file after extracting', async () => {
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn({
selectedVersion: new MappedVersion({
major: 11,
minor: 12,
branch: 13,
patch: 14,
disabled: false,
})
}))

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

unlinkMock.mockImplementation(() => {
throw new Error('unlink-error')
})

// Act
const config = createChromeFullConfig({
autoUnzip: true,
})
await downloadChromium(config)

expect(onMock).toHaveBeenCalledTimes(3)
expect(onMock).toHaveBeenCalledWith('finish', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('end', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('error', expect.any(Function))

const progressCallback = progressOnMock.mock.calls[0][1]
const extractCallback = extractMock.mock.calls[0][1].onEntry

progressCallback({
total: 3 * 1024 * 1024,
progress: 0.1,
})

extractCallback({fileName: 'some-file'})
extractCallback({fileName: 'some-file2'})

expect(progressConstructorMock).toHaveBeenCalledTimes(1)
expect(progressConstructorMock).toHaveBeenCalledWith(zipFileResource, { throttle: 100 })

expect(progressMock.start).toHaveBeenCalledTimes(1)
expect(progressMock.start).toHaveBeenCalledWith({
barLength: 40,
steps: 3,
unit: 'MB',
showNumeric: true,
start: 'Downloading binary...',
success: 'Successfully downloaded to "chrome-filenameOS-x64-11.12.13.14.zip"',
fail: 'Failed to download binary'
})

expect(progressMock.fraction).toHaveBeenCalledTimes(0)

expect(spinnerMock.update).toHaveBeenCalledTimes(2)
expect(spinnerMock.update.mock.calls).toEqual([['Extracting: some-file'], ['Extracting: some-file2']])
expect(logger.error).toHaveBeenCalledTimes(1)
expect(logger.error).toHaveBeenCalledWith('Error removing zip file after extracting: Error: unlink-error')
})

it.skip('should stop the spinner with error on error extracting', () => {
it('should stop the spinner with error on error extracting', async () => {
// download.ts:70

getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn({
selectedVersion: new MappedVersion({
major: 11,
minor: 12,
branch: 13,
patch: 14,
disabled: false,
})
}))

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

extractMock.mockImplementation(() => {
throw new Error('extract-error')
})

// Act
const config = createChromeFullConfig({
autoUnzip: true,
})
await downloadChromium(config)

expect(onMock).toHaveBeenCalledTimes(3)
expect(onMock).toHaveBeenCalledWith('finish', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('end', expect.any(Function))
expect(onMock).toHaveBeenCalledWith('error', expect.any(Function))

const progressCallback = progressOnMock.mock.calls[0][1]
const extractCallback = extractMock.mock.calls[0][1].onEntry

progressCallback({
total: 3 * 1024 * 1024,
progress: 0.1,
})

extractCallback({fileName: 'some-file'})
extractCallback({fileName: 'some-file2'})

expect(progressConstructorMock).toHaveBeenCalledTimes(1)
expect(progressConstructorMock).toHaveBeenCalledWith(zipFileResource, { throttle: 100 })

expect(progressMock.start).toHaveBeenCalledTimes(1)
expect(progressMock.start).toHaveBeenCalledWith({
barLength: 40,
steps: 3,
unit: 'MB',
showNumeric: true,
start: 'Downloading binary...',
success: 'Successfully downloaded to "chrome-filenameOS-x64-11.12.13.14.zip"',
fail: 'Failed to download binary'
})

expect(progressMock.fraction).toHaveBeenCalledTimes(0)

expect(spinnerMock.update).toHaveBeenCalledTimes(2)
expect(spinnerMock.update.mock.calls).toEqual([['Extracting: some-file'], ['Extracting: some-file2']])

expect(spinner.error).toHaveBeenCalledTimes(1)
expect(spinner.error).toHaveBeenCalledWith('Error: extract-error')
})

it('should do nothing on --no-download', async () => {
Expand Down

0 comments on commit 0e24863

Please sign in to comment.