Skip to content

Commit

Permalink
feat: basic tests for pull components command
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Nov 5, 2024
1 parent cc2b7b8 commit 8668e31
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 2 deletions.
187 changes: 187 additions & 0 deletions src/commands/pull-components/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
import { session } from '../../session'
import { CommandError, konsola } from '../../utils'
import { pullComponents, saveComponentsToFiles } from './actions'
import { pullComponentsCommand } from '.'
import chalk from 'chalk'
import { colorPalette } from '../../constants'
import { saveLanguagesToFile } from '../pull-languages/actions'

Check failure on line 7 in src/commands/pull-components/index.test.ts

View workflow job for this annotation

GitHub Actions / Lint (20)

'saveLanguagesToFile' is defined but never used

vi.mock('./actions', () => ({
pullComponents: vi.fn(),
saveComponentsToFiles: vi.fn(),
}))

vi.mock('../../creds', () => ({
addNetrcEntry: vi.fn(),
isAuthorized: vi.fn(),
getNetrcCredentials: vi.fn(),
getCredentialsForMachine: vi.fn(),
}))

// Mocking the session module
vi.mock('../../session', () => {
let _cache: Record<string, any> | null = null
const session = () => {
if (!_cache) {
_cache = {
state: {
isLoggedIn: false,
},
updateSession: vi.fn(),
persistCredentials: vi.fn(),
initializeSession: vi.fn(),
}
}
return _cache
}

return {
session,
}
})

vi.mock('../../utils', async () => {
const actualUtils = await vi.importActual('../../utils')
return {
...actualUtils,
konsola: {
ok: vi.fn(),
title: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
handleError: (error: Error, header = false) => {
konsola.error(error, header)
// Optionally, prevent process.exit during tests
},
}
})

describe('pullComponents', () => {
beforeEach(() => {
vi.resetAllMocks()
vi.clearAllMocks()
// Reset the option values
pullComponentsCommand._optionValues = {}
})

describe('default mode', () => {
it('should prompt tge yser if the operation was sucessfull', async () => {
const mockResponse = [{
name: 'component-name',
display_name: 'Component Name',
created_at: '2021-08-09T12:00:00Z',
updated_at: '2021-08-09T12:00:00Z',
id: 12345,
schema: { type: 'object' },
color: null,
internal_tags_list: ['tag'],
interntal_tags_ids: [1],
}, {
name: 'component-name-2',
display_name: 'Component Name 2',
created_at: '2021-08-09T12:00:00Z',
updated_at: '2021-08-09T12:00:00Z',
id: 12346,
schema: { type: 'object' },
color: null,
internal_tags_list: ['tag'],
interntal_tags_ids: [1],
}]

session().state = {
isLoggedIn: true,
password: 'valid-token',
region: 'eu',
}

vi.mocked(pullComponents).mockResolvedValue(mockResponse)
await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345'])
expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu')
expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, {

})
expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`components.12345.json`)}`)
})

it('should throw an error if the user is not logged in', async () => {
session().state = {
isLoggedIn: false,
}
const mockError = new CommandError(`You are currently not logged in. Please login first to get your user info.`)
await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345'])
expect(konsola.error).toHaveBeenCalledWith(mockError, false)
})

it('should throw an error if the space is not provided', async () => {
session().state = {
isLoggedIn: true,
password: 'valid-token',
region: 'eu',
}

const mockError = new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`)

await pullComponentsCommand.parseAsync(['node', 'test'])
expect(konsola.error).toHaveBeenCalledWith(mockError, false)
})
})

describe('--path option', () => {
it('should save the file at the provided path', async () => {
const mockResponse = [{
name: 'component-name',
display_name: 'Component Name',
created_at: '2021-08-09T12:00:00Z',
updated_at: '2021-08-09T12:00:00Z',
id: 12345,
schema: { type: 'object' },
color: null,
internal_tags_list: ['tag'],
interntal_tags_ids: [1],
}]

session().state = {
isLoggedIn: true,
password: 'valid-token',
region: 'eu',
}

vi.mocked(pullComponents).mockResolvedValue(mockResponse)

await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--path', '/path/to/components'])
expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu')
expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { path: '/path/to/components' })
expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`/path/to/components/components.12345.json`)}`)
})
})

describe('--filename option', () => {
it('should save the file with the custom filename', async () => {
const mockResponse = [{
name: 'component-name',
display_name: 'Component Name',
created_at: '2021-08-09T12:00:00Z',
updated_at: '2021-08-09T12:00:00Z',
id: 12345,
schema: { type: 'object' },
color: null,
internal_tags_list: ['tag'],
interntal_tags_ids: [1],
}]

session().state = {
isLoggedIn: true,
password: 'valid-token',
region: 'eu',
}

vi.mocked(pullComponents).mockResolvedValue(mockResponse)

await pullComponentsCommand.parseAsync(['node', 'test', '--space', '12345', '--filename', 'custom'])
expect(pullComponents).toHaveBeenCalledWith('12345', 'valid-token', 'eu')
expect(saveComponentsToFiles).toHaveBeenCalledWith('12345', mockResponse, { filename: 'custom' })
expect(konsola.ok).toHaveBeenCalledWith(`Components downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`custom.json`)}`)
})
})
})
3 changes: 2 additions & 1 deletion src/commands/pull-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export const pullComponentsCommand = program
path,
filename,
})
konsola.ok(`Components downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(path ? `${path}/components.${space}.json` : `components.${space}.json`)}`)
const msgFilename = filename ? `${filename}.json` : `components.${space}.json`
konsola.ok(`Components downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(path ? `${path}/${msgFilename}` : `${msgFilename}`)}`)
}
catch (error) {
handleError(error as Error, verbose)
Expand Down
1 change: 0 additions & 1 deletion src/commands/pull-languages/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ describe('pullLanguages', () => {

const mockError = new CommandError(`Please provide the space as argument --space YOUR_SPACE_ID.`)

console.log(pullLanguagesCommand)
await pullLanguagesCommand.parseAsync(['node', 'test'])
expect(konsola.error).toHaveBeenCalledWith(mockError, false)
})
Expand Down

0 comments on commit 8668e31

Please sign in to comment.