Skip to content

Commit

Permalink
log a warning if all versions are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 30, 2022
1 parent 110b125 commit 4d48e11
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
50 changes: 48 additions & 2 deletions select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
* @group unit/file/select
*/

import type { MaybeMocked } from 'ts-jest/dist/utils/testing'
import type { MaybeMocked, MaybeMockedDeep } from 'ts-jest/dist/utils/testing'
import { mocked } from 'ts-jest/utils'

import { MappedVersion } from './commons/MappedVersion'
import { logger } from './log/logger'
import { userSelectedVersion } from './select'
import { createChromeFullConfig } from './test/test.utils'

/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const prompts = require('prompts')

jest.mock('prompts')
jest.mock('./log/logger')

describe('userSelectedVersion', () => {
let promptsMock: MaybeMocked<typeof prompts>
let loggerMock: MaybeMockedDeep<typeof logger>

beforeEach(() => {
promptsMock = mocked(prompts)
promptsMock.mockClear()

loggerMock = mocked(logger, true)
loggerMock.warn.mockClear()
})

it('should select the vesion received by prompts', async () => {
Expand Down Expand Up @@ -55,6 +61,7 @@ describe('userSelectedVersion', () => {
choices: [mappedVersion1, mappedVersion2],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should automatically select the first entry on config.results === 1', async () => {
Expand All @@ -80,6 +87,7 @@ describe('userSelectedVersion', () => {

expect(await userSelectedVersion([mappedVersion1, mappedVersion2], config)).toEqual(mappedVersion1)
expect(promptsMock).toHaveBeenCalledTimes(0)
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should return null on config.results === 1 with no version', async () => {
Expand All @@ -89,6 +97,8 @@ describe('userSelectedVersion', () => {
})

expect(await userSelectedVersion([], config)).toBeNull()
expect(loggerMock.warn).toHaveBeenCalledTimes(1)
expect(loggerMock.warn).toHaveBeenCalledWith('All versions in the range are disabled, try a different range and amount!')
})

it('should return null on config.results === 1 with version disabled', async () => {
Expand All @@ -107,6 +117,37 @@ describe('userSelectedVersion', () => {

expect(await userSelectedVersion([mappedVersion1], config)).toBeNull()
expect(promptsMock).toHaveBeenCalledTimes(0)
expect(loggerMock.warn).toHaveBeenCalledTimes(1)
expect(loggerMock.warn).toHaveBeenCalledWith('All versions in the range are disabled, try a different range and amount!')

})

it('should return null with all versions disabled', async () => {
const mappedVersion1 = new MappedVersion({
major: 10,
minor: 0,
branch: 0,
patch: 0,
disabled: true
})

const mappedVersion2 = new MappedVersion({
major: 20,
minor: 0,
branch: 0,
patch: 0,
disabled: true
})

const config = createChromeFullConfig({
results: 1,
onlyNewestMajor: false,
})

expect(await userSelectedVersion([mappedVersion1, mappedVersion2], config)).toBeNull()
expect(promptsMock).toHaveBeenCalledTimes(0)
expect(loggerMock.warn).toHaveBeenCalledTimes(1)
expect(loggerMock.warn).toHaveBeenCalledWith('All versions in the range are disabled, try a different range and amount!')
})

it('should filter out not-newest major versions on --only-newest-major', async () => {
Expand Down Expand Up @@ -145,6 +186,7 @@ describe('userSelectedVersion', () => {
onlyNewestMajor: true,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion60_2, mappedVersion60, mappedVersion10_2, mappedVersion10], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
Expand All @@ -154,6 +196,7 @@ describe('userSelectedVersion', () => {
choices: [mappedVersion60_2, mappedVersion10_2],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should strip the amount of versions passed to prompts on --only-newest-major', async () => {
Expand Down Expand Up @@ -192,6 +235,7 @@ describe('userSelectedVersion', () => {
onlyNewestMajor: true,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion60, mappedVersion40, mappedVersion20, mappedVersion10], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
Expand All @@ -201,6 +245,7 @@ describe('userSelectedVersion', () => {
choices: [mappedVersion60, mappedVersion40, mappedVersion20],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should select the next version is newest major is disabled on --only-newest-major', async () => {
Expand All @@ -212,6 +257,7 @@ describe('userSelectedVersion', () => {
results: 2,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion20, mappedVersion201], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
Expand All @@ -221,6 +267,6 @@ describe('userSelectedVersion', () => {
choices: [mappedVersion20],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

})
2 changes: 2 additions & 0 deletions select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as prompts from 'prompts'
import type { MappedVersion } from './commons/MappedVersion'
import type { IChromeFullConfig } from './interfaces/interfaces'
import type { Nullable } from './interfaces/interfaces'
import { logger } from './log/logger'

/**
* Lets the user select a version via CLI prompt and returns it.
Expand All @@ -14,6 +15,7 @@ import type { Nullable } from './interfaces/interfaces'
*/
export async function userSelectedVersion(versions: MappedVersion[], config: IChromeFullConfig): Promise<Nullable<MappedVersion>> {
if (versions.every(version => version.disabled)) {
logger.warn('All versions in the range are disabled, try a different range and amount!')
return null
}
if (config.results === 1) {
Expand Down

0 comments on commit 4d48e11

Please sign in to comment.