From 47abced756a9fc85ff39d21bf5b75ec7a25f138a Mon Sep 17 00:00:00 2001 From: Afonso Jorge Ramos Date: Wed, 6 Dec 2023 17:13:01 +0000 Subject: [PATCH] chore: improve tests and snapshots --- src/components/fields/Checkbox.tsx | 4 +- src/routes/Settings.test.tsx | 50 ++++++++++------ .../__snapshots__/Settings.test.tsx.snap | 58 +++++++++---------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/components/fields/Checkbox.tsx b/src/components/fields/Checkbox.tsx index ae70b236c..4f4bc6583 100644 --- a/src/components/fields/Checkbox.tsx +++ b/src/components/fields/Checkbox.tsx @@ -27,7 +27,9 @@ export const FieldCheckbox = (props: IFieldCheckbox) => { diff --git a/src/routes/Settings.test.tsx b/src/routes/Settings.test.tsx index b7fe83bbd..ca951060e 100644 --- a/src/routes/Settings.test.tsx +++ b/src/routes/Settings.test.tsx @@ -1,4 +1,4 @@ -import { fireEvent, render } from '@testing-library/react'; +import { fireEvent, render, screen } from '@testing-library/react'; import React from 'react'; import { MemoryRouter } from 'react-router-dom'; import TestRenderer, { act } from 'react-test-renderer'; @@ -313,9 +313,6 @@ describe('routes/Settings.tsx', () => { }); it('should be able to enable colors', async () => { - let getByLabelText; - let findByLabelText; - jest.spyOn(apiRequests, 'apiRequestAuth').mockResolvedValue({ headers: { 'x-oauth-scopes': Constants.AUTH_SCOPE.join(', '), @@ -323,10 +320,7 @@ describe('routes/Settings.tsx', () => { } as unknown as AxiosResponse); await act(async () => { - const { - getByLabelText: getByLabelTextLocal, - findByLabelText: findByLabelTextLocal, - } = render( + render( { , ); - getByLabelText = getByLabelTextLocal; - findByLabelText = findByLabelTextLocal; }); - await findByLabelText('Use GitHub-like state colors'); + await screen.findByLabelText('Use GitHub-like state colors'); - fireEvent.click(getByLabelText('Use GitHub-like state colors')); + fireEvent.click(screen.getByLabelText('Use GitHub-like state colors')); expect(updateSetting).toHaveBeenCalledTimes(1); expect(updateSetting).toHaveBeenCalledWith('colors', true); }); - it('should not be able to disable colors', async () => { - let queryByLabelText; - + it('should not be able to enable colors', async () => { jest.spyOn(apiRequests, 'apiRequestAuth').mockResolvedValue({ headers: { 'x-oauth-scopes': 'read:user, notifications', @@ -361,11 +351,12 @@ describe('routes/Settings.tsx', () => { } as unknown as AxiosResponse); await act(async () => { - const { queryByLabelText: queryByLabelLocal } = render( + render( @@ -373,11 +364,32 @@ describe('routes/Settings.tsx', () => { , ); - queryByLabelText = queryByLabelLocal; }); expect( - queryByLabelText('Use GitHub-like state colors (requires re-auth)'), - ).toBeDefined(); + screen + .getByLabelText('Use GitHub-like state colors (requires repo scope)') + .closest('input'), + ).toHaveProperty('disabled', true); + + // click the checkbox + fireEvent.click( + screen.getByLabelText( + 'Use GitHub-like state colors (requires repo scope)', + ), + ); + + // check if the checkbox is still unchecked + expect( + screen.getByLabelText( + 'Use GitHub-like state colors (requires repo scope)', + ), + ).not.toBe('checked'); + + expect( + screen.getByLabelText( + 'Use GitHub-like state colors (requires repo scope)', + ).parentNode.parentNode, + ).toMatchSnapshot(); }); }); diff --git a/src/routes/__snapshots__/Settings.test.tsx.snap b/src/routes/__snapshots__/Settings.test.tsx.snap index 63574cbf5..c2dd928be 100644 --- a/src/routes/__snapshots__/Settings.test.tsx.snap +++ b/src/routes/__snapshots__/Settings.test.tsx.snap @@ -1,5 +1,33 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`routes/Settings.tsx should not be able to enable colors 1`] = ` +
+
+ +
+
+ +
+
+`; + exports[`routes/Settings.tsx should render itself & its children 1`] = `
Use GitHub-like state colors @@ -170,11 +193,6 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` @@ -200,11 +218,6 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` @@ -230,11 +243,6 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` @@ -260,11 +268,6 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = ` @@ -290,11 +293,6 @@ exports[`routes/Settings.tsx should render itself & its children 1`] = `