-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(testing): add unit tests for checkout
- Loading branch information
Showing
7 changed files
with
204 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import IRepoAdapter, { IEnvironmentVariables, IRepo } from './base'; | ||
|
||
const mockAdapter: IRepoAdapter = { | ||
getCandidateRepos: jest.fn() as unknown as (onRetry: jest.Mock) => Promise<IRepo[]>, | ||
parseRepo: jest.fn() as unknown as (repo: string) => IRepo, | ||
reposEqual: jest.fn() as unknown as (repo1: IRepo, repo2: IRepo) => boolean, | ||
stringifyRepo: jest.fn() as unknown as (repo: IRepo) => string, | ||
mapRepoAfterCheckout: jest.fn() as unknown as (repo: Readonly<IRepo>) => Promise<IRepo>, | ||
checkoutRepo: jest.fn() as unknown as (repo: IRepo) => Promise<void>, | ||
resetChangedFiles: jest.fn() as unknown as (repo: IRepo) => Promise<void>, | ||
resetRepoBeforeApply: jest.fn() as unknown as (repo: IRepo, force: boolean) => Promise<void>, | ||
commitRepo: jest.fn() as unknown as (repo: IRepo) => Promise<void>, | ||
pushRepo: jest.fn() as unknown as (repo: IRepo, force: boolean) => Promise<void>, | ||
createPullRequest: jest.fn() as unknown as ( | ||
repo: IRepo, | ||
message: string, | ||
upstreamOwner: string | ||
) => Promise<void>, | ||
getPullRequestStatus: jest.fn() as unknown as (repo: IRepo) => Promise<string[]>, | ||
getRepoDir: jest.fn() as unknown as (repo: IRepo) => string, | ||
getDataDir: jest.fn() as unknown as (repo: IRepo) => string, | ||
getBaseBranch: jest.fn() as unknown as (repo: IRepo) => string, | ||
getEnvironmentVariables: jest.fn() as unknown as (repo: IRepo) => Promise<IEnvironmentVariables>, | ||
}; | ||
|
||
export default mockAdapter; |
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,111 @@ | ||
import { IMigrationContext } from '../migration-context'; | ||
import checkout from './checkout'; | ||
import mockAdapter from '../adapters/adapter.mock'; | ||
import mockLogger from '../logger/logger.mock'; | ||
import mockSpinner from '../logger/spinner.mock'; | ||
import executeSteps from '../util/execute-steps'; | ||
|
||
jest.mock('fs-extra', () => { | ||
return { | ||
// Mock other methods as needed | ||
mkdirs: jest.fn().mockResolvedValue(undefined), | ||
pathExists: jest.fn().mockResolvedValue(true), | ||
readFile: jest.fn().mockResolvedValue('{"name": "test"}'), | ||
outputFile: jest.fn().mockResolvedValue(undefined), | ||
remove: jest.fn().mockResolvedValue(undefined), | ||
}; | ||
}); | ||
|
||
jest.mock('../util/execute-steps'); | ||
|
||
describe('checkout command', () => { | ||
const mockContext: IMigrationContext = { | ||
shepherd: { | ||
workingDirectory: 'workingDirectory', | ||
}, | ||
migration: { | ||
migrationDirectory: 'migrationDirectory', | ||
spec: { | ||
id: 'id', | ||
title: 'title', | ||
adapter: { | ||
type: 'adapter', | ||
}, | ||
hooks: {}, | ||
}, | ||
workingDirectory: 'workingDirectory', | ||
selectedRepos: [{ name: 'selectedRepos' }], | ||
repos: [{ name: 'selectedRepos' }], | ||
upstreamOwner: 'upstreamOwner', | ||
}, | ||
adapter: mockAdapter, | ||
logger: mockLogger, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('clones repos given a specific list of repos', async () => { | ||
(executeSteps as jest.Mock) | ||
.mockResolvedValueOnce({ | ||
succeeded: true, | ||
stepResults: [], | ||
}) | ||
.mockResolvedValueOnce({ | ||
succeeded: true, | ||
stepResults: [], | ||
}); | ||
await checkout(mockContext); | ||
expect(mockLogger.info).toHaveBeenCalledWith('Using 1 selected repos'); | ||
expect(mockAdapter.checkoutRepo).toHaveBeenCalledWith({ name: 'selectedRepos' }); | ||
expect(mockSpinner.succeed).toHaveBeenCalledWith('Checked out repo'); | ||
}); | ||
|
||
it('gets candidate repos when list of repos is not provided', async () => { | ||
mockContext.migration.selectedRepos = undefined; | ||
await checkout(mockContext); | ||
expect(mockAdapter.getCandidateRepos).toHaveBeenCalled(); | ||
expect(mockSpinner.succeed).toHaveBeenCalledWith('Loaded 0 repos'); | ||
}); | ||
|
||
it('handles errors when checking out repos', async () => { | ||
mockContext.migration.selectedRepos = [{ name: 'selectedRepos' }]; | ||
mockContext.migration.repos = null; | ||
mockAdapter.checkoutRepo = jest.fn().mockImplementationOnce(() => { | ||
throw new Error('Mocked error'); | ||
}); | ||
await checkout(mockContext); | ||
expect(mockLogger.error).toHaveBeenCalledWith(new Error('Mocked error')); | ||
expect(mockSpinner.fail).toHaveBeenCalledWith('Failed to check out repo; skipping'); | ||
}); | ||
|
||
it('handles errors when running should_migrate steps', async () => { | ||
mockContext.migration.selectedRepos = [{ name: 'selectedRepos' }]; | ||
mockContext.migration.repos = null; | ||
(executeSteps as jest.Mock).mockResolvedValueOnce({ | ||
succeeded: false, | ||
stepResults: [], | ||
}); | ||
await checkout(mockContext); | ||
expect(mockLogger.failIcon).toHaveBeenCalledWith( | ||
'Error running should_migrate steps; skipping' | ||
); | ||
}); | ||
|
||
it('handles errors when running post_checkout steps', async () => { | ||
mockContext.migration.selectedRepos = [{ name: 'selectedRepos' }]; | ||
(executeSteps as jest.Mock) | ||
.mockResolvedValueOnce({ | ||
succeeded: true, | ||
stepResults: [], | ||
}) | ||
.mockResolvedValueOnce({ | ||
succeeded: false, | ||
stepResults: [], | ||
}); | ||
|
||
await checkout(mockContext); | ||
expect(mockLogger.failIcon).toHaveBeenCalledWith('Error running post_checkout steps; skipping'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ILogger } from './index'; | ||
import mockSpinner from './spinner.mock'; | ||
|
||
const mockLogger: ILogger = { | ||
// Basic logging | ||
debug: jest.fn() as unknown as (message: string) => void, | ||
info: jest.fn() as unknown as (message: string) => void, | ||
warn: jest.fn() as unknown as (message: string) => void, | ||
error: jest.fn() as unknown as (message: string) => void, | ||
fatal: jest.fn() as unknown as (message: string) => void, | ||
succeedIcon: jest.fn() as unknown as (message: string) => void, | ||
failIcon: jest.fn() as unknown as (message: string) => void, | ||
warnIcon: jest.fn() as unknown as (message: string) => void, | ||
infoIcon: jest.fn() as unknown as (message: string) => void, | ||
spinner: jest.fn().mockImplementation(() => mockSpinner), | ||
}; | ||
|
||
export default mockLogger; |
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,15 @@ | ||
import { ISpinner } from '.'; | ||
|
||
const mockSpinner: ISpinner = { | ||
start: jest.fn(), | ||
stop: jest.fn(), | ||
succeed: jest.fn(), | ||
fail: jest.fn(), | ||
warn: jest.fn(), | ||
info: jest.fn(), | ||
clear: jest.fn(), | ||
render: jest.fn(), | ||
destroy: jest.fn(), | ||
}; | ||
|
||
export default mockSpinner; |