Skip to content

Commit

Permalink
add test for branch new command (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Opeyem1a authored Oct 11, 2024
1 parent e7b59a0 commit a79d7bb
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
119 changes: 119 additions & 0 deletions src/commands/branch/new.test.tsx
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';

Check warning on line 49 in src/commands/branch/new.test.tsx

View workflow job for this annotation

GitHub Actions / lint_test

'SUCCESS_MESSAGE' is assigned a value but never used

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());
});
});
2 changes: 1 addition & 1 deletion src/commands/changes/add.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 ARBITRARY_DELAY = 120; // ms

const mocks = vi.hoisted(() => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/changes/commit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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 ARBITRARY_DELAY = 120; // ms

const mocks = vi.hoisted(() => {
return {
Expand Down

0 comments on commit a79d7bb

Please sign in to comment.