-
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.
* changes add command * use changes add properly * ultra basic tests * add props * add props
- Loading branch information
Showing
4 changed files
with
196 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import ChangesAdd from './add.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'; | ||
|
||
const ARBITRARY_DELAY = 250; // ms | ||
|
||
const mocks = vi.hoisted(() => { | ||
return { | ||
createGitService: vi.fn(({}) => { | ||
return { | ||
addAllFiles: async () => { | ||
return new Promise((resolve) => | ||
setTimeout(resolve, ARBITRARY_DELAY) | ||
); | ||
}, | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
vi.mock('../../services/git.js', () => { | ||
return { | ||
DEFAULT_OPTIONS: {}, | ||
createGitService: mocks.createGitService, | ||
}; | ||
}); | ||
|
||
const LOADING_MESSAGE = 'Loading...'; | ||
const SUCCESS_MESSAGE = 'Staged all changes'; | ||
|
||
describe('correctly renders changes add UI', () => { | ||
it('runs as intended', async () => { | ||
const actual1 = render( | ||
<ChangesAdd | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['changes', 'add']} | ||
/> | ||
); | ||
|
||
const actual2 = render( | ||
<ChangesAdd | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['changes', 'a']} | ||
/> | ||
); | ||
|
||
const ExpectedComp = () => { | ||
return ( | ||
<Text bold color="green"> | ||
{SUCCESS_MESSAGE} | ||
</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( | ||
<ChangesAdd | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['changes', 'add']} | ||
/> | ||
); | ||
|
||
const actual2 = render( | ||
<ChangesAdd | ||
cli={{ | ||
flags: {}, | ||
unnormalizedFlags: {}, | ||
}} | ||
input={['changes', 'a']} | ||
/> | ||
); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import ErrorDisplay from '../../components/error-display.js'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { CommandConfig, CommandProps } from '../../types.js'; | ||
import { Text } from 'ink'; | ||
import { useGit } from '../../hooks/use-git.js'; | ||
|
||
function ChangedAdd({}: CommandProps) { | ||
const result = useChangesAdd(); | ||
|
||
if (result.isError) { | ||
return <ErrorDisplay error={result.error} />; | ||
} | ||
|
||
if (result.isLoading) { | ||
return <Text color="cyan">Loading...</Text>; | ||
} | ||
|
||
return ( | ||
<Text bold color="green"> | ||
Staged all changes | ||
</Text> | ||
); | ||
} | ||
|
||
type Action = { isLoading: boolean } & ( | ||
| { | ||
isError: false; | ||
} | ||
| { | ||
isError: true; | ||
error: Error; | ||
} | ||
); | ||
|
||
type State = | ||
| { | ||
type: 'LOADING'; | ||
} | ||
| { | ||
type: 'COMPLETE'; | ||
} | ||
| { | ||
type: 'ERROR'; | ||
error: Error; | ||
}; | ||
|
||
const useChangesAdd = (): Action => { | ||
const git = useGit(); | ||
const [state, setState] = useState<State>({ type: 'LOADING' }); | ||
|
||
useEffect(() => { | ||
git.addAllFiles() | ||
.then(() => setState({ type: 'COMPLETE' })) | ||
.catch((e: Error) => { | ||
setState({ type: 'ERROR', error: e }); | ||
}); | ||
}, []); | ||
|
||
if (state.type === 'ERROR') { | ||
return { | ||
isLoading: false, | ||
isError: true, | ||
error: state.error, | ||
}; | ||
} | ||
|
||
return { | ||
isLoading: state.type === 'LOADING', | ||
isError: false, | ||
}; | ||
}; | ||
|
||
export const changesAddConfig: CommandConfig = { | ||
description: 'Stage all changes.', | ||
usage: 'changes add', | ||
key: 'add', | ||
aliases: ['a'], | ||
}; | ||
|
||
export default ChangedAdd; |
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