Skip to content

Fix GitPanel unit tests #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/__tests__/test-components/GitPanel.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as apputils from '@jupyterlab/apputils';
import { nullTranslator } from '@jupyterlab/translation';
import { JSONObject } from '@lumino/coreutils';
import '@testing-library/jest-dom';
import { render, screen, waitFor } from '@testing-library/react';
import { RenderResult, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import 'jest';
import React from 'react';
Expand Down Expand Up @@ -41,15 +41,20 @@ const mockedResponses: {
* @private
* @returns mock settings
*/
function MockSettings(commitAndPush = true, promptUserIdentity = false) {
function MockSettings(
commitAndPush = true,
promptUserIdentity = false,
simpleStaging = false
) {
return {
changed: {
connect: () => true,
disconnect: () => true
},
composite: {
commitAndPush,
promptUserIdentity
promptUserIdentity,
simpleStaging
}
};
}
Expand Down Expand Up @@ -103,6 +108,7 @@ describe('GitPanel', () => {
describe('#commitFiles()', () => {
let commitSpy: jest.SpyInstance<Promise<void>>;
let configSpy: jest.SpyInstance<Promise<void | JSONObject>>;
let renderResult: RenderResult;

const commitSummary = 'Fix really stupid bug';
const commitDescription = 'This will probably break everything :)';
Expand Down Expand Up @@ -151,8 +157,6 @@ describe('GitPanel', () => {
beforeEach(() => {
configSpy = props.model.config = jest.fn();
commitSpy = props.model.commit = jest.fn();
// @ts-expect-error turn off set status
props.model._setStatus = jest.fn();

// @ts-expect-error set a private prop
props.model._status = {
Expand All @@ -173,10 +177,15 @@ describe('GitPanel', () => {
state: 0
};

render(<GitPanel {...props} />);
// @ts-expect-error turn off set status
props.model._setStatus = jest.fn(() => {
props.model['_statusChanged'].emit(props.model['_status']);
});

renderResult = render(<GitPanel {...props} />);
});

it.skip('should commit when commit message is provided', async () => {
it('should commit when commit message is provided', async () => {
configSpy.mockResolvedValue({ options: commitUser });

await userEvent.type(screen.getAllByRole('textbox')[0], commitSummary);
Expand Down Expand Up @@ -212,16 +221,18 @@ describe('GitPanel', () => {
expect(commitSpy).not.toHaveBeenCalled();
});

it.skip('should prompt for user identity if explicitly configured', async () => {
it('should prompt for user identity if explicitly configured', async () => {
configSpy.mockResolvedValue({ options: commitUser });

props.settings = MockSettings(false, true) as any;
render(<GitPanel {...props} />);
renderResult.rerender(<GitPanel {...props} />);

mockUtils.showDialog.mockResolvedValue(dialogValue);

await userEvent.type(screen.getAllByRole('textbox')[0], commitSummary);
await userEvent.click(screen.getByRole('button', { name: 'Commit' }));
await userEvent.click(
screen.getAllByRole('button', { name: 'Commit' })[0]
);

expect(configSpy).toHaveBeenCalledTimes(1);
expect(configSpy.mock.calls[0]).toHaveLength(0);
Expand All @@ -231,7 +242,7 @@ describe('GitPanel', () => {
expect(commitSpy).toHaveBeenCalledWith(commitSummary, false, author);
});

it.skip('should prompt for user identity if user.name is not set', async () => {
it('should prompt for user identity if user.name is not set', async () => {
configSpy.mockImplementation(mockConfigImplementation('user.email'));
mockUtils.showDialog.mockResolvedValue(dialogValue);

Expand All @@ -247,7 +258,7 @@ describe('GitPanel', () => {
expect(commitSpy).toHaveBeenCalledWith(commitSummary, false, null);
});

it.skip('should prompt for user identity if user.email is not set', async () => {
it('should prompt for user identity if user.email is not set', async () => {
configSpy.mockImplementation(mockConfigImplementation('user.name'));
mockUtils.showDialog.mockResolvedValue(dialogValue);

Expand All @@ -263,7 +274,7 @@ describe('GitPanel', () => {
expect(commitSpy).toHaveBeenCalledWith(commitSummary, false, null);
});

it.skip('should not commit if no user identity is set and the user rejects the dialog', async () => {
it('should not commit if no user identity is set and the user rejects the dialog', async () => {
configSpy.mockResolvedValue({ options: {} });
mockUtils.showDialog.mockResolvedValue({
button: {
Expand Down
Loading