From 479d51916f548c8cb72f0c5d872e42a9c74e289c Mon Sep 17 00:00:00 2001 From: Jialin Zhang Date: Mon, 13 May 2024 10:41:49 -0700 Subject: [PATCH 1/3] Fix GitPanel unit tests --- .../test-components/GitPanel.spec.tsx | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/__tests__/test-components/GitPanel.spec.tsx b/src/__tests__/test-components/GitPanel.spec.tsx index 05dbd1b3c..0d9d045b2 100644 --- a/src/__tests__/test-components/GitPanel.spec.tsx +++ b/src/__tests__/test-components/GitPanel.spec.tsx @@ -41,7 +41,11 @@ const mockedResponses: { * @private * @returns mock settings */ -function MockSettings(commitAndPush = true, promptUserIdentity = false) { +function MockSettings( + commitAndPush = true, + promptUserIdentity = false, + simpleStaging = false +) { return { changed: { connect: () => true, @@ -49,7 +53,8 @@ function MockSettings(commitAndPush = true, promptUserIdentity = false) { }, composite: { commitAndPush, - promptUserIdentity + promptUserIdentity, + simpleStaging } }; } @@ -151,8 +156,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 = { @@ -173,10 +176,15 @@ describe('GitPanel', () => { state: 0 }; + // @ts-expect-error turn off set status + props.model._setStatus = jest.fn(() => { + props.model._statusChanged.emit(props.model._status); + }); + render(); }); - 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); @@ -212,16 +220,15 @@ 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(); - 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); @@ -231,7 +238,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); @@ -247,7 +254,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); @@ -263,7 +270,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: { From 65598ca26ee06c8a879f0df5c0c500923354b8db Mon Sep 17 00:00:00 2001 From: Zach Sailer Date: Wed, 15 May 2024 13:56:33 -0700 Subject: [PATCH 2/3] Access private properties in tests --- src/__tests__/test-components/GitPanel.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__tests__/test-components/GitPanel.spec.tsx b/src/__tests__/test-components/GitPanel.spec.tsx index 0d9d045b2..03134c0dc 100644 --- a/src/__tests__/test-components/GitPanel.spec.tsx +++ b/src/__tests__/test-components/GitPanel.spec.tsx @@ -178,7 +178,7 @@ describe('GitPanel', () => { // @ts-expect-error turn off set status props.model._setStatus = jest.fn(() => { - props.model._statusChanged.emit(props.model._status); + props.model['_statusChanged'].emit(props.model['_status']); }); render(); From 02a8f595ad18affedafad5d534e2c9b44ea1fb48 Mon Sep 17 00:00:00 2001 From: Zach Sailer Date: Wed, 15 May 2024 15:43:56 -0700 Subject: [PATCH 3/3] re-render in GitPanel unit test --- src/__tests__/test-components/GitPanel.spec.tsx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/__tests__/test-components/GitPanel.spec.tsx b/src/__tests__/test-components/GitPanel.spec.tsx index 03134c0dc..4d264a5fa 100644 --- a/src/__tests__/test-components/GitPanel.spec.tsx +++ b/src/__tests__/test-components/GitPanel.spec.tsx @@ -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'; @@ -108,6 +108,7 @@ describe('GitPanel', () => { describe('#commitFiles()', () => { let commitSpy: jest.SpyInstance>; let configSpy: jest.SpyInstance>; + let renderResult: RenderResult; const commitSummary = 'Fix really stupid bug'; const commitDescription = 'This will probably break everything :)'; @@ -181,7 +182,7 @@ describe('GitPanel', () => { props.model['_statusChanged'].emit(props.model['_status']); }); - render(); + renderResult = render(); }); it('should commit when commit message is provided', async () => { @@ -223,6 +224,9 @@ describe('GitPanel', () => { it('should prompt for user identity if explicitly configured', async () => { configSpy.mockResolvedValue({ options: commitUser }); + props.settings = MockSettings(false, true) as any; + renderResult.rerender(); + mockUtils.showDialog.mockResolvedValue(dialogValue); await userEvent.type(screen.getAllByRole('textbox')[0], commitSummary);