-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for branch new command (#29)
- Loading branch information
Showing
3 changed files
with
121 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import BranchNew from './new.js'; | ||
import React from 'react'; | ||
import { Text } from 'ink'; | ||
import { delay } from '../../utils/time.js'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { render } from '@levelbreaded/ink-testing-library'; | ||
import { safeBranchNameFromCommitMessage } from '../../utils/naming.js'; | ||
|
||
const ARBITRARY_DELAY = 120; // ms | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
createGitService: vi.fn(({}) => { | ||
return { | ||
checkout: async () => { | ||
return new Promise((resolve) => | ||
setTimeout(resolve, ARBITRARY_DELAY / 4) | ||
); | ||
}, | ||
createBranch: async () => { | ||
return new Promise((resolve) => | ||
setTimeout(resolve, ARBITRARY_DELAY / 4) | ||
); | ||
}, | ||
addAllFiles: async () => { | ||
return new Promise((resolve) => | ||
setTimeout(resolve, ARBITRARY_DELAY / 4) | ||
); | ||
}, | ||
commit: async ({ message }: { message: string }) => { | ||
console.log(message); | ||
return new Promise((resolve) => | ||
setTimeout(resolve, ARBITRARY_DELAY / 4) | ||
); | ||
}, | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
vi.mock('../../services/git.js', () => { | ||
return { | ||
DEFAULT_OPTIONS: {}, | ||
createGitService: mocks.createGitService, | ||
}; | ||
}); | ||
|
||
const LOADING_MESSAGE = 'Loading...'; | ||
const SUCCESS_MESSAGE = 'Committed all changes'; | ||
|
||
describe('correctly renders changes commit UI', () => { | ||
it('runs as intended', async () => { | ||
const actual1 = render( | ||
<BranchNew | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['branch', 'new', 'commit message']} | ||
/> | ||
); | ||
|
||
const actual2 = render( | ||
<BranchNew | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['branch', 'n', 'commit message']} | ||
/> | ||
); | ||
|
||
const newBranchName = safeBranchNameFromCommitMessage('commit message'); | ||
|
||
const ExpectedComp = () => { | ||
return ( | ||
<Text bold color="green"> | ||
New branch created - {newBranchName} | ||
</Text> | ||
); | ||
}; | ||
const expected = render(<ExpectedComp />); | ||
|
||
await delay(ARBITRARY_DELAY + 250); | ||
expect(actual1.lastFrame()).to.equal(expected.lastFrame()); | ||
expect(actual2.lastFrame()).to.equal(expected.lastFrame()); | ||
}); | ||
|
||
it('displays a loading state while processing', async () => { | ||
const actual1 = render( | ||
<BranchNew | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['branch', 'new', 'commit message']} | ||
/> | ||
); | ||
|
||
const actual2 = render( | ||
<BranchNew | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['branch', 'n', 'commit message']} | ||
/> | ||
); | ||
|
||
const ExpectedComp = () => { | ||
return <Text color="cyan">{LOADING_MESSAGE}</Text>; | ||
}; | ||
const expected = render(<ExpectedComp />); | ||
|
||
await delay(ARBITRARY_DELAY / 2); | ||
expect(actual1.lastFrame()).to.equal(expected.lastFrame()); | ||
expect(actual2.lastFrame()).to.equal(expected.lastFrame()); | ||
}); | ||
}); |
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