-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from storyblok/feature/refactor-language-comm…
…and-updated-dx feat: re-structured language command with sub commands
- Loading branch information
Showing
10 changed files
with
268 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
import chalk from 'chalk' | ||
import { languagesCommand } from '.' | ||
import { session } from '../../session' | ||
import { CommandError, konsola } from '../../utils' | ||
import { fetchLanguages, saveLanguagesToFile } from './actions' | ||
import { colorPalette } from '../../constants' | ||
|
||
vi.mock('./actions', () => ({ | ||
fetchLanguages: vi.fn(), | ||
saveLanguagesToFile: vi.fn(), | ||
})) | ||
|
||
vi.mock('../../creds', () => ({ | ||
getCredentials: vi.fn(), | ||
addCredentials: vi.fn(), | ||
removeCredentials: vi.fn(), | ||
removeAllCredentials: 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('languagesCommand', () => { | ||
describe('pull', () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
vi.clearAllMocks() | ||
// Reset the option values | ||
languagesCommand._optionValues = {} | ||
for (const command of languagesCommand.commands) { | ||
command._optionValues = {} | ||
} | ||
}) | ||
describe('default mode', () => { | ||
it('should prompt the user if operation was sucessfull', async () => { | ||
const mockResponse = { | ||
default_lang_name: 'en', | ||
languages: [ | ||
{ | ||
code: 'ca', | ||
name: 'Catalan', | ||
}, | ||
{ | ||
code: 'fr', | ||
name: 'French', | ||
}, | ||
], | ||
} | ||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
|
||
vi.mocked(fetchLanguages).mockResolvedValue(mockResponse) | ||
await languagesCommand.parseAsync(['node', 'test', 'pull', '--space', '12345']) | ||
expect(fetchLanguages).toHaveBeenCalledWith('12345', 'valid-token', 'eu') | ||
expect(saveLanguagesToFile).toHaveBeenCalledWith('12345', mockResponse, { | ||
path: undefined, | ||
}) | ||
expect(konsola.ok).toHaveBeenCalledWith(`Languages schema downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`.storyblok/languages/12345/languages.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 languagesCommand.parseAsync(['node', 'test', 'pull', '--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 languagesCommand.parseAsync(['node', 'test', 'pull']) | ||
expect(konsola.error).toHaveBeenCalledWith(mockError, false) | ||
}) | ||
|
||
it('should prompt a warning the user if no languages are found', async () => { | ||
const mockResponse = { | ||
default_lang_name: 'en', | ||
languages: [], | ||
} | ||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
|
||
vi.mocked(fetchLanguages).mockResolvedValue(mockResponse) | ||
|
||
await languagesCommand.parseAsync(['node', 'test', 'pull', '--space', '24568']) | ||
expect(konsola.warn).toHaveBeenCalledWith(`No languages found in the space 24568`) | ||
}) | ||
}) | ||
|
||
describe('--path option', () => { | ||
it('should save the file at the provided path', async () => { | ||
const mockResponse = { | ||
default_lang_name: 'en', | ||
languages: [ | ||
{ | ||
code: 'ca', | ||
name: 'Catalan', | ||
}, | ||
{ | ||
code: 'fr', | ||
name: 'French', | ||
}, | ||
], | ||
} | ||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
|
||
vi.mocked(fetchLanguages).mockResolvedValue(mockResponse) | ||
await languagesCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--path', '/tmp']) | ||
expect(fetchLanguages).toHaveBeenCalledWith('12345', 'valid-token', 'eu') | ||
expect(saveLanguagesToFile).toHaveBeenCalledWith('12345', mockResponse, { | ||
path: '/tmp', | ||
}) | ||
expect(konsola.ok).toHaveBeenCalledWith(`Languages schema downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`/tmp/languages.json`)}`) | ||
}) | ||
}) | ||
|
||
describe('--filename option', () => { | ||
it('should save the file with the provided filename', async () => { | ||
const mockResponse = { | ||
default_lang_name: 'en', | ||
languages: [ | ||
{ | ||
code: 'ca', | ||
name: 'Catalan', | ||
}, | ||
{ | ||
code: 'fr', | ||
name: 'French', | ||
}, | ||
], | ||
} | ||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
|
||
vi.mocked(fetchLanguages).mockResolvedValue(mockResponse) | ||
await languagesCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--filename', 'custom-languages']) | ||
expect(fetchLanguages).toHaveBeenCalledWith('12345', 'valid-token', 'eu') | ||
expect(saveLanguagesToFile).toHaveBeenCalledWith('12345', mockResponse, { | ||
filename: 'custom-languages', | ||
path: undefined, | ||
}) | ||
expect(konsola.ok).toHaveBeenCalledWith(`Languages schema downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`.storyblok/languages/12345/custom-languages.json`)}`) | ||
}) | ||
}) | ||
|
||
describe('--suffix option', () => { | ||
it('should save the file with the provided suffix', async () => { | ||
const mockResponse = { | ||
default_lang_name: 'en', | ||
languages: [ | ||
{ | ||
code: 'ca', | ||
name: 'Catalan', | ||
}, | ||
{ | ||
code: 'fr', | ||
name: 'French', | ||
}, | ||
], | ||
} | ||
session().state = { | ||
isLoggedIn: true, | ||
password: 'valid-token', | ||
region: 'eu', | ||
} | ||
|
||
vi.mocked(fetchLanguages).mockResolvedValue(mockResponse) | ||
await languagesCommand.parseAsync(['node', 'test', 'pull', '--space', '12345', '--suffix', 'custom-suffix']) | ||
expect(fetchLanguages).toHaveBeenCalledWith('12345', 'valid-token', 'eu') | ||
expect(saveLanguagesToFile).toHaveBeenCalledWith('12345', mockResponse, { | ||
suffix: 'custom-suffix', | ||
path: undefined, | ||
}) | ||
expect(konsola.ok).toHaveBeenCalledWith(`Languages schema downloaded successfully at ${chalk.hex(colorPalette.PRIMARY)(`.storyblok/languages/12345/languages.custom-suffix.json`)}`) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.